Hey guys, I need help. I am a beginner and I am trying to create a 3-way traffic light with 1 pedestrian crossing that can interrupt the loop of the traffic lights with a button. I have tried to create a debounce thing but the button does not work. I have found a long way around needing to loop the code so ill send both codes, one for the traffic lights only (it works)
int r1 = 1;
int y1 = 2;
int g1 = 3;
int r2 = 4;
int y2 = 5;
int g2 = 6;
int r3 = 7;
int y3 = 8;
int g3 = 9;
void setup() {
pinMode (r1, OUTPUT);
pinMode (y1, OUTPUT);
pinMode (g1, OUTPUT);
pinMode (r2, OUTPUT);
pinMode (y2, OUTPUT);
pinMode (g2, OUTPUT);
pinMode (r3, OUTPUT);
pinMode (y3, OUTPUT);
pinMode (g3, OUTPUT);
}
void loop() {
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(750);
digitalWrite(g1, HIGH);
digitalWrite(r1, LOW);
delay(5000);
digitalWrite(r1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, HIGH);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(750);
digitalWrite(g2, HIGH);
digitalWrite(r2, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, HIGH);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(750);
digitalWrite(g3, HIGH);
digitalWrite(r3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, LOW);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, HIGH);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
}
and the code for the pedestrian crossing (doesn't work)
int r1 = 1;
int y1 = 2;
int g1 = 3;
int r2 = 4;
int y2 = 5;
int g2 = 6;
int r3 = 7;
int y3 = 8;
int g3 = 9;
int button = 13;
int pedred = 10;
int pedgreen = 11;
void setup() {
pinMode (r1, OUTPUT);
pinMode (y1, OUTPUT);
pinMode (g1, OUTPUT);
pinMode (r2, OUTPUT);
pinMode (y2, OUTPUT);
pinMode (g2, OUTPUT);
pinMode (r3, OUTPUT);
pinMode (y3, OUTPUT);
pinMode (g3, OUTPUT);
pinMode (button, INPUT);
pinMode (pedred, OUTPUT);
pinMode (pedgreen, OUTPUT);
}
void loop() {
if (digitalRead(button) == HIGH) {
delay(15); // software debounce
if (digitalRead(button) == HIGH) {
// if the switch is HIGH, ie. pushed down - change the lights!
changeLights();
}
}
else
{
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(pedred, HIGH);
digitalWrite(pedgreen, LOW);
delay(750);
digitalWrite(g1, HIGH);
digitalWrite(r1, LOW);
delay(5000);
digitalWrite(r1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, HIGH);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(750);
digitalWrite(g2, HIGH);
digitalWrite(r2, LOW);
delay(5000);
}
}
void changeLights() {
delay(3000);
digitalWrite(r1, LOW);
digitalWrite(y1, HIGH);
digitalWrite(g1, LOW);
digitalWrite(r2, LOW);
digitalWrite(y2, HIGH);
digitalWrite(g2, LOW);
digitalWrite(r3, LOW);
digitalWrite(y3, HIGH);
digitalWrite(g3, LOW);
digitalWrite(pedred, HIGH);
digitalWrite(pedgreen, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(y1, LOW);
digitalWrite(g1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(y2, LOW);
digitalWrite(g2, LOW);
digitalWrite(r3, HIGH);
digitalWrite(y3, LOW);
digitalWrite(g3, LOW);
digitalWrite(pedred, LOW);
digitalWrite(pedgreen, HIGH);
delay(6500);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(pedred, HIGH);
digitalWrite(pedgreen, LOW);
delay(750);
digitalWrite(g1, HIGH);
digitalWrite(r1, LOW);
delay(5000);
digitalWrite(r1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, HIGH);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(750);
digitalWrite(g2, HIGH);
digitalWrite(r2, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, HIGH);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(750);
digitalWrite(g3, HIGH);
digitalWrite(r3, LOW);
delay(5000);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, LOW);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(y3, HIGH);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
delay(5000);
}
the code for the pedestrian crossing is much longer as I just copy and paste the code over and over to avoid the loop but it wouldn't let me add all of it in this forum.