const int resetPin = 2; // Button connected to pin 2 (for manual reset)
// Define the LED pins for the traffic lights
const int greenPin_NordSouth = 2; // Green light
const int orangePin_NordSouth = 3; // Orange light
const int redPin_NordSouth = 4; // Red light
const int greenPin_EstWest = 5; // Green light
const int orangePin_EstWest = 6; // Orange light
const int redPin_EstWest = 7; // Red light
// Time durations for each light in milliseconds
const unsigned long greenDuration = 3000; // 160 seconds in milliseconds (180,000 ms)
const unsigned long orangeDuration = 1000; // 20 seconds in milliseconds (20,000 ms)
const unsigned long redDuration1 = 3001; // 160 seconds in milliseconds (124,000 ms)
const unsigned long redDuration2 = 1001; // 20 seconds in milliseconds (124,000 ms)
// Timing counter
unsigned long previousMillis = 0;
unsigned long lightCycleDuration = 0; // This variable will control the timing for each light
void setup() {
// Set the LED pins as outputs
pinMode(greenPin_NordSouth, OUTPUT);
pinMode(orangePin_NordSouth, OUTPUT);
pinMode(redPin_NordSouth, OUTPUT);
pinMode(greenPin_EstWest, OUTPUT);
pinMode(orangePin_EstWest, OUTPUT);
pinMode(redPin_EstWest, OUTPUT);
// Start with the green light on
digitalWrite(greenPin_NordSouth, HIGH); // Turn on Green light
digitalWrite(orangePin_NordSouth, LOW); // Turn off Orange light
digitalWrite(redPin_NordSouth, LOW); // Turn off Red light
digitalWrite(greenPin_EstWest, HIGH); // Turn on Green light
digitalWrite(orangePin_EstWest, LOW); // Turn off Orange light
digitalWrite(redPin_EstWest, LOW); // Turn off Red light
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time in milliseconds
// If the current time exceeds the cycle duration, change the traffic light
if (currentMillis - previousMillis >= lightCycleDuration) {
previousMillis = currentMillis; // Reset the timer for the next light change
// Cycle through the lights in the following order:
// 1. Green Light Duration (3 minutes)
if (lightCycleDuration == 0) {
digitalWrite(greenPin_NordSouth, HIGH); // Turn on Green light
digitalWrite(orangePin_NordSouth, LOW); // Turn off Orange light
digitalWrite(redPin_NordSouth, LOW); // Turn off Red light
digitalWrite(greenPin_EstWest, LOW); // Turn off Green light
digitalWrite(orangePin_EstWest, LOW); // Turn off Orange light
digitalWrite(redPin_EstWest, HIGH); // Turn on Red light
lightCycleDuration = greenDuration; // Set the duration for Green light
}
// 2. Orange Light Duration (20 seconds)
else if (lightCycleDuration == greenDuration) {
digitalWrite(greenPin_NordSouth, LOW); // Turn off Green light
digitalWrite(orangePin_NordSouth, HIGH); // Turn on Orange light
digitalWrite(redPin_NordSouth, LOW); // Turn off Red light
digitalWrite(greenPin_EstWest, LOW); // Turn off Green light
digitalWrite(orangePin_EstWest, LOW); // Turn off Orange light
digitalWrite(redPin_EstWest, HIGH); // Turn on Red light
lightCycleDuration = orangeDuration; // Set the duration for Orange light
}
// 3. Red Light Duration (2 minutes and 4 seconds)
else if (lightCycleDuration == orangeDuration) {
digitalWrite(greenPin_NordSouth, LOW); // Turn off Green light
digitalWrite(orangePin_NordSouth, LOW); // Turn off Orange light
digitalWrite(redPin_NordSouth, HIGH); // Turn on Red light
digitalWrite(greenPin_EstWest, HIGH); // Turn on Green light
digitalWrite(orangePin_EstWest, LOW); // Turn off Orange light
digitalWrite(redPin_EstWest, LOW); // Turn off Red light
lightCycleDuration = redDuration1; // Set the duration for Red light
}
// 4.
else if (lightCycleDuration == redDuration1) {
digitalWrite(greenPin_NordSouth, LOW); // Turn off Green light
digitalWrite(orangePin_NordSouth, LOW); // Turn off Orange light
digitalWrite(redPin_NordSouth, HIGH); // Turn on Red light
digitalWrite(greenPin_EstWest, LOW); // Turn off Green light
digitalWrite(orangePin_EstWest, HIGH); // Turn on Orange light
digitalWrite(redPin_EstWest, LOW); // Turn off Red light
lightCycleDuration = redDuration2; // Set the duration for Red light
}
// After Red light duration, reset the cycle to start again with Green light
else if (lightCycleDuration == redDuration2) {
lightCycleDuration = 0; // Reset the cycle to start with Green light
}
}
}