natalmakers
Published © CC BY-SA

Arduino Pedestrian Crossing Traffic Lights

Arduino based traffic lights using car lights delay to enable safe pedestrian crossing.

BeginnerProtip22,646
Arduino Pedestrian Crossing Traffic Lights

Things used in this project

Story

Read more

Schematics

Fritzing

Code

Arduino Pedestrian Traffic Lights Code

Arduino
//leds for both traffic lights
int sinal_pedestre[2] = {A1, A0};
int sinal_carros[3] = {A2, A3, A4};
//pushbuttonpin
byte botao = 12;
//auxiliar time variables
unsigned long t_0;
unsigned long t_1;
unsigned long t_aux;
unsigned int safe_time;
//Waiting time for a request just after the traffic lights of the cars open
unsigned int t = 4000;

//matrix of the display of 7 segments
int disp[10][8] = { { 0, 1, 1, 1, 1, 1, 1, 0 }, // = Digit 0
  { 0, 1, 0, 0, 1, 0, 0, 0 }, // = Digit 1
  { 0, 0, 1, 1, 1, 1, 0, 1 }, // = Digit 2
  { 0, 1, 1, 0, 1, 1, 0, 1 }, // = Digit 3
  { 0, 1, 0, 0, 1, 0, 1, 1 }, // = Digit 4
  { 0, 1, 1, 0, 0, 1, 1, 1 }, // = Digit 5
  { 0, 1, 1, 1, 0, 1, 1, 1 }, // = Digit 6
  { 0, 1, 0, 0, 1, 1, 0, 0 }, // = Digit 7
  { 0, 1, 1, 1, 1, 1, 1, 1 }, // = Digit 8
  { 0, 1, 1, 0, 1, 1, 1, 1 }  // = Digit 9
};

void setup() {

  //PinMode of display pins
  for (int i = 2; i < 9; i++)
  {
    pinMode(i, OUTPUT);
  }
  //PinMode of leds pins
  for (int i = A0; i < A5; i++)
  {
    pinMode(i, OUTPUT);
  }
  ////PinMode of pusbutton pin
  pinMode(botao, INPUT);
  t_0 = millis();
}

void loop() {

  //Show the S of stop at the beginning
  number_display(5);

  //conting the minimum time that the traffic light will be green for the cars
  t_1 = millis();
  if (t_1 - t_0 < t) {
    safe_time = t_1 - t_0;
  }

  //green sign for cars and red for pedestrians
  digitalWrite(sinal_carros[2], HIGH);
  digitalWrite(sinal_pedestre[0], HIGH);
  //while no one makes a request, the signal will not change.
  if (digitalRead(botao) == HIGH)
  {

    delay(8000 - safe_time);

    //red light for cars goes out and yellow turns on for 2.5 seconds
    digitalWrite(sinal_carros[2], LOW);
    digitalWrite(sinal_carros[1], HIGH);

    delay(2500);

    //Pedestrians sign opens
    digitalWrite(sinal_carros[1], LOW);
    digitalWrite(sinal_carros[0], HIGH);
    digitalWrite(sinal_pedestre[0], LOW);
    digitalWrite(sinal_pedestre[1], HIGH);

    //Countdown to the pedestrian signal close
    t_0 = millis();
    t_aux = 0;
    while (t_aux < 5000) {
      idle_display();
      t_aux = millis() - t_0;
    }

    for (int k = 9; k > 0; k--) {
      number_display(k);

      delay(1000);
    }
    //the signals are turned off for the cycle to start again
    digitalWrite(sinal_carros[0], LOW);
    digitalWrite(sinal_pedestre[1], LOW);

    t_0 = millis();
  }

}
//function that shows a number in the 7 segment display

void number_display(int m) {

  for (int j = 2; j < 9; j++) {
    digitalWrite(j, disp[m][j - 1]);
  }

}

void idle_display() {
  turnOf_display();
  digitalWrite(3, 1);
  delay(150);
  digitalWrite(3, 0);
  digitalWrite(8, 1);
  delay(150);
  digitalWrite(8, 0);
  digitalWrite(6, 1);
  delay(150);
  digitalWrite(6, 0);
  delay(150);

}

void turnOf_display() {
  for (int i = 2; i < 9; i++) {
    digitalWrite(i, 0);
  }
}

Credits

natalmakers

natalmakers

0 projects • 2 followers

Comments