0% found this document useful (0 votes)
17 views7 pages

Traffic Light

Uploaded by

home122gangotri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Traffic Light

Uploaded by

home122gangotri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Sure!

Let's modify the materials and code for a more enhanced version of
the traffic light system. In this new version, we'll simulate a simple
intersection with two traffic lights (for north-south and east-west
directions) and add a pedestrian button that can trigger a pedestrian
signal when pressed.

Modified Materials:

1. Arduino Uno (or compatible board)

2. 6 LEDs (Red, Yellow, Green for both directions — North-South and


East-West)

3. 6 220-ohm Resistors (for current limiting)

4. 1 Pushbutton (for pedestrian signal)

5. 1 10k-ohm Resistor (for button pull-down)

6. Breadboard

7. Jumper Wires

8. Ultrasonic sensor

9. The LCD (Liquid Crystal Display)

Circuit Setup:

1. North-South Traffic Light (3 LEDs):

o Red LED to pin 13

o Yellow LED to pin 12

o Green LED to pin 11

2. East-West Traffic Light (3 LEDs):

o Red LED to pin 10

o Yellow LED to pin 9

o Green LED to pin 8

3. Pedestrian Button:

o One leg of the pushbutton connects to pin 7 (digital input).

o The other leg connects to GND, and you can use a 10k-ohm
pull-down resistor between the button pin and GND to
ensure the pin reads LOW when the button is not pressed.

4. Grounding:
o Connect the negative terminals (cathodes) of all LEDs to GND
through 220-ohm resistors.

o Connect the GND rail of the breadboard to the GND pin on the
Arduino.

Modified Arduino Code:

In this modified code, we simulate two sets of traffic lights, and we


introduce the pedestrian light, which can be triggered by pressing the
pedestrian button.

cpp

Copy code

// Define the pins for the North-South and East-West traffic lights

int redNS = 13; // North-South Red LED

int yellowNS = 12; // North-South Yellow LED

int greenNS = 11; // North-South Green LED

int redEW = 10; // East-West Red LED

int yellowEW = 9; // East-West Yellow LED

int greenEW = 8; // East-West Green LED

// Define the pin for the pedestrian button

int buttonPin = 7; // Pedestrian button (digital input)

// Define state variables

bool pedestrianRequest = false; // Pedestrian button state

void setup() {

// Set the traffic light pins as OUTPUT

pinMode(redNS, OUTPUT);

pinMode(yellowNS, OUTPUT);

pinMode(greenNS, OUTPUT);
pinMode(redEW, OUTPUT);

pinMode(yellowEW, OUTPUT);

pinMode(greenEW, OUTPUT);

// Set the button pin as INPUT

pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor

// Initial state: All lights off

digitalWrite(redNS, LOW);

digitalWrite(yellowNS, LOW);

digitalWrite(greenNS, LOW);

digitalWrite(redEW, LOW);

digitalWrite(yellowEW, LOW);

digitalWrite(greenEW, LOW);

void loop() {

// Check the pedestrian button (active LOW)

if (digitalRead(buttonPin) == LOW) {

pedestrianRequest = true;

delay(200); // Debounce delay

// If the pedestrian button is pressed, show pedestrian light

if (pedestrianRequest) {

pedestrianWalk();

} else {

// Normal traffic light sequence

trafficLightCycle();
}

// Function to simulate the traffic light cycle

void trafficLightCycle() {

// North-South traffic light: Green -> Yellow -> Red

digitalWrite(greenNS, HIGH);

digitalWrite(redNS, LOW);

digitalWrite(yellowNS, LOW);

delay(5000); // Green for 5 seconds

digitalWrite(greenNS, LOW);

digitalWrite(yellowNS, HIGH);

delay(2000); // Yellow for 2 seconds

digitalWrite(yellowNS, LOW);

digitalWrite(redNS, HIGH);

// East-West traffic light: Green -> Yellow -> Red

digitalWrite(greenEW, HIGH);

digitalWrite(redEW, LOW);

digitalWrite(yellowEW, LOW);

delay(5000); // Green for 5 seconds

digitalWrite(greenEW, LOW);

digitalWrite(yellowEW, HIGH);

delay(2000); // Yellow for 2 seconds

digitalWrite(yellowEW, LOW);
digitalWrite(redEW, HIGH);

// Function to simulate pedestrian light when button is pressed

void pedestrianWalk() {

// Turn off traffic lights and activate pedestrian signals

digitalWrite(redNS, HIGH);

digitalWrite(redEW, HIGH);

// Show walk signal (green for pedestrian)

digitalWrite(greenNS, LOW);

digitalWrite(greenEW, LOW);

// Pause for pedestrian crossing (5 seconds)

delay(5000);

// After pedestrian light, go back to normal traffic light sequence

pedestrianRequest = false;

Code Breakdown:

1. Traffic Light Pins:

o redNS, yellowNS, greenNS control the North-South traffic light.

o redEW, yellowEW, greenEW control the East-West traffic light.

2. Pedestrian Button:

o buttonPin is used for the pedestrian button. When pressed, it


triggers the pedestrian light.

3. Traffic Light Cycle:

o The trafficLightCycle() function handles the switching of traffic


lights in a regular pattern (5 seconds for green, 2 seconds for
yellow, and red otherwise).
4. Pedestrian Light:

o If the pedestrian button is pressed, the traffic lights turn red


for both directions, and the system simulates the pedestrian
crossing with a 5-second walk signal.

5. Button Debouncing:

o A small delay of 200 milliseconds is added to avoid the button


being read multiple times too quickly (debouncing).

6. Pedestrian Request:

o If the button is pressed, the pedestrian light is activated, and


the normal traffic light cycle is paused until the pedestrian
light cycle is complete.

Testing:

Once you upload this modified code to the Arduino, the system will
behave as follows:

1. Normal Operation:

o North-South and East-West traffic lights alternate, following


the standard sequence.

2. Pedestrian Request:

o When the pedestrian button is pressed, both traffic lights turn


red, and the pedestrian light is activated for 5 seconds.

o After that, the system resumes the regular traffic light cycle.

Optional Enhancements:

 More Complex Pedestrian Timing: Add a timer to give a walk


signal and then a "don’t walk" signal.

 Sensor-activated Lights: Add sensors (e.g., IR or ultrasonic) to


detect vehicles waiting at a light.

 Sound Indicator for Pedestrians: Add a buzzer that sounds when


the pedestrian light is on.

This modified setup makes the traffic light system more interactive and
realistic by simulating pedestrian crossings and handling both directions of
traffic.

4o mini

ChatGPT can make mis

You might also like