0% found this document useful (0 votes)
4 views

arduino project code

This document contains an Arduino sketch for a traffic signal system that includes red, yellow, and green LEDs. It uses a button to switch the signal to red for pedestrian crossing when pressed, otherwise it cycles through the normal traffic light sequence. The setup function initializes the pins, and the loop function manages the LED states based on button input.

Uploaded by

husseinimhmd100
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

arduino project code

This document contains an Arduino sketch for a traffic signal system that includes red, yellow, and green LEDs. It uses a button to switch the signal to red for pedestrian crossing when pressed, otherwise it cycles through the normal traffic light sequence. The setup function initializes the pins, and the loop function manages the LED states based on button input.

Uploaded by

husseinimhmd100
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

const int redLed = 2;

const int yellowLed = 3;


const int greenLed = 4;
const int buttonPin = 5;

bool buttonPressed = false;

void setup() {
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
}

void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
buttonPressed = true;
}

if (buttonPressed) {
// Turn the traffic signal red for pedestrian crossing
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);

delay(5000); // Keep red for 5 seconds

buttonPressed = false; // Reset the button state


} else {
// Normal traffic signal operation
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, HIGH);
delay(5000); // Green for 5 seconds

digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, HIGH);
delay(2000); // Yellow for 2 seconds

digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
delay(5000); // Red for 5 seconds
}
}

You might also like