bokilebobile
Published © GPL3+

Traffic Light

It's a traffic light. I'm a 12 year old boy and I like to program.

IntermediateShowcase (no instructions)82
Traffic Light

Things used in this project

Hardware components

Perma-Proto Breadboard Half Size
Perma-Proto Breadboard Half Size
×1
Arduino Nano R3
Arduino Nano R3
×1
Switch Actuator, Head for spring return push-button
Switch Actuator, Head for spring return push-button
×2
RGB Diffused Common Anode
RGB Diffused Common Anode
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Traffic light

Arduino
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int vreme,boja,normal,yel,start,kraj;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);




void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 3);

pinMode (8,OUTPUT);  // crvena LED
pinMode (6,OUTPUT);  // zuta LED
pinMode (7,OUTPUT);  // zelena LED

pinMode (9,INPUT_PULLUP);  // start,normal = 1
pinMode (10,INPUT_PULLUP); // kraj,normal = 0

vreme  = 55;
boja   = 1;  // 1 - crveno, 2 - zuto, 3 - zeleno
normal = 1 ; // 1 - semafor radi normalno, 0 - zuto trepcuce
}


void loop() {

  start = digitalRead (9);
  kraj  = digitalRead (10);

  if (start == 0) {
    normal = 1;
  }
  if (kraj == 0) {
    normal = 0;
    lcd.clear();
    lcd.print ("Semafor ne radi!");
    lcd.setCursor (0, 1);
    lcd.print ("Vozi pazljivo!");
    vreme = 55;
    boja = 1;
    digitalWrite (6, LOW);
    digitalWrite (7, LOW);
    digitalWrite (8, LOW);
  }

  if (normal == 1) {
     if (boja == 1)
       {digitalWrite (8,HIGH);
       digitalWrite (6,LOW);
       digitalWrite (7,LOW);}
     if (boja == 2)
       { digitalWrite (8,LOW);
       digitalWrite (6,HIGH);
       digitalWrite (7,LOW);}
    if (boja == 3)
      { digitalWrite (8,LOW);
      digitalWrite (6,LOW);
      digitalWrite (7,HIGH);}
    lcd.clear();
    lcd.print (vreme);
    delay (998);
    vreme = vreme - 1;

    if (vreme == 0) {
      if (boja == 3) {
        boja = 0;
      }
      boja = boja + 1;

      if (boja == 2) {
        vreme = 11;
      }
      else {
        vreme = 55;
      }
    
    }
  }
  else {
    digitalWrite (6, HIGH);
    delay (500);
    digitalWrite (6, LOW);
    delay (500);
  }

}

Credits

bokilebobile
0 projects • 0 followers

Comments