#define STH1 13

#define BUZZ 12

#define LED1 1
#define LED2 2
#define LED3 3
#define LED4 4
#define LED5 5
#define LED6 6

#define STRT 1000
#define REDT 4000
#define YELT 2000
#define GRET 3000
#define BLKT 500

int timest;
int time;
int sths1;
int i;

void setup() 
{
  pinMode(STH1, INPUT);

  pinMode(BUZZ, OUTPUT);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);


  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);

  digitalWrite(BUZZ, HIGH);

}

void traficlight()
{
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);
  
  digitalWrite(LED1, LOW);
  timest = millis();
  while(REDT + timest > time)
    {
      time = millis();
      if((time/100) % 2 == 0)
      {
        digitalWrite(BUZZ, LOW);
        digitalWrite(BUZZ, HIGH);
      }
    }
  digitalWrite(LED1, HIGH);

  digitalWrite(LED2, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(BUZZ, LOW);
  timest = millis();
  while(YELT + timest > time)
    time = millis();
  digitalWrite(LED2, HIGH);
  

  digitalWrite(LED3, LOW);
  timest = millis();
  while(GRET + timest > time)
    time = millis();
  digitalWrite(LED3, HIGH);

  digitalWrite(LED2, LOW);
  timest = millis();
  while(YELT + timest > time)
    time = millis();
  digitalWrite(LED2, HIGH);
  digitalWrite(LED5, HIGH);

}

void emergency()
{
    digitalWrite(LED2, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(BUZZ, LOW);
    timest = millis();
    while(BLKT + timest > time)
      time = millis();
    digitalWrite(BUZZ, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED6, HIGH);
    timest = millis();
    while(BLKT + timest > time)
      time = millis();
}

void loop() 
{
  delay(STRT);

  time = millis();
  sths1 = digitalRead(STH1);

  if(sths1 == HIGH)
    emergency();
  else
    traficlight();


}