Traffic light
My 3 years old son is a fan of the traffic lights, and I decided to create a simulator for him.
Components and supplies
1
4xAA battery holder
1
Switch Actuator, APEM A01 series Illuminated Push-Button Switches
1
5 mm LED: Red
1
DC/DC Charge Pump Adjustable Voltage Regulator, 2.7V to 5.5V in
1
Arduino Nano R3
1
5 mm LED: Yellow
1
5 mm LED: Green
Project description
Code
Traffic Light code
arduino
1//Setup the PINs on board 2int LED_RED = 4; 3int LED_YELLOW = 5; 4int LED_GREEN = 6; 5int LED_BUTTON = 7; 6int BUTTON = 8; 7int BUZZER = 9; 8 9//Declaring general variables 10long now = 0; //General actual time 11long lastAction = 0; //Last action on the actual block 12long lastBeep = 0; //Checks the initial sequence's blinking periods 13long buttonTimer = 0; //Checks how much time spent since the button had been pushed (hold time) 14long longPressTime = 400; //Setup the required time of the long press (sequence start) 15 16int Program = 0; //Program number 17int NewProgram = 0; //Button pushed, the new program is initiated 18int BeginTrigger = 0; //Enable or disable the initial sequence (blinking button and buzzer) 19int buttonstate = 0; //Stores the button state (pushed or released) 20int Program1Active = 0; //Stores if Program 1 is active 21int Program2Active = 0; //Stores if Program 2 is active 22int BuzzerAuth = 1; //Enable or disable the sound during the initial sequence 23int BeepTrigger = 0; //Helps to determine how many times the button flashes ( 2= 1 flash; 4= 2 flashes) 24int ProgCont = 0; //Enable or disable the main sequence (traffic light) after the initial period 25 26boolean LEDRED_State = false; //RED LED's state 27boolean LEDYELLOW_State = false; //YELLOW LED's state 28boolean LEDGREEN_State = false; //GREEN LED's state 29boolean LEDBUTTON_State = false; //BUTTON LED's state 30boolean BUZZER_State = false; //BUZZER's state 31boolean buttonActive = false; //This will be 'true' if the button had been pushed and 'false' if it had been released 32boolean longPressActive = false; //This will be 'true' if we reached the limit of the 'longPressTime' which means the button is in 'HOLD'. 33 34 35//Initialize the PINs on the board 36void setup() { 37 pinMode(LED_RED, OUTPUT); 38 pinMode(LED_YELLOW, OUTPUT); 39 pinMode(LED_GREEN, OUTPUT); 40 pinMode(LED_BUTTON, OUTPUT); 41 pinMode(BUZZER, OUTPUT); 42 pinMode(BUTTON, INPUT); 43// Serial.begin(9600); //Uncomment if need to debug 44} 45 46//Main program 47void loop() { 48// Check the BUTTON state 49 buttonstate = digitalRead(BUTTON); 50 if (buttonstate == HIGH) { 51 NewProgram = 1; 52 if (buttonActive == false) { 53 buttonActive = true; 54// Serial.println("The button is pressed..."); //For debug only 55// Serial.println(Program); //For debug only 56 // 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. 57 if (Program1Active == 1) { 58 Program = 2; 59 lastBeep = millis(); 60 BeepTrigger = 0; 61 ProgCont = 0; 62 BuzzerAuth = 1; 63 } 64 65 // 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. 66 if (Program2Active == 1) { 67 Program = 1; 68 lastBeep = millis(); 69 BeepTrigger = 0; 70 ProgCont = 0; 71 BuzzerAuth = 1; 72 } 73 74 // 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. 75 if (Program == 3) { 76 Program = 0; 77 BeepTrigger = 0; 78 ProgCont = 0; 79 BuzzerAuth = 1; 80 BeginTrigger = 0; 81 } 82 buttonTimer = millis(); 83 } 84 85 // Set the longPressActive to TRUE in case of the button is pressed longer period which is determined in 'longPressTime'. 86 if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { 87 longPressActive = true; 88 BeginTrigger = 0; 89 } 90 91 } else { 92 if (buttonActive == true) { 93 if (longPressActive == true) { 94 //Tasks during LONG PRESS of the button. 95 Program = 3; 96 longPressActive = false; 97 } else { 98 longPressActive = false; 99 //Tasks during SHORT PRESS of the button. 100 if (Program == 0) { 101 Program = 1; 102 } 103 104 if (Program1Active == 1) { 105 Program = 2; 106 } 107 108 if (Program2Active == 1) { 109 Program = 1; 110 } 111 112 if (Program == 3) { 113 BeginTrigger = 0; 114 Program = 0; 115 } 116 } 117 buttonActive = false; //Button released. 118 } 119 } 120 121 //Program "0" is the initial program, only the RED is lighting. 122 if (Program == 0 && NewProgram == 1) { 123 LEDRED_State = true; 124 LEDYELLOW_State = false; 125 LEDGREEN_State = false; 126 LEDBUTTON_State = false; 127 BUZZER_State = false; 128 NewProgram = 0; 129 } 130 131 132 //Program "1" is the sequence from RED to GREEN. All sequences start with one initial sound and blink of the button's LED twice. 133 if (Program == 1 && NewProgram == 1){ 134 Program2Active = 0; 135 // This is the INITIAL sequence of the program. 136 if (BeginTrigger == 0) { 137 if (BeepTrigger < 4){ //This is the number of the initial sequence where "2" means 1 blinking period. 138 now = millis(); 139 if (now - lastBeep > 400 * 1) { //This is the frequency of the blinking. 140 lastBeep = millis(); 141 BeepTrigger++; 142 if (LEDBUTTON_State == false) { 143 LEDBUTTON_State = true; 144 if (BuzzerAuth == 1) { 145 BUZZER_State = true; 146 } 147 BuzzerAuth = 0; //We only allow the buzzer to operate in the first phase of the initial sequence. 148 } else { 149 LEDBUTTON_State = false; 150 BUZZER_State = false; 151 } 152 } 153 } else { 154 BeginTrigger = 1; 155 BuzzerAuth = 1; 156 ProgCont = 1; 157 lastAction = millis(); 158 } 159 } 160 // This is the MAIN sequence of the program. 161 if (ProgCont == 1) { 162 LEDRED_State = true; 163 LEDYELLOW_State = true; 164 now = millis(); 165 if (now - lastAction > 4000 * 1) { 166 lastAction = now; 167 LEDRED_State = false; 168 LEDYELLOW_State = false; 169 LEDGREEN_State = true; 170 NewProgram = 0; 171 BeginTrigger = 0; 172 Program1Active = 1; 173 } 174 } 175 } 176 177 178 //Program "2" is the sequence from GREEN to RED. All sequences start with one initial sound and blink of the button's LED twice. 179 if (Program == 2 && NewProgram == 1){ 180 Program1Active = 0; 181 // This is the INITIAL sequence of the program. 182 if (BeginTrigger == 0) { 183 if (BeepTrigger < 4){ //This is the number of the initial sequence where "2" means 1 blinking period. 184 now = millis(); 185 if (now - lastBeep > 400 * 1) { //This is the frequency of the blinking. 186 lastBeep = millis(); 187 BeepTrigger++; 188 if (LEDBUTTON_State == false) { 189 LEDBUTTON_State = true; 190 if (BuzzerAuth == 1) { 191 BUZZER_State = true; 192 } 193 BuzzerAuth = 0; //We only allow the buzzer to operate in the first phase of the initial sequence. 194 } else { 195 LEDBUTTON_State = false; 196 BUZZER_State = false; 197 } 198 } 199 } else { 200 BeginTrigger = 1; 201 BuzzerAuth = 1; 202 ProgCont = 1; 203 lastAction = millis(); 204 } 205 } 206 // This is the MAIN sequence of the program. 207 if (ProgCont == 1) { 208 LEDGREEN_State = false; 209 LEDRED_State = false; 210 LEDYELLOW_State = true; 211 now = millis(); 212 if (now - lastAction > 4000 * 1) { 213 lastAction = now; 214 LEDRED_State = true; 215 LEDYELLOW_State = false; 216 LEDGREEN_State = false; 217 NewProgram = 0; 218 BeginTrigger = 0; 219 Program2Active = 1; 220 ProgCont = 0; 221 } 222 } 223 } 224 225 // Program "3" is simulating the traffic light's "out of order" status. The YELLOW led is blinking. 226 if (Program == 3){ 227 if (BeginTrigger == 0) { 228 LEDRED_State = false; 229 LEDYELLOW_State = false; 230 LEDGREEN_State = false; 231 LEDBUTTON_State = false; 232 BUZZER_State = false; 233 NewProgram = 0; 234 BeginTrigger = 1; 235 BuzzerAuth = 1; 236 lastAction = millis(); 237 } 238 now = millis(); 239 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. 240 lastAction = now; 241 if (LEDYELLOW_State == false) { 242 LEDYELLOW_State = true; 243 LEDRED_State = false; 244 LEDGREEN_State = false; 245 } else { 246 LEDYELLOW_State = false; 247 LEDRED_State = false; 248 LEDGREEN_State = false; 249 } 250 } 251 } 252 253 //Shows the status of the traffic light 254 digitalWrite(LED_RED, LEDRED_State); 255 digitalWrite(LED_YELLOW, LEDYELLOW_State); 256 digitalWrite(LED_GREEN, LEDGREEN_State); 257 digitalWrite(LED_BUTTON, LEDBUTTON_State); 258 digitalWrite(BUZZER, BUZZER_State); 259 260} 261
Comments
Only logged in users can leave comments