// Define pins for the traffic lights
const int redPin = 1;
const int yellowPin = 2;
const int greenPin = 3;
int button= 4;
int ledfix =13;
// Define timing variables
unsigned long previousMillis = 0;
const long intervalRed = 5000; // Red light interval (5 seconds)
const long intervalGreen = 3000; // Green light interval (3 seconds)
const long intervalYellow = 2000; // Yellow light interval (2 seconds)
// Define states for the traffic light
enum LightState {
RED,
GREEN,
YELLOW
};
LightState currentLightState = RED;
void setup() {
// Initialize the pins as outputs
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(button, INPUT );
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
switch(currentLightState) {
case RED:
// Turn on the red light
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
// Check if it's time to switch to green
if (currentMillis - previousMillis >= intervalRed) {
previousMillis = currentMillis;
currentLightState = GREEN;
}
break;
case GREEN:
// Turn on the green light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
// Check if it's time to switch to yellow
if (currentMillis - previousMillis >= intervalGreen) {
previousMillis = currentMillis;
currentLightState = YELLOW;
}
break;
case YELLOW:
// Turn on the yellow light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
// Check if it's time to switch to red
if (currentMillis - previousMillis >= intervalYellow) {
previousMillis = currentMillis;
currentLightState = RED;
}
break;
case ledfix:
if
(digitalRead(button)
button == HIGH);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(LEDFIX,HIGH);
else
digitalWrite(LEDFIX,LOW);
// Check if it's time to switch to red
if (currentMillis - previousMillis >= intervalYellow) {
previousMillis = currentMillis;
currentLightState = RED;
}
break;
}
}