I would like to control a sequence with three LED lights. The sequence being GREEN (on for 5 seconds then off), BLUE (on for 1second then off), RED (on for five seconds, then off). So a total of 11 seconds of activity then all lights are off and wait for a second button press.
When the button is held down, the sequence performs. The difficulty I am having, is creating an interrupt where when the button is depressed, the sequence I described above comes to a dead halt and all LEDs go dark.
Ive tried many iterations of this code using the online help center for the attachInterrupt command. There was another method I saw using for loops to interrupt the sequence and continually check for the button being HIGH , but attachInterrupt seemed more elegant. I'd really like to learn how to use it.
In the current version of the code below, this is the behaviour:
-All lights are OFF
-Press button Lights go through sequence between 2 and 3 times (should only be once)
*At this time further slow button presses are ignored, length of time spent holding button is ignored
*Can speed up the sequence by pressing the button multiple times very very quickly. The lights will switch from green to blue to red quickly. After the button has stopped being pressed, the sequence will perform on it's on two more times at the normal speed
int RED = 13;
int BLUE = 12;
int GREEN = 11;
const int buttonPin = 2;
volatile int buttonState = digitalRead(buttonPin);
void setup () {
// configure data communication
Serial.begin(9600); //Baud Rate
// configure hardware peripherals
pinMode(RED, OUTPUT); //Pin 13, Red LED
pinMode(BLUE, OUTPUT); //Pin 12, yellow LED
pinMode(GREEN, OUTPUT); //Pin 12, green LED
pinMode(buttonPin, INPUT); //Pin 2, buttonPin
attachInterrupt(digitalPinToInterrupt(buttonPin), pin_ISR, LOW);
}
void pin_ISR() {
buttonState = !buttonState;
}
void loop () {
if (buttonState){
digitalWrite(GREEN, HIGH);
delay(5000);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE, HIGH);
delay(1000);
digitalWrite(BLUE,LOW);
digitalWrite(RED,HIGH);
delay(5000);
digitalWrite(RED,LOW);
}
else{
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(RED, LOW);
}
}