Hi, I am new to programming Arduinos and a workmate is wanting to build a working Traffic Light display, I made a simple list of codes that run different lighting sequences, and they follow through to the next. Modified an existing piece of code from online.
Currently I have a set of codes for running a single Traffic Light.
Green => Yellow => Red for 3 cycles, then
Flashing Yellow for 6 cycles (just used Yellow High/Low 6 times), back to Green, Yellow, Red, then
Flashing Red for 6 cycles, then back to the top, running a normal Traffic Light sequence.
So instead of repeating the lines multiple times, can I use a countdown for each?
Included is my attempt at coding.
int Red = 12;
int Yellow = 11;
int Green = 10;
void setup() {
// put your setup code here, to run once:
pinMode(Red, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
changelights();
delay(30000);
}
void changelights(){
//Turn Green On for 7secs
digitalWrite(Red, HIGH);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, LOW);
delay(7000);
//green off, yellow on for 3 secs
digitalWrite(Green, HIGH);
digitalWrite(Yellow, LOW);
digitalWrite(Red, HIGH);
delay(3000);
//turn off yellow, then turn red on for 5secs
digitalWrite(Yellow, HIGH);
digitalWrite(Red, LOW);
delay(7000);
//red and yellow on for 2secs (red is already on though)
//digitalWrite(Yellow, HIGH);
//delay(2000);
//turn red and yellow off, then turn on green
digitalWrite(Yellow, HIGH);
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
delay(7000);
//green off, yellow on for 3 secs
digitalWrite(Green, HIGH);
digitalWrite(Yellow, LOW);
digitalWrite(Red, HIGH);
delay(3000);
//turn off yellow, then turn red on for 5secs
digitalWrite(Yellow, HIGH);
digitalWrite(Red, LOW);
delay(7000);
//red and yellow on for 2secs (red is already on though)
//digitalWrite(Yellow, HIGH);
//delay(2000);
//turn red and yellow off, then turn on green
digitalWrite(Yellow, HIGH);
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
delay(3000);
//Turn Yellow on for Faulty Light
digitalWrite(Green, HIGH);
digitalWrite(Yellow, LOW);
delay(500);
digitalWrite(Yellow,HIGH);
delay(500);
digitalWrite(Yellow, LOW);
delay(500);
digitalWrite(Yellow,HIGH);
delay(500);
digitalWrite(Yellow, LOW);
delay(500);
digitalWrite(Yellow,HIGH);
delay(500);
digitalWrite(Yellow, LOW);
delay(500);
digitalWrite(Yellow,HIGH);
delay(500);
digitalWrite(Yellow, LOW);
delay(500);
digitalWrite(Yellow,HIGH);
delay(500);
digitalWrite(Yellow, LOW);
delay(500);
digitalWrite(Yellow,HIGH);
delay(500);
digitalWrite(Red, LOW);
delay(7000);
//Turn Green On for 7secs
digitalWrite(Red, HIGH);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, LOW);
delay(7000);
//green off, yellow on for 3 secs
digitalWrite(Green, HIGH);
digitalWrite(Yellow, LOW);
digitalWrite(Red, HIGH);
delay(3000);
//turn off yellow, then turn red on for 5secs
digitalWrite(Yellow, HIGH);
digitalWrite(Red, LOW);
delay(7000);
//red and yellow on for 2secs (red is already on though)
//digitalWrite(Yellow, HIGH);
//delay(2000);
//turn red and yellow off, then turn on green
digitalWrite(Yellow, HIGH);
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
delay(7000);
//green off, yellow on for 3 secs
digitalWrite(Green, HIGH);
digitalWrite(Yellow, LOW);
digitalWrite(Red, HIGH);
delay(3000);
//turn off yellow, then turn red on for 5secs
digitalWrite(Yellow, HIGH);
digitalWrite(Red, LOW);
delay(7000);
//red and yellow on for 2secs (red is already on though)
//digitalWrite(Yellow, HIGH);
//delay(2000);
//turn red and yellow off, then turn on green
digitalWrite(Yellow, HIGH);
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
delay(3000);
//Turn Red on for Hazard, Stop
digitalWrite(Green, HIGH);
digitalWrite(Red, LOW);
delay(500);
digitalWrite(Red,HIGH);
delay(500);
digitalWrite(Red, LOW);
delay(500);
digitalWrite(Red,HIGH);
delay(500);
digitalWrite(Red, LOW);
delay(500);
digitalWrite(Red,HIGH);
delay(500);
digitalWrite(Red, LOW);
delay(500);
digitalWrite(Red,HIGH);
delay(500);
digitalWrite(Red, LOW);
delay(500);
digitalWrite(Red,HIGH);
delay(500);
digitalWrite(Red, LOW);
delay(500);
digitalWrite(Red,HIGH);
delay(500);
digitalWrite(Red, LOW);
}
Thanks for any help. Kevin