I want to make my LED do this
Sequence 1 – Red on for 3 sec followed by Green on for 3 sec followed by Yellow on for 2 sec. Repeat.
Pressing the button will switch to sequence 2.
Sequence 2 – Red and green flashed alternately for 0.5 sec each for 5 sec followed by Yellow on for 5 sec. Stop.
This is my code but it is not working
//code
int button = 12;
int red = 10;
int yellow = 9;
int green = 8;
int val;
int val2;
int State;
int Mode = 0;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
State = digitalRead(button);
}
void loop() {
{
int State = digitalRead(button);
Serial.println(State);
}
val = digitalRead(button);
val2 = digitalRead(button);
if (val == val2) {
if (val != State) {
if (val == LOW) {
if (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 2;
} else {
if (Mode == 2) {
Mode = 3;
} else {
if (Mode == 3) {
Mode = 0;
}
}
}
}
}
}
State = val;
}
if (Mode == 0) { // all-off
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
}
if (Mode == 1) { //Red is blinks at SOS
digitalWrite(red, HIGH);
delay(3000);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(3000);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(2000);
digitalWrite(yellow, LOW);
}
if (Mode == 2) { ////Yellow is on but you need to hold for 800ms to switch to the next.
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
delay(500);
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
delay(500);
digitalWrite(yellow, HIGH);
delay(5000);
digitalWrite(yellow, LOW);
}
if (Mode == 3) { //green is on
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
}
}