Right so I cleaned up your program and added three functions one light (Lighter(boolean, boolean, boolean), one to detect the button has been pressed (ButtonChange()) and the last is simply a debounce function. I used a switch case stament to select LED pattern, though not the nicest or shortest it is way faster than using an array.
//LED Pins
const int greenLED = 6;
const int yellowLED = 10;
const int redLED = 11;
//Button Pin
const int buttonPin = 9;
//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
int numberofPatterns = 7; //number of patterns indexed to whatever cases are (this case 0 indexed, 0 to 7)
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
}
void loop () {
if (ButtonChange() == HIGH) { //if button has been pressed
if (buttonPushCounter > numberofPatterns) { //if counter exeeds max reset to index(0)
buttonPushCounter = 0;
}
switch (buttonPushCounter) { //depending on counter value choose a case
case 0:
Lighter(0,0,0);
break;
case 1:
Lighter(1,0,0);
break;
case 2:
Lighter(1,1,0);
break;
case 3:
Lighter(1,1,1);
break;
case 4:
Lighter(0,1,1);
break;
case 5:
Lighter(0,1,0);
break;
case 6:
Lighter(0,0,1);
break;
case 7:
Lighter(1,0,1);
break;
}
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;
}
void Lighter(boolean redState, boolean yellowState, boolean greenState) {//lights the threeLEDs to the values redState, yellowState and greenState
digitalWrite (redLED, redState);
digitalWrite (yellowLED, yellowState);
digitalWrite (greenLED, greenState);
}
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
}
I commented everything to be sure you get it cause I'm not sure of your programing level. I've compiled this and tested it so I'm sure it works. Hope I help.
Keep making!!