#define RED_LED_PIN 17
#define YELLOW_LED_PIN 5
#define GREEN_LED_PIN 18
#define BUTTON_PIN 19
// State variables
enum TrafficLightState { RED, YELLOW, GREEN, GREEN_MANUAL };
TrafficLightState currentState = RED;
unsigned long previousMillis = 0;
const long redInterval = 5000; // 5 seconds for Red
const long yellowInterval = 4000; // 4 seconds for Yellow
const long greenInterval = 2000; // 2 seconds for Green
const long greenManualInterval = 15000; // 15 seconds for Green when button is pressed
unsigned long greenManualStartMillis = 0; // To track the start time of Green in manual mode
bool buttonPressed = false; // Flag to track button state
bool greenLedState = false; // Flag to track the Green LED state (on or off)
void setup() {
// Initialize LED pins as output
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
// Initialize button pin as input with pullup resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Start with Red LED on
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
void loop() {
// Read the button state
int buttonState = digitalRead(BUTTON_PIN);
// If the button is pressed (active LOW)
if (buttonState == LOW && !buttonPressed) {
buttonPressed = true; // Mark the button as pressed
// Toggle the Green LED
if (greenLedState) {
// Turn off Green LED if it was on
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW); // Turn off Green LED
currentState = RED; // Return to RED state
greenLedState = false; // Set Green LED state to off
} else {
// Turn on Green LED for 15 seconds
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on Green LED
currentState = GREEN_MANUAL; // Set to manual Green mode
greenManualStartMillis = millis(); // Start timer for 15 seconds
greenLedState = true; // Set Green LED state to on
}
delay(200); // Debounce delay to avoid multiple button presses
}
// If the button is released, reset the button pressed flag
if (buttonState == HIGH) {
buttonPressed = false;
}
// If in manual Green mode, check if 15 seconds have passed
if (currentState == GREEN_MANUAL) {
unsigned long currentMillis = millis();
if (currentMillis - greenManualStartMillis >= greenManualInterval) {
// After 15 seconds, turn off the Green LED and return to automatic cycling
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW); // Turn off Green LED
currentState = RED;
greenLedState = false; // Set Green LED state to off
}
}
// Cycle through the lights automatically if not in manual Green mode
else {
// Check the time passed to manage the automatic cycle
unsigned long currentMillis = millis();
if (currentState == RED && currentMillis - previousMillis >= redInterval) {
// Time to change from RED to YELLOW
previousMillis = currentMillis;
currentState = YELLOW;
}
else if (currentState == YELLOW && currentMillis - previousMillis >= yellowInterval) {
// Time to change from YELLOW to GREEN
previousMillis = currentMillis;
currentState = GREEN;
}
else if (currentState == GREEN && currentMillis - previousMillis >= greenInterval) {
// Time to change from GREEN to RED
previousMillis = currentMillis;
currentState = RED;
}
// Update the traffic light based on the current state
updateTrafficLight();
}
}
void updateTrafficLight() {
// Turn off all LEDs first
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
// Set the appropriate LED based on the current state
if (currentState == RED) {
digitalWrite(RED_LED_PIN, HIGH);
} else if (currentState == YELLOW) {
digitalWrite(YELLOW_LED_PIN, HIGH);
} else if (currentState == GREEN) {
digitalWrite(GREEN_LED_PIN, HIGH);
}
}