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

Interactive Traffic Lights

This document describes an Arduino sketch for an interactive traffic light system with pedestrian crossing. It defines pin assignments for the car and pedestrian lights, a button input, and timing variables. The setup function initializes the pin modes and turns on the initial green car light and red pedestrian light. The main loop checks if the button is pressed and enough time has passed since the last change, calling the changeLights function. This function sequentially changes the car and pedestrian lights, with delays to match light sequences and crossing times.

Uploaded by

Jamesdomingo
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)
54 views

Interactive Traffic Lights

This document describes an Arduino sketch for an interactive traffic light system with pedestrian crossing. It defines pin assignments for the car and pedestrian lights, a button input, and timing variables. The setup function initializes the pin modes and turns on the initial green car light and red pedestrian light. The main loop checks if the button is pressed and enough time has passed since the last change, calling the changeLights function. This function sequentially changes the car and pedestrian lights, with delays to match light sequences and crossing times.

Uploaded by

Jamesdomingo
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/ 3

// Interactive Traffic Lights

int carRed = 12; // To assign the car lights


int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // To assign the pedestrian lights
int pedGreen = 8;
int button = 2; // Tactile button pin
int crossTime = 5000; // time allowed to cross

unsigned long changeTime; // time since button pressed


void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}
void loop() {
int state = digitalRead(button);
/* check if button is pressed and it is
over 5 seconds since last button press */
if (state == HIGH && (millis() - changeTime) > 5000) {
// Call the function to change the lights
changeLights();
}

}
void changeLights() {
digitalWrite(carGreen, LOW); // green of
digitalWrite(carYellow, HIGH); // yellow on
delay(2000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow of
digitalWrite(carRed, HIGH); // red on
delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red of
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
// turn ped red on
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(carYellow, HIGH); // Yellow will switch on
digitalWrite(carRed, LOW); // red will switch of
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // Yellow will switch of

// record the time since last change of lights


changeTime = millis();

// Retun / Loop
}

You might also like