I'm supposing that since there is no wait between debouncing the button and looping through the code, you just keep looping and WHEN the button is pressed a pseudoRANDOM part of your code activates, "hey I got a button press after the red but before the green section so I'll light up green'.
doesn't do exactly as you say, because it doesn't wait for the old animation before mixing colours...
Includes debounce code from http://www.arduino.cc/en/Tutorial/Debounce
I don't have my duino plugged in, or any buttons/leds handy, so I can't actually test it but I'm fairly confident this will work.
const int triggerPin = 2;
const int rledPin = 9;
const int gledPin = 10;
const int bledPin = 11;
byte rValue = 255; //global values for the leds
byte gValue = 255;
byte bValue = 255;
int animStep = 0; //value from 0-3, referring to white, red, green, blue
int dimmingTimer = 2; //number of mS between updates (ish). higher = slower
boolean lastButtonState = LOW; //used for debounce, previous reading of button state
boolean buttonState = LOW; //current reading of button state
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup ()
{
pinMode (triggerPin, INPUT);
pinMode (rledPin, OUTPUT);
pinMode (gledPin, OUTPUT);
pinMode (bledPin, OUTPUT);
}
void loop()
{
//update leds
displayColour();
//dim led values
dimLedValues();
//check for a button press, assigns values to buttonState (global)
checkButton();
//act on button if pressed, setting appropriate led value high
if (buttonState) {
updateLedValues();
buttonState = LOW;
} //end if
//animation delay
delay(dimmingTimer);
}//end loop
void updateLedValues() //button detected, update led values
{
switch (animStep) { //do cool stuff in here.
case 0: //first step, white
rValue = 255;
gValue = 255;
bValue = 255;
break;
case 1: //add red
rValue = 255;
break;
case 2: //add green
gValue = 255;
break;
case 3: //add blue
bValue = 255;
break;
default:
break;
// statements
}//end switch animStep
animStep++;
if (animStep > 3) animStep = 0;//loop animation
}//end updateLedValues
void checkButton() //includes debounce code from http://www.arduino.cc/en/Tutorial/Debounce
{ //saves a debounced button state to global buttonState
// read the state of the switch into a local variable:
int reading = digitalRead(triggerPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
void dimLedValues()//move r g b values down one notch if above zero
{
if (rValue>0) rValue--;
if (gValue>0) rValue--;
if (bValue>0) rValue--;
}
void displayColour() //set the analog outputs to the values set elsewhere
{
analogWrite(rledPin,rValue);
analogWrite(gledPin,gValue);
analogWrite(bledPin,gValue);
}//end displayColour