Traffic Lights Arduino Project

Overview

Sharing my two way traffic lights intersection with pedestrian walk cycle, coded for microcontroller boards with ATmega328P chip. It offers two sets of traffic lights and a separate signals with a physical button for pedestrians. Pressing the button will queue the crossing cycle and activate as soon as the last traffic light sequence is finished with a red light. It also utilizes sound effect synchronized with the pedestrian’s lights signal – using a piezoelectric speaker. I designed and constructed DIY enclosure for college purposes. Enjoy.

Parts

Parts used in this project:

Elegoo Uno R3

5mm Diodes

Plastic LED Holder 5mm

9V battery

Battery connector clip

3 pin toggle switch

Schematic

To be added later!

Code

/* December 19, 2019 Eden Cohort 8 Year 1 Marcin Buczek
                     Traffic lights with pedestrian crossing (on request)
                     Automation logic programmed using ATMEL ATMEGA328P microcontroller */

/* Initiating variables for pedestrian crossing (requested using push button) */
int WalkRequest = 0;       // Storing state of push button from pin 2
const int WalkButton = 2;  // Pin 2 used for crossing request (5v = 1, 0v = 0)
const int RedPedLED = 6;   // Pedestrian red light 5v on pin 6
const int GreenPedLED = 7; // Pedestrian green light 5v on pin 7
const int PedSound = 4;    // Pedestrian buzzer 5v on pin 4

/* Initiating variables for traffic light set 1 */
const int Red1LED = 8;     // Station 1 red light 5v on pin 8
const int Yellow1LED = 9;  // Station 1 yellow light 5v on pin 9
const int Green1LED = 10;  // Station 1 green light 5v on pin 10

/* Initiating variables for traffic light set 2 */
const int Red2LED = 11;    // Station 2 red light 5v on pin 11
const int Yellow2LED = 12; // Station 2 red light 5v on pin 12
const int Green2LED = 13;  // Station 2 red light 5v on pin 13

/* Initiating variable buttonState for pedestrian switch status */
volatile int buttonState = 0;  

/* Setup all pins to either output (5v) or input (5v) logic */
void setup() {
  pinMode(RedPedLED, OUTPUT);  //  Sets all LED's as OUTPUT
  pinMode(GreenPedLED, OUTPUT);
  pinMode(Red1LED, OUTPUT);
  pinMode(Yellow1LED, OUTPUT);
  pinMode(Green1LED, OUTPUT);
  pinMode(Red2LED, OUTPUT);
  pinMode(Yellow2LED, OUTPUT);
  pinMode(Green2LED, OUTPUT);
  pinMode(PedSound, OUTPUT);
  pinMode(WalkButton, INPUT);  //  Sets Push Button as INPUT

/* Attach interrupt to pin 2 (interrupt number 0) - constant monitoring of pin 2 input */
/* to detect pedestrian crossing request instantly while main traffic light procedure executes */
  attachInterrupt(0, pin_ISR, CHANGE); 
  
/* Set all red lights to Red at the start of program */
  digitalWrite (Red1LED, HIGH);
  digitalWrite (Red2LED, HIGH);
  digitalWrite (RedPedLED, HIGH);
}

/* Main procedure (loop) logic alternating between two sets of traffic lights */
void loop() {
  
  delay(2500);                     // Traffic lights set 1 logic start
  digitalWrite(Yellow1LED, HIGH);  //
  delay(1500);                     //
  digitalWrite(Red1LED, LOW);      //
  digitalWrite(Yellow1LED, LOW);   //
  digitalWrite(Green1LED, HIGH);   //
  delay(15000);                    //
  digitalWrite(Green1LED, LOW);    //
  digitalWrite(Yellow1LED, HIGH);  //
  delay(3500);                     //
  digitalWrite(Yellow1LED, LOW);   //
  digitalWrite(Red1LED, HIGH);     // Traffic lights set 1 logic end

  if (WalkRequest == 1) {          // Check if crossing button was pressed
    WalkCycle();                   // If equals 1 then run WalkCycle() procedure
  }

  delay(2500);                     // Traffic lights set 2 logic start
  digitalWrite(Yellow2LED, HIGH);  //
  delay(1500);                     //
  digitalWrite(Red2LED, LOW);      //
  digitalWrite(Yellow2LED, LOW);   //
  digitalWrite(Green2LED, HIGH);   //
  delay(15000);                    //
  digitalWrite(Green2LED, LOW);    //
  digitalWrite(Yellow2LED, HIGH);  //
  delay(3500);                     //
  digitalWrite(Yellow2LED, LOW);   //
  digitalWrite(Red2LED, HIGH);     // Traffic lights set 2 logic start

  if (WalkRequest == 1) {          // Check if crossing button was pressed
    WalkCycle();                   // If equals 1 then run WalkCycle() procedure
  }
}

/* Pedestrian crossing logic procedure */
void WalkCycle() {
  
  delay(3500);                        // Pedestrian logic start
  digitalWrite (RedPedLED, LOW);      //
  digitalWrite (GreenPedLED, HIGH);   //
  
  for (int x = 0; x < 15; x++) {      // Internal loop for 15 long
  digitalWrite (PedSound, HIGH);      // buzzer sounds of 0.15s
  delay(150);                         // with
  digitalWrite (PedSound, LOW);       // 0.85s delays in between
  delay(850);                         //
  }

  digitalWrite (GreenPedLED, LOW);    //
  digitalWrite(WalkButton, LOW);      //
  delay(250);

  for (int x = 0; x < 10; x++) {      // Flashing green pedestrian light 10 times
    digitalWrite(GreenPedLED, HIGH);  //
    digitalWrite (PedSound, HIGH);    // Internal loop for 10 short
    delay(150);                       // buzzer sounds of 0.15s
    digitalWrite(GreenPedLED, LOW);   // with
    digitalWrite (PedSound, LOW);     // 0.35s delays in between
    delay(350);                       //
  }                                   //
  digitalWrite(RedPedLED, HIGH);      // Red pedestrian light back on - end of procedure
  
  WalkRequest = 0;                    // Reset switch status to 0
  asm volatile ("  jmp 0");           // Soft-reset of sketch to restart the logic with red lights
}

/* Constant reading of pedestrian crossing switch */
void pin_ISR() {
  buttonState = digitalRead(WalkButton);
  (WalkRequest = 1);  
}

/* End of code */

Demonstration