Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Dimming colour light
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Dimming colour light

by dylan2106 on Wed May 28, 2008 11:10 am

Hi, thanks for any help anyone can offer in advance. New to programming so its a bit confusing at times (but fun when i can figure it out! :D )

anyway I'm trying to do an extention of the button controlled blinky light tutorial hxxp://www.ladyada.net/learn/arduino/lesson5.html

But having the LEDs dimming and mixing like a colour mood light. I've run into a few problems in such as when I have the LEDs cycle colour (by dimming and relighting) the button does not cause the mode to change. I've tried debugging with a println, and it shows that the button state is not changing at all...
here is the code

Code: Select all | TOGGLE FULL SIZE


int switchPin = 2;              // switch is connected to pin 2
int ledpin1 = 10;
int ledpin2 = 9;
int ledpin3 = 6;

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?

int value1 = 0;                            // variable to keep the actual value
int value2 = 0;
int value3 = 0;

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(ledpin3, OUTPUT);
 
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // light is off
          lightMode = 1;               // turn light on!
          Serial.println("mode 0");
        } else {
              if (lightMode == 1) {
                lightMode = 2;
                Serial.println ("mode 1");
              } else {
                if (lightMode == 2){
                  lightMode = 3;
                  Serial.println ("mode 2");
                } else {
                  if (lightMode == 3){
                    lightMode = 0;
                    Serial.println("mode 3");
                  }
                }
              }
   
          }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 1) {
    digitalWrite(ledpin1, HIGH);
    digitalWrite(ledpin2, HIGH);
    digitalWrite(ledpin3, HIGH);
  } else {
    if (lightMode == 2) {
        for(value1 = 0 ; value1 <= 255; value1+=5) // fade in (from min to max) led1
          {
          analogWrite(ledpin1, value1);           // sets the value (range from 0 to 255)
          delay(30);                            // waits for 30 milli seconds to see the dimming effect
           }
 
         for(value3 = 255; value3 >=0; value3-=5)   // fade out (from max to min) led1
          {
          analogWrite(ledpin3, value3);
          delay(30);
          }
         
       for(value2 = 0 ; value2 <= 255; value2+=5) // fade in (from min to max) led2
          {
          analogWrite(ledpin2, value2);           // sets the value (range from 0 to 255)
          delay(30);                            // waits for 30 milli seconds to see the dimming effect
          }
 
          for(value1 = 255; value1 >=0; value1-=5)   // fade out (from max to min) led1
          {
          analogWrite(ledpin1, value1);
          delay(30);
          }
 
          for(value3 = 0 ; value3 <= 255; value3+=5) // fade in (from min to max) led3
          {
          analogWrite(ledpin3, value3);           // sets the value (range from 0 to 255)
          delay(30);                            // waits for 30 milli seconds to see the dimming effect
          }
 
 
          for(value2 = 255; value2 >=0; value2-=5)   // fade out (from max to min) led2
          {
          analogWrite(ledpin2, value2);
          delay(30);
          }

    } else {
      if (lightMode == 3) {
         for(value1 = 0 ; value1 <= 255; value1+=5) // fade in (from min to max) led1
          {
          analogWrite(ledpin1, value1);           // sets the value (range from 0 to 255)
          delay(10);                            // waits for 30 milli seconds to see the dimming effect
           }
           
         for(value3 = 255; value3 >=0; value3-=5)   // fade out (from max to min) led1
          {
          analogWrite(ledpin3, value3);
          delay(10);
          }
 
          for(value2 = 0 ; value2 <= 255; value2+=5) // fade in (from min to max) led2
          {
          analogWrite(ledpin2, value2);           // sets the value (range from 0 to 255)
          delay(10);                            // waits for 30 milli seconds to see the dimming effect
          }
 
          for(value1 = 255; value1 >=0; value1-=5)   // fade out (from max to min) led1
          {
          analogWrite(ledpin1, value1);
          delay(10);
          }
 
          for(value3 = 0 ; value3 <= 255; value3+=5) // fade in (from min to max) led3
          {
          analogWrite(ledpin3, value3);           // sets the value (range from 0 to 255)
          delay(10);                            // waits for 30 milli seconds to see the dimming effect
          }
 
 
          for(value2 = 255; value2 >=0; value2-=5)   // fade out (from max to min) led2
          {
          analogWrite(ledpin2, value2);
          delay(10);
          }
       
     
    } else {
        if (lightMode == 0) {
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
        }
      }
    }
  }
}


thanks again if anyone can offer any advice, as I said, not really sure what i am doing at all, usually try and figure it out, but I am a stumped on this one...
dylan2106
 
Posts: 1
Joined: Wed May 28, 2008 11:01 am

by Silver on Tue Jun 03, 2008 4:00 am

I wonder if you problem occurs after you start using pin6 as an analog output. I try to avoid using pins 5 and 6 as analog outputs because of a known interaction with the delay() function.

From the Arduino reference page:
Notes and Known Issues

You do not need to call pinMode() to set the pin as an output before calling analogWrite().

The frequency of the PWM signal is approximately 490 Hz.

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs.


Because you used variable for the pin assignments, you should be able to easily test this by moving your LED 3 connection from pin 6 to pin 11 and changing your code to "int ledpin3 = 11"

Hope this fixes your problem.
Silver
 
Posts: 12
Joined: Mon Apr 21, 2008 1:39 am

Please be positive and constructive with your questions and comments.