// Traffic Light Arduino Uno Code with Toggle Button
const int redPin = 13; // Red LED connected to digital pin 13
const int greenPin = 12; // Green LED connected to digital pin 12
const int orangePin = 11; // Orange LED connected to digital pin 11
const int toggleButtonPin = 10; // Toggle button connected to digital pin 10
int buttonState = 0;
int lastButtonState = 0;
bool trafficLightRunning = false;
int cycleCount = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(orangePin, OUTPUT);
pinMode(toggleButtonPin, INPUT);
}
void loop() {
buttonState = digitalRead(toggleButtonPin);
if (buttonState == HIGH && lastButtonState == LOW) {
trafficLightRunning = !trafficLightRunning; // Toggle the traffic light state on button press
delay(50); // Debounce delay
}
lastButtonState = buttonState;
if (trafficLightRunning) {
// Red light for 4 seconds
digitalWrite(redPin, HIGH);
delay(4000); // 4 seconds
digitalWrite(redPin, LOW);
delay(1000);
// Orange light for 1 second
digitalWrite(orangePin, HIGH);
delay(1000); // 1 second
digitalWrite(orangePin, LOW);
delay(1000);
digitalWrite(orangePin, HIGH);
delay(1000); // 1 second
digitalWrite(orangePin, LOW);
delay(1000);
// Green and Red lights together for 5 seconds
digitalWrite(greenPin, HIGH);
delay(5000); // 5 seconds
digitalWrite(greenPin, LOW);
delay(1000);
// Orange light for 1 second
digitalWrite(orangePin, HIGH);
delay(1000); // 1 second
digitalWrite(orangePin, LOW);
delay(1000);
digitalWrite(orangePin, HIGH);
delay(1000); // 1 second
digitalWrite(orangePin, LOW);
delay(1000);
// Green and Red lights together for 4 seconds
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, HIGH);
delay(4000); // 4 seconds
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
delay(1000);
cycleCount++;
if (cycleCount >= 3) {
trafficLightRunning = false; // Stop after 3 cycles
cycleCount = 0; // Reset cycle count for the next activation
}
}
}