#define RED_PIN_1 12
#define GREEN_PIN_1 11
#define YELLOW_PIN_1 10
#define RED_PIN_2 7
#define GREEN_PIN_2 6
#define YELLOW_PIN_2 5
#define RED_DELAY 5000
#define GREEN_DELAY 4000
#define YELLOW_DELAY 1000
// Setting up the parameters for each millisecond delay
unsigned long previousMillis_1 = 0;
unsigned long previousMillis_2 = 0;
unsigned long previousMillis_3 = 0;
void setup() {
// Setting up output pins for Traffic light 1
pinMode(RED_PIN_1, OUTPUT);
pinMode(GREEN_PIN_1, OUTPUT);
pinMode(YELLOW_PIN_1, OUTPUT);
// Setting up output pins for Traffic light 2
pinMode(RED_PIN_2, OUTPUT);
pinMode(GREEN_PIN_2, OUTPUT);
pinMode(YELLOW_PIN_2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis(); // Reads time from the start of program in milliseconds
// ### Default scenario with no switches pressed ###
// First interval
digitalWrite(RED_PIN_1, HIGH); // Turn on first red light
digitalWrite(GREEN_PIN_2, HIGH); // Turn on second green light
delay(GREEN_DELAY); // Wait for green light to turn off
// Second interval
digitalWrite(GREEN_PIN_2, LOW); // Turn off second green light
digitalWrite(YELLOW_PIN_2, HIGH); // Turn on second yellow light
delay(YELLOW_DELAY); // Wait for yellow light to turn off
// Third interval
digitalWrite(YELLOW_PIN_2, LOW); // Turn off second yellow light
digitalWrite(RED_PIN_2, HIGH); // Turn on second red light
digitalWrite(RED_PIN_1, LOW); // Turn off second red light
digitalWrite(GREEN_PIN_1, HIGH); // Turn on first green light
delay(GREEN_DELAY); // Wait for green light to turn off
// Fourth interval
digitalWrite(GREEN_PIN_1, LOW); // Turn off first green light
digitalWrite(YELLOW_PIN_1, HIGH); // Turn on first yellow light
delay(YELLOW_DELAY); // Wait for yellow light to turn off
digitalWrite(YELLOW_PIN_1, LOW); // Turn off first yellow light
digitalWrite(RED_PIN_2, LOW); // Turn off second red light
// #################################################
// ### Code Example for millis() ###
// if (currentMillis - previousMillis >= interval) {
// previousMillis = currentMillis;
//}
}
void changeLights(int RED1, int RED2, int YELLOW1, int YELLOW2, int GREEN1, int GREEN2) {
// Function for RESET each of the traffic lights upon switch press
// For the first interval
digitalWrite(GREEN2, LOW); // Turn off opposite green light
digitalWrite(YELLOW2, HIGH); // Turn on opposite yellow light
digitalWrite(RED1, HIGH); // Turn on adjacent red light
delay(YELLOW_DELAY); // Set the time for opposite yellow light
// For the second interval
digitalWrite(RED1, LOW); // Turn off adjacent red light
digitalWrite(GREEN1, HIGH); // Turn on adjacent green light
digitalWrite(RED2, HIGH); // Turn on opposite red light
delay(GREEN_DELAY); // Set the time for adjacent green light
// For the third interval
digitalWrite(GREEN1, LOW); // Turn off adjacent green light
digitalWrite(YELLOW1, HIGH); // Turn on adjacent yellow light
delay(YELLOW_DELAY); // Set the time for adjacent yellow light
// For the fourth and final interval
digitalWrite(RED1, HIGH); // Turn on adjacent red light
digitalWrite(RED2, LOW); // Turn off opposite red light
digitalWrite(GREEN2, HIGH); // Turn on opposite green light
}