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
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);
}
}
}
}
}