Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
lmsousa
Published © GPL3+

Traffic Lights Controlled by Arduino

Traffic lights system for a two-street crossing with pedestrian lights, controlled by Arduino.

IntermediateShowcase (no instructions)14,558
Traffic Lights Controlled by Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
12 traffic 5mm LED (4 Red, 4 Yellow, 4 Green ) + 16 pedestrian lights 3mm LED (8 Red, 8 Green) See main picture if numbers dont sound right
×28

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

LED Circuit Diagram

DB 25 pin socket mounted on the frame. A cable is needed to connect to Arduino

Code

Semaforos_VAP.ino

Arduino
//Semaforos 2 Vias
//Projecto Integrado ENIDH CET EAN 2013/2014
//Luis Sousa
//Pedro Mealha
//Emanuel Carpinteiro

// Variaveis

int RED1 = 0;
int YEL1 = 1;
int GRE1 = 2;
int RED2 = 3;
int YEL2 = 4;
int GRE2 = 5;

int PRED1 = 6;
int PGRE1 = 7;
int PRED2 = 8;
int PGRE2 = 9;

int PREQ1=10;
int PREQ2=11;

int BT1=HIGH; //Botao 1
int BT2=HIGH; //Botao 2
int FLASH=HIGH; //Amarelos Intermitentes 
int TEST=HIGH; //Funo de teste de LEDs

int tgre = 12000; // 12s
int tyel = 2000;  // 2s
int tred = 12000; // 12s

int mem0 = LOW;
int mem1 = LOW;
int mem2 = LOW;
int mem3 = LOW;
int mem4 = LOW;
int mem5 = LOW;
int mem6 = LOW;
int mem7 = LOW;
int mem8 = LOW;
int mem9 = LOW;

// basic functions
void setup()
{
  pinMode(RED1, OUTPUT);
  pinMode(YEL1, OUTPUT);
  pinMode(GRE1, OUTPUT);
  pinMode(RED2, OUTPUT);
  pinMode(YEL2, OUTPUT);
  pinMode(GRE2, OUTPUT);
  pinMode(PRED1, OUTPUT);
  pinMode(PGRE1, OUTPUT);
  pinMode(PRED2, OUTPUT);
  pinMode(PGRE2, OUTPUT);

  pinMode(PREQ1, INPUT);
  pinMode(PREQ2, INPUT);

  test1(); // Sequencia de teste de LEDs
//test2();
  blinkYellow(); //Amarelo intermitente
}

void loop()
{
  //Tempo 1
  //Via 1 com vermelho
  digitalWrite(RED1, HIGH);
  digitalWrite(YEL1, LOW);
  digitalWrite(GRE1, LOW);
  delay(1000);//Delay de segurana;
  //peoes Via 1 verde,
  digitalWrite(PRED1, LOW);
  digitalWrite(PGRE1, HIGH); 
  //peoes Via 2 vermelho 
  digitalWrite(PRED2, HIGH);
  digitalWrite(PGRE2, LOW);
  //delay(500);//Delay de segurana;  
  //Via 2 com verde  
  digitalWrite(RED2, LOW);
  digitalWrite(YEL2, LOW);
  digitalWrite(GRE2, HIGH);

  //espera tempo do vermelho
  //delay(tred);
  for (int i=0; i <= (tred/100); i++)
  {
    BT1=digitalRead(PREQ1);
    BT2=digitalRead(PREQ2);
    BT1=!BT1;
    BT2=!BT2;
    FLASH=BT1&&BT2;
    if (FLASH) 
    {
      blinkYellow();
      BT1=(LOW);
    } 
    if (BT1) 
    {
      i=(tred/100);
    }
    delay(100);
  } 
  //pisca verde peoes
  for (int i=0; i <= 5; i++)
  { 
    digitalWrite (PGRE1,LOW);
    delay (500);
    digitalWrite (PGRE1,HIGH);
    delay (500);
  }

  //Tempo 2
  //Via 2 com amarelo  
  // digitalWrite(RED1, HIGH);
  // digitalWrite(YEL1, LOW);
  // digitalWrite(GRE1, LOW);
  digitalWrite(PGRE1, LOW);
  digitalWrite(PRED1, HIGH);
  //delay (500);  
  //digitalWrite(RED2, LOW);
  digitalWrite(YEL2, HIGH);
  digitalWrite(GRE2, LOW);
  //digitalWrite(PRED2, HIGH);
  //digitalWrite(PGRE2, LOW);
  delay (tyel);

  //Tempo 3
  //Via 2 com vermelho  
  digitalWrite(RED2, HIGH);
  digitalWrite(YEL2, LOW);
  //digitalWrite(GRE2, LOW);
  delay(1000);//Delay de segurana;
  //peoes Via 2 verde,  
  digitalWrite(PRED2, LOW);
  digitalWrite(PGRE2, HIGH);
  //peoes Via 1 vermelho   
  //digitalWrite(PRED1, HIGH);
  //digitalWrite(PGRE1, LOW);
  //delay(500);//Delay de segurana; 
  //Via 1 com verde 
  digitalWrite(RED1, LOW);
  //digitalWrite(YEL1, LOW);
  digitalWrite(GRE1, HIGH);

  //espera tempo do verde  
  //delay (tgre);
  for (int i=0; i <= (tgre/100); i++)
  {
    BT1=digitalRead(PREQ1);
    BT2=digitalRead(PREQ2);
    BT1=!BT1;
    BT2=!BT2;
    FLASH=BT1&&BT2;
    if (FLASH) 
    {
      blinkYellow();
      BT2=LOW;
    }    
    if (BT2) 
    {
      i=(tgre/100);
    }
    delay(100);
  }

  //pisca verde peoes
  for (int i=0; i <= 5; i++)
  { 
    digitalWrite (PGRE2,LOW);
    delay (500);
    digitalWrite (PGRE2,HIGH);
    delay (500);
  }

  //Tempo 4
  //Via 1 com amarelo  
  //  digitalWrite(RED2, HIGH);
  //  digitalWrite(YEL2, LOW);
  //  digitalWrite(GRE2, LOW);
  digitalWrite(PGRE2, LOW);
  digitalWrite(PRED2, HIGH);
  //delay (500);
  //digitalWrite(RED1, LOW);
  digitalWrite(YEL1, HIGH);
  digitalWrite(GRE1, LOW);
  //digitalWrite(PRED1, HIGH);
  //digitalWrite(PGRE1, LOW);
  delay (tyel);
}

void blinkYellow()
{
  //memoriza valores actuais dos LEDS
  mem0 = digitalRead(RED1);
  mem1 = digitalRead(YEL1);
  mem2 = digitalRead(GRE1);
  mem3 = digitalRead(RED2);
  mem4 = digitalRead(YEL2);
  mem5 = digitalRead(GRE2);
  mem6 = digitalRead(PRED1);
  mem7 = digitalRead(PGRE1);
  mem8 = digitalRead(PRED2);
  mem9 = digitalRead(PGRE2);

  //Apaga todos os LEDS 
  digitalWrite(RED1, LOW);
  digitalWrite(YEL1, LOW);
  digitalWrite(GRE1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(YEL2, LOW);
  digitalWrite(GRE2, LOW);
  digitalWrite(PRED1, LOW);
  digitalWrite(PGRE1, LOW);
  digitalWrite(PRED2, LOW);
  digitalWrite(PGRE2, LOW);

  //Inicia sequencia de pisca pisca
  for (int i=0; i <= 100; i++)
  {
    digitalWrite (YEL1,HIGH);
    digitalWrite (YEL2,HIGH); 
    delay (500);
    digitalWrite (YEL1,LOW);
    digitalWrite (YEL2,LOW); 
    delay (500);

    BT1=digitalRead(PREQ1);//l estado do botao 1
    BT2=digitalRead(PREQ2);//l estado do botao 2
    BT1=!BT1;//inverte estado do botao 1
    BT2=!BT2;//inverte estado do botao 2
    TEST=BT1&&BT2;
    FLASH=BT1||BT2;//Executa operao OR entre BT1 e BT2 e poe resultado na Variavel FLASH
    if (TEST) 
    {
      test1(); 
      FLASH=LOW;
    }   
    if (FLASH) 
    {
      i=100; 
    }
  }

  //Repoe LEDS como estavam originalmente
  digitalWrite(RED1, mem0);
  digitalWrite(YEL1, mem1);
  digitalWrite(GRE1, mem2);
  digitalWrite(RED2, mem3);
  digitalWrite(YEL2, mem4);
  digitalWrite(GRE2, mem5);
  digitalWrite(PRED1, mem6);
  digitalWrite(PGRE1, mem7);
  digitalWrite(PRED2, mem8);
  digitalWrite(PGRE2, mem9); 
}

void test1()
{
  //Inicia sequencia de luzes
  delay(400);
  for (int a=0; a <= 30; a++)
  {
    for (int i=0; i <= 9; i++)
    {
      digitalWrite (i,HIGH);
    }
    delay(100);
    for (int i=9; i >= 0; i--)
    {
      digitalWrite (i,LOW);
    }
    delay (100);

    BT1=digitalRead(PREQ1);//l estado do botao 1
    BT2=digitalRead(PREQ2);//l estado do botao 2
    BT1=!BT1;//inverte estado do botao 1
    BT2=!BT2;//inverte estado do botao 2
    FLASH=BT1||BT2;//Executa operao OR entre BT1 e BT2 e poe resultado na Variavel FLASH

    if (FLASH) 
    {
      a=30; 
    }
  }
}

void test2()
{
  //Inicia sequencia de luzes
  delay(400);
  for (int a=0; a <= 30; a++)
  {
    for (int i=0; i <= 9; i++)
    {
      digitalWrite (i,HIGH);
      delay(100);      
    }

    for (int i=9; i >= 0; i--)
    {
      digitalWrite (i,LOW);
      delay (100);      
    }

    BT1=digitalRead(PREQ1);//l estado do botao 1
    BT2=digitalRead(PREQ2);//l estado do botao 2
    BT1=!BT1;//inverte estado do botao 1
    BT2=!BT2;//inverte estado do botao 2
    FLASH=BT1||BT2;//Executa operao OR entre BT1 e BT2 e poe resultado na Variavel FLASH

    if (FLASH) 
    {
      a=30; 
    }
  }
}

Credits

lmsousa
5 projects • 21 followers
Contact

Comments

Please log in or sign up to comment.