Hello all,
I made a double traffic light circuit that I learned how to make (I am a beginner) using the Blink example. The coding works perfectly. However, I now want to incorporate a button into the circuit that will turn both lights to yellow, then red. Here is the code:
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState == HIGH) {
digitalWrite(yellow, HIGH);
digitalWrite(yellow2, HIGH);
digitalWrite(green, LOW);
digitalWrite(green2, LOW);
digitalWrite(red, LOW);
digitalWrite(red2, LOW);
delay(1000);
digitalWrite(yellow, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(red, HIGH);
digitalWrite(red2, HIGH);
delay(3000);
}
else {
digitalWrite(red, HIGH);
digitalWrite(yellow,LOW);
digitalWrite(green, LOW);
digitalWrite(red2, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(green2, HIGH);
delay(5000);
digitalWrite(yellow2, HIGH);
digitalWrite(green2,LOW);
delay(1000);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(green2, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(red2, HIGH);
delay(3000);
digitalWrite(green,LOW);
digitalWrite(yellow,HIGH);
delay(1000);
}
}
I use the 5v pin to supply power across my button switch and a 10k pull down resistor. However, when I push the button, the lights don't turn yellow then red as I wanted. Nothing happens except for the 'else' part continually loops. Is there a way to make it so when I push the button, the circuit will immediately go to the 'if' part instead of continually looping the 'else'?