To me though there is a nicer way to do this (add LEDs and/or patterns): using MULTIDIMENSIONAL ARRAYS!! 8) and Arrays
From http://arduino.cc/en/Reference/Array:
An array is a collection of variables that are accessed with an index number.
All of the methods below are valid ways to create (declare) an array.
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";
So a MULTIDIMENSIONAL ARRAY would be one array with other arrays inside it: in this case, to hold patterns:
LEDPattern[numberofPatterns][numberofLEDs] {
{0, 0, 0} ,
{1, 0, 0} ,
{1, 1, 0} ,
{1, 1, 1} ,
{0, 1, 1} ,
{0, 1, 0} ,
{0, 0, 1} ,
{1, 0, 1}
};
We'll use a array to store LEDPins to make the code even shorter.
Thanks to both arrays mentioned a simple for(){} loop can get the job done and adding and changing patterns or LEDs will be much easier (and faster)
Note:(didn't post this yesterday cause for some reason it was slow).
We'd end up with:
//LED Pins
const int numberofLEDs = 3;
const int LEDPin[numberofLEDs] = {22, 23, 24}; //array for pin numbers in color order of pattern (red, yellow, green)
//Button Pin
const int buttonPin = 3;
//Variables for Debounce (Button has to be pressed for a certain amount of time for press to count)
const int debounceDelay = 10; //miliseconds the button has to be pressed for press to count
boolean state; //actual state of button (for debounce)
boolean previousState; //last state of button (for debounce)
int buttonPushCounter = 0; //counts presses
boolean buttonState = 0; //actual state of button, LOW
boolean lastButtonState = 0; //last state of button, LOW
const int numberofPatterns = 7; //number of patterns indexed to whatever cases are (this case 0 indexed, 0 to 7)
const int LEDPattern[(numberofPatterns + 1)][numberofLEDs] = { //multidimensional array ([var] in declaration, index 1. When accessing, index 0
{ 0, 0, 0 } , //LEDPattern[0][0] will access the first value in this row
{ 1, 0, 0 } ,
{ 1, 1, 0 } ,
{ 1, 1, 1 } ,
{ 0, 1, 1 } ,
{ 0, 1, 0 } ,
{ 0, 0, 1 } ,
{ 1, 0, 1 }
};
void setup() {
pinMode(buttonPin, INPUT);
for (int i = 0; i < numberofLEDs; i++) { //Taking advantage of LEDPin array(when you add more LEDs this stays the same)
pinMode(LEDPin[i], OUTPUT);
}
}
void loop () {
if (ButtonChange() == HIGH) { //if button has been pressed
if (buttonPushCounter > numberofPatterns) { //if counter exeeds max reset to index(0)
buttonPushCounter = 0;
}
for (int i = 0; i < numberofLEDs; i++) { //once per LED
digitalWrite(LEDPin[i], LEDPattern[buttonPushCounter][i]); //access array to know Pin, acces multidimesional to know state
} //(depends on pattern number and Led number)
buttonPushCounter++; //increment counter by one
}
}
boolean ButtonChange() { //returns HIGH if there has been a change in button state from last scan
lastButtonState = buttonState; //store latest state
buttonState = debounce(buttonPin); //update buttonState with debounced value of buttonPin
if (lastButtonState != buttonState && buttonState == HIGH) { //if there is a change in button state and it is now HIGH(pressed)
return HIGH;
}
return LOW;
}
boolean debounce(int pin) { //returns HIGH if button if HIGH for debounceDelay in miliseconds
previousState = digitalRead(pin); //store pin state
for (int i = 0; i < debounceDelay; i++) { //make sure button is pressed every milisecond debounceDelay times
delay(1);
state = digitalRead(pin);
if (state != previousState) { //reset if change in state
i = 0;
previousState = state;
}
}
return state; //return stable state
}
Don't get this:
making "int"..