Arduino Nano - Traffic Light
Learn how traffic light module works, how to connect traffic light module to Arduino Nano, how to program Arduino Nano step by step. The detail instruction, code, wiring diagram, video tutorial, line-by-line code explanation are provided to help you
Devices & Components
1
Arduino Nano
1
Traffic Light Module
1
DIYables Nano V3.0 Development Board
Software & Tools
Arduino IDE
Project description
Code
arduino-nano-traffic-light.ino
cpp
Full code from the newbiely.com tutorial
1/* 2 * This Arduino Nano code was developed by newbiely.com 3 * 4 * This Arduino Nano code is made available for public use without any restriction 5 * 6 * For comprehensive instructions and wiring diagrams, please visit: 7 * https://newbiely.com/tutorials/arduino-nano/arduino-nano-traffic-light 8 */ 9 10#define PIN_RED 3 // The Arduino Nano pin connected to R pin of traffic light module 11#define PIN_YELLOW 4 // The Arduino Nano pin connected to Y pin of traffic light module 12#define PIN_GREEN 5 // The Arduino Nano pin connected to G pin of traffic light module 13 14#define RED_TIME 4000 // RED time in millisecond 15#define YELLOW_TIME 4000 // YELLOW time in millisecond 16#define GREEN_TIME 4000 // GREEN time in millisecond 17 18void setup() { 19 pinMode(PIN_RED, OUTPUT); 20 pinMode(PIN_YELLOW, OUTPUT); 21 pinMode(PIN_GREEN, OUTPUT); 22} 23 24// The loop function repeats indefinitely 25void loop() { 26 // red light on 27 digitalWrite(PIN_RED, HIGH); // turn on 28 digitalWrite(PIN_YELLOW, LOW); // turn off 29 digitalWrite(PIN_GREEN, LOW); // turn off 30 delay(RED_TIME); // keep red light on during a period of time 31 32 // yellow light on 33 digitalWrite(PIN_RED, LOW); // turn off 34 digitalWrite(PIN_YELLOW, HIGH); // turn on 35 digitalWrite(PIN_GREEN, LOW); // turn off 36 delay(YELLOW_TIME); // keep yellow light on during a period of time 37 38 // green light on 39 digitalWrite(PIN_RED, LOW); // turn off 40 digitalWrite(PIN_YELLOW, LOW); // turn off 41 digitalWrite(PIN_GREEN, HIGH); // turn on 42 delay(GREEN_TIME); // keep green light on during a period of time 43}
arduino-nano-traffic-light-3.ino
cpp
Full code from the newbiely.com tutorial
1/* 2 * This Arduino Nano code was developed by newbiely.com 3 * 4 * This Arduino Nano code is made available for public use without any restriction 5 * 6 * For comprehensive instructions and wiring diagrams, please visit: 7 * https://newbiely.com/tutorials/arduino-nano/arduino-nano-traffic-light 8 */ 9 10#define PIN_RED 3 // The Arduino Nano pin connected to R pin of traffic light module 11#define PIN_YELLOW 4 // The Arduino Nano pin connected to Y pin of traffic light module 12#define PIN_GREEN 5 // The Arduino Nano pin connected to G pin of traffic light module 13 14#define RED_TIME 2000 // RED time in millisecond 15#define YELLOW_TIME 1000 // YELLOW time in millisecond 16#define GREEN_TIME 2000 // GREEN time in millisecond 17 18#define RED 0 // Index in array 19#define YELLOW 1 // Index in array 20#define GREEN 2 // Index in array 21 22const int pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN}; 23const int times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME}; 24 25void setup() { 26 pinMode(PIN_RED, OUTPUT); 27 pinMode(PIN_YELLOW, OUTPUT); 28 pinMode(PIN_GREEN, OUTPUT); 29} 30 31// The loop function repeats indefinitely 32void loop() { 33 for (int light = RED; light <= GREEN; light ++) { 34 trafic_light_on(light); 35 delay(times[light]); // keep light on during a period of time 36 } 37} 38 39void trafic_light_on(int light) { 40 for (int i = RED; i <= GREEN; i ++) { 41 if (i == light) 42 digitalWrite(pins[i], HIGH); // turn on 43 else 44 digitalWrite(pins[i], LOW); // turn off 45 } 46}
arduino-nano-traffic-light-2.ino
cpp
Full code from the newbiely.com tutorial
1/* 2 * This Arduino Nano code was developed by newbiely.com 3 * 4 * This Arduino Nano code is made available for public use without any restriction 5 * 6 * For comprehensive instructions and wiring diagrams, please visit: 7 * https://newbiely.com/tutorials/arduino-nano/arduino-nano-traffic-light 8 */ 9 10#define PIN_RED 3 // The Arduino Nano pin connected to R pin of traffic light module 11#define PIN_YELLOW 4 // The Arduino Nano pin connected to Y pin of traffic light module 12#define PIN_GREEN 5 // The Arduino Nano pin connected to G pin of traffic light module 13 14#define RED_TIME 2000 // RED time in millisecond 15#define YELLOW_TIME 1000 // YELLOW time in millisecond 16#define GREEN_TIME 2000 // GREEN time in millisecond 17 18#define RED 0 // Index in array 19#define YELLOW 1 // Index in array 20#define GREEN 2 // Index in array 21 22const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; 23const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; 24 25void setup() { 26 pinMode(PIN_RED, OUTPUT); 27 pinMode(PIN_YELLOW, OUTPUT); 28 pinMode(PIN_GREEN, OUTPUT); 29} 30 31// The loop function repeats indefinitely 32void loop() { 33 // red light on 34 trafic_light_on(RED); 35 delay(times[RED]); // keep red light on during a period of time 36 37 // yellow light on 38 trafic_light_on(YELLOW); 39 delay(times[YELLOW]); // keep yellow light on during a period of time 40 41 // green light on 42 trafic_light_on(GREEN); 43 delay(times[GREEN]); // keep green light on during a period of time 44} 45 46void trafic_light_on(int light) { 47 for (int i = RED; i <= GREEN; i++) { 48 if (i == light) 49 digitalWrite(pins[i], HIGH); // turn on 50 else 51 digitalWrite(pins[i], LOW); // turn off 52 } 53}
arduino-nano-traffic-light-4.ino
cpp
Full code from the newbiely.com tutorial
1/* 2 * This Arduino Nano code was developed by newbiely.com 3 * 4 * This Arduino Nano code is made available for public use without any restriction 5 * 6 * For comprehensive instructions and wiring diagrams, please visit: 7 * https://newbiely.com/tutorials/arduino-nano/arduino-nano-traffic-light 8 */ 9 10#define PIN_RED 3 // The Arduino Nano pin connected to R pin of traffic light module 11#define PIN_YELLOW 4 // The Arduino Nano pin connected to Y pin of traffic light module 12#define PIN_GREEN 5 // The Arduino Nano pin connected to G pin of traffic light module 13 14#define RED_TIME 2000 // RED time in millisecond 15#define YELLOW_TIME 1000 // YELLOW time in millisecond 16#define GREEN_TIME 2000 // GREEN time in millisecond 17 18#define RED 0 // Index in array 19#define YELLOW 1 // Index in array 20#define GREEN 2 // Index in array 21 22const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; 23const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; 24unsigned long last_time = 0; 25int light = RED; // start with RED light 26 27void setup() { 28 pinMode(PIN_RED, OUTPUT); 29 pinMode(PIN_YELLOW, OUTPUT); 30 pinMode(PIN_GREEN, OUTPUT); 31 trafic_light_on(light); 32 last_time = millis(); 33} 34 35// The loop function repeats indefinitely 36void loop() { 37 if ((millis() - last_time) > times[light]) { 38 light++; 39 if (light >= 3) 40 light = RED; // new circle 41 42 trafic_light_on(light); 43 last_time = millis(); 44 } 45 46 // TO DO: your other code 47} 48 49void trafic_light_on(int light) { 50 for (int i = RED; i <= GREEN; i++) { 51 if (i == light) 52 digitalWrite(pins[i], HIGH); // turn on 53 else 54 digitalWrite(pins[i], LOW); // turn off 55 } 56}
Comments
Only logged in users can leave comments