KissGe83
Published © GPL3+

Traffic light

My 3 years old son is a fan of the traffic lights, and I decided to create a simulator for him.

BeginnerProtip196
Traffic light

Things used in this project

Hardware components

5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Arduino Nano R3
Arduino Nano R3
×1
Switch Actuator, APEM A01 series Illuminated Push-Button Switches
Switch Actuator, APEM A01 series Illuminated Push-Button Switches
×1
4xAA battery holder
4xAA battery holder
×1
DC/DC Charge Pump Adjustable Voltage Regulator, 2.7V to 5.5V in
DC/DC Charge Pump Adjustable Voltage Regulator, 2.7V to 5.5V in
×1

Story

Read more

Code

Traffic Light code

Arduino
//Setup the PINs on board
int LED_RED = 4;
int LED_YELLOW = 5;
int LED_GREEN = 6;
int LED_BUTTON = 7;
int BUTTON = 8;
int BUZZER = 9;

//Declaring general variables
long now = 0;                             //General actual time
long lastAction = 0;                      //Last action on the actual block
long lastBeep = 0;                        //Checks the initial sequence's blinking periods
long buttonTimer = 0;                     //Checks how much time spent since the button had been pushed (hold time)
long longPressTime = 400;                 //Setup the required time of the long press (sequence start)

int Program = 0;                          //Program number
int NewProgram = 0;                       //Button pushed, the new program is initiated 
int BeginTrigger = 0;                     //Enable or disable the initial sequence (blinking button and buzzer)
int buttonstate = 0;                      //Stores the button state (pushed or released)
int Program1Active = 0;                   //Stores if Program 1 is active         
int Program2Active = 0;                   //Stores if Program 2 is active
int BuzzerAuth = 1;                       //Enable or disable the sound during the initial sequence
int BeepTrigger = 0;                      //Helps to determine how many times the button flashes ( 2= 1 flash; 4= 2 flashes) 
int ProgCont = 0;                         //Enable or disable the main sequence (traffic light) after the initial period

boolean LEDRED_State = false;             //RED LED's state
boolean LEDYELLOW_State = false;          //YELLOW LED's state
boolean LEDGREEN_State = false;           //GREEN LED's state
boolean LEDBUTTON_State = false;          //BUTTON LED's state
boolean BUZZER_State = false;             //BUZZER's state
boolean buttonActive = false;             //This will be 'true' if the button had been pushed and 'false' if it had been released
boolean longPressActive = false;          //This will be 'true' if we reached the limit of the 'longPressTime' which means the button is in 'HOLD'.


//Initialize the PINs on the board
void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BUTTON, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(BUTTON, INPUT);
//  Serial.begin(9600);                   //Uncomment if need to debug
}

//Main program 
void loop() {
// Check the BUTTON state
  buttonstate = digitalRead(BUTTON);
  if (buttonstate == HIGH) {
      NewProgram = 1;
    if (buttonActive == false) {
      buttonActive = true;
//      Serial.println("The button is pressed...");       //For debug only
//      Serial.println(Program);                          //For debug only
        // If Program "1" is active, let's choose Program "2", reset the number of the initial sequence's "BeepTrigger" and stop the program with "ProgCont". Program run will be enabled after the initial button blinking and buzzer.
        if (Program1Active == 1) {
          Program = 2;
          lastBeep = millis();
          BeepTrigger = 0;
          ProgCont = 0;
          BuzzerAuth = 1; 
        }

        // If Program "2" is active, let's choose Program "1", reset the number of the initial sequence's "BeepTrigger" and stop the program with "ProgCont". Program run will be enabled after the initial button blinking and buzzer.
        if (Program2Active == 1) {
          Program = 1;
          lastBeep = millis();
          BeepTrigger = 0;
          ProgCont = 0;
          BuzzerAuth = 1;
        }

        // If Program "3" is active, let's choose Program "0", reset the number of the initial sequence's "BeepTrigger" and stop the program with "ProgCont". Program run will be enabled after the initial button blinking and buzzer. 
        if (Program == 3) {
          Program = 0;
          BeepTrigger = 0;
          ProgCont = 0;
          BuzzerAuth = 1;
          BeginTrigger = 0;
        }
      buttonTimer = millis();
    }

    // Set the longPressActive to TRUE in case of the button is pressed longer period which is determined in 'longPressTime'.
    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
      longPressActive = true;
      BeginTrigger = 0;
    } 
    
  } else {
    if (buttonActive == true) {
      if (longPressActive == true) {
          //Tasks during LONG PRESS of the button.
          Program = 3;
          longPressActive = false;
    } else {
        longPressActive = false;
        //Tasks during SHORT PRESS of the button.
        if (Program == 0) {
          Program = 1;
        }

        if (Program1Active == 1) {
          Program = 2;
        }

        if (Program2Active == 1) {
          Program = 1;
        }

        if (Program == 3) {
          BeginTrigger = 0;
          Program = 0;
        }
      }
      buttonActive = false;             //Button released.
    }
   }

  //Program "0" is the initial program, only the RED is lighting.
  if (Program == 0 && NewProgram == 1) {
    LEDRED_State = true;
    LEDYELLOW_State = false;
    LEDGREEN_State = false;
    LEDBUTTON_State = false;
    BUZZER_State = false;
    NewProgram = 0;
  }


  //Program "1" is the sequence from RED to GREEN. All sequences start with one initial sound and blink of the button's LED twice.
  if (Program == 1 && NewProgram == 1){
    Program2Active = 0;
    // This is the INITIAL sequence of the program.
    if (BeginTrigger == 0) {
      if (BeepTrigger < 4){                               //This is the number of the initial sequence where "2" means 1 blinking period.
        now = millis();
        if (now - lastBeep > 400 * 1) {                   //This is the frequency of the blinking.
          lastBeep = millis();
          BeepTrigger++;
          if (LEDBUTTON_State == false) {
            LEDBUTTON_State = true;
            if (BuzzerAuth == 1) {
              BUZZER_State = true;
            }
            BuzzerAuth = 0;                              //We only allow the buzzer to operate in the first phase of the initial sequence.
          } else {
            LEDBUTTON_State = false;
            BUZZER_State = false;
          }
          } 
        } else {
         BeginTrigger = 1;
         BuzzerAuth = 1;
         ProgCont = 1;
         lastAction = millis();
        }
       }
    // This is the MAIN sequence of the program.  
    if (ProgCont == 1) {
      LEDRED_State = true;
      LEDYELLOW_State = true;
      now = millis();
      if (now - lastAction > 4000 * 1) {
        lastAction = now;
        LEDRED_State = false;
        LEDYELLOW_State = false;
        LEDGREEN_State = true;
        NewProgram = 0;
        BeginTrigger = 0;
        Program1Active = 1;
      }
    }
  }


  //Program "2" is the sequence from GREEN to RED. All sequences start with one initial sound and blink of the button's LED twice.
  if (Program == 2 && NewProgram == 1){
    Program1Active = 0;
    // This is the INITIAL sequence of the program.
    if (BeginTrigger == 0) {
      if (BeepTrigger < 4){                   //This is the number of the initial sequence where "2" means 1 blinking period.
        now = millis();
        if (now - lastBeep > 400 * 1) {       //This is the frequency of the blinking.
          lastBeep = millis();
          BeepTrigger++;
          if (LEDBUTTON_State == false) {
            LEDBUTTON_State = true;
            if (BuzzerAuth == 1) {
              BUZZER_State = true;
            }
            BuzzerAuth = 0;                 //We only allow the buzzer to operate in the first phase of the initial sequence.
          } else {
            LEDBUTTON_State = false;
            BUZZER_State = false;
          }
          } 
        } else {
         BeginTrigger = 1;
         BuzzerAuth = 1;
         ProgCont = 1;
         lastAction = millis();
        }
       }
    // This is the MAIN sequence of the program. 
    if (ProgCont == 1) {
      LEDGREEN_State = false;
      LEDRED_State = false;
      LEDYELLOW_State = true;
      now = millis();
      if (now - lastAction > 4000 * 1) {
        lastAction = now;
        LEDRED_State = true;
        LEDYELLOW_State = false;
        LEDGREEN_State = false;
        NewProgram = 0;
        BeginTrigger = 0;
        Program2Active = 1;
        ProgCont = 0;
      }       
    }
  }

  // Program "3" is simulating the traffic light's "out of order" status. The YELLOW led is blinking.
  if (Program == 3){
    if (BeginTrigger == 0) {
        LEDRED_State = false;
        LEDYELLOW_State = false;
        LEDGREEN_State = false;
        LEDBUTTON_State = false;
        BUZZER_State = false;
        NewProgram = 0;
        BeginTrigger = 1;
        BuzzerAuth = 1;
        lastAction = millis();
    }
    now = millis();
    if (now - lastAction > 500 * 1) {       //This is the frequency of the blinking, where '500' means the LED is ON/OFF in every 0,5 seconds, where 1 period will happen in 1 second.
      lastAction = now;
      if (LEDYELLOW_State == false) {
        LEDYELLOW_State = true;
        LEDRED_State = false;
        LEDGREEN_State = false;
      } else {
        LEDYELLOW_State = false;
        LEDRED_State = false;
        LEDGREEN_State = false;
      }
    }  
  }

    //Shows the status of the traffic light
    digitalWrite(LED_RED, LEDRED_State);
    digitalWrite(LED_YELLOW, LEDYELLOW_State);
    digitalWrite(LED_GREEN, LEDGREEN_State);
    digitalWrite(LED_BUTTON, LEDBUTTON_State);
    digitalWrite(BUZZER, BUZZER_State);

}

Credits

KissGe83
1 project • 0 followers

Comments