Components and supplies
Arduino UNO
Breadboard (generic)
SparkFun 7-Segment Serial Display - Red
Pushbutton switch 12mm
LED (generic)
Apps and platforms
Arduino IDE
Project description
Code
Arduino Pedestrian Traffic Lights Code
arduino
1//leds for both traffic lights 2int sinal_pedestre[2] = {A1, A0}; 3int sinal_carros[3] = {A2, A3, A4}; 4//pushbuttonpin 5byte botao = 12; 6//auxiliar time variables 7unsigned long t_0; 8unsigned long t_1; 9unsigned long t_aux; 10unsigned int safe_time; 11//Waiting time for a request just after the traffic lights of the cars open 12unsigned int t = 4000; 13 14//matrix of the display of 7 segments 15int disp[10][8] = { { 0, 1, 1, 1, 1, 1, 1, 0 }, // = Digit 0 16 { 0, 1, 0, 0, 1, 0, 0, 0 }, // = Digit 1 17 { 0, 0, 1, 1, 1, 1, 0, 1 }, // = Digit 2 18 { 0, 1, 1, 0, 1, 1, 0, 1 }, // = Digit 3 19 { 0, 1, 0, 0, 1, 0, 1, 1 }, // = Digit 4 20 { 0, 1, 1, 0, 0, 1, 1, 1 }, // = Digit 5 21 { 0, 1, 1, 1, 0, 1, 1, 1 }, // = Digit 6 22 { 0, 1, 0, 0, 1, 1, 0, 0 }, // = Digit 7 23 { 0, 1, 1, 1, 1, 1, 1, 1 }, // = Digit 8 24 { 0, 1, 1, 0, 1, 1, 1, 1 } // = Digit 9 25}; 26 27void setup() { 28 29 //PinMode of display pins 30 for (int i = 2; i < 9; i++) 31 { 32 pinMode(i, OUTPUT); 33 } 34 //PinMode of leds pins 35 for (int i = A0; i < A5; i++) 36 { 37 pinMode(i, OUTPUT); 38 } 39 ////PinMode of pusbutton pin 40 pinMode(botao, INPUT); 41 t_0 = millis(); 42} 43 44void loop() { 45 46 //Show the S of stop at the beginning 47 number_display(5); 48 49 //conting the minimum time that the traffic light will be green for the cars 50 t_1 = millis(); 51 if (t_1 - t_0 < t) { 52 safe_time = t_1 - t_0; 53 } 54 55 //green sign for cars and red for pedestrians 56 digitalWrite(sinal_carros[2], HIGH); 57 digitalWrite(sinal_pedestre[0], HIGH); 58 //while no one makes a request, the signal will not change. 59 if (digitalRead(botao) == HIGH) 60 { 61 62 delay(8000 - safe_time); 63 64 //red light for cars goes out and yellow turns on for 2.5 seconds 65 digitalWrite(sinal_carros[2], LOW); 66 digitalWrite(sinal_carros[1], HIGH); 67 68 delay(2500); 69 70 //Pedestrians sign opens 71 digitalWrite(sinal_carros[1], LOW); 72 digitalWrite(sinal_carros[0], HIGH); 73 digitalWrite(sinal_pedestre[0], LOW); 74 digitalWrite(sinal_pedestre[1], HIGH); 75 76 //Countdown to the pedestrian signal close 77 t_0 = millis(); 78 t_aux = 0; 79 while (t_aux < 5000) { 80 idle_display(); 81 t_aux = millis() - t_0; 82 } 83 84 for (int k = 9; k > 0; k--) { 85 number_display(k); 86 87 delay(1000); 88 } 89 //the signals are turned off for the cycle to start again 90 digitalWrite(sinal_carros[0], LOW); 91 digitalWrite(sinal_pedestre[1], LOW); 92 93 t_0 = millis(); 94 } 95 96} 97//function that shows a number in the 7 segment display 98 99void number_display(int m) { 100 101 for (int j = 2; j < 9; j++) { 102 digitalWrite(j, disp[m][j - 1]); 103 } 104 105} 106 107void idle_display() { 108 turnOf_display(); 109 digitalWrite(3, 1); 110 delay(150); 111 digitalWrite(3, 0); 112 digitalWrite(8, 1); 113 delay(150); 114 digitalWrite(8, 0); 115 digitalWrite(6, 1); 116 delay(150); 117 digitalWrite(6, 0); 118 delay(150); 119 120} 121 122void turnOf_display() { 123 for (int i = 2; i < 9; i++) { 124 digitalWrite(i, 0); 125 } 126} 127
Arduino Pedestrian Traffic Lights Code
arduino
1//leds for both traffic lights 2int sinal_pedestre[2] = {A1, A0}; 3int 4 sinal_carros[3] = {A2, A3, A4}; 5//pushbuttonpin 6byte botao = 12; 7//auxiliar 8 time variables 9unsigned long t_0; 10unsigned long t_1; 11unsigned long t_aux; 12unsigned 13 int safe_time; 14//Waiting time for a request just after the traffic lights of 15 the cars open 16unsigned int t = 4000; 17 18//matrix of the display of 7 segments 19int 20 disp[10][8] = { { 0, 1, 1, 1, 1, 1, 1, 0 }, // = Digit 0 21 { 0, 1, 0, 0, 1, 0, 22 0, 0 }, // = Digit 1 23 { 0, 0, 1, 1, 1, 1, 0, 1 }, // = Digit 2 24 { 0, 1, 25 1, 0, 1, 1, 0, 1 }, // = Digit 3 26 { 0, 1, 0, 0, 1, 0, 1, 1 }, // = Digit 4 27 28 { 0, 1, 1, 0, 0, 1, 1, 1 }, // = Digit 5 29 { 0, 1, 1, 1, 0, 1, 1, 1 }, // = 30 Digit 6 31 { 0, 1, 0, 0, 1, 1, 0, 0 }, // = Digit 7 32 { 0, 1, 1, 1, 1, 1, 1, 33 1 }, // = Digit 8 34 { 0, 1, 1, 0, 1, 1, 1, 1 } // = Digit 9 35}; 36 37void 38 setup() { 39 40 //PinMode of display pins 41 for (int i = 2; i < 9; i++) 42 43 { 44 pinMode(i, OUTPUT); 45 } 46 //PinMode of leds pins 47 for (int 48 i = A0; i < A5; i++) 49 { 50 pinMode(i, OUTPUT); 51 } 52 ////PinMode of 53 pusbutton pin 54 pinMode(botao, INPUT); 55 t_0 = millis(); 56} 57 58void 59 loop() { 60 61 //Show the S of stop at the beginning 62 number_display(5); 63 64 65 //conting the minimum time that the traffic light will be green for the cars 66 67 t_1 = millis(); 68 if (t_1 - t_0 < t) { 69 safe_time = t_1 - t_0; 70 } 71 72 73 //green sign for cars and red for pedestrians 74 digitalWrite(sinal_carros[2], 75 HIGH); 76 digitalWrite(sinal_pedestre[0], HIGH); 77 //while no one makes a request, 78 the signal will not change. 79 if (digitalRead(botao) == HIGH) 80 { 81 82 83 delay(8000 - safe_time); 84 85 //red light for cars goes out and yellow 86 turns on for 2.5 seconds 87 digitalWrite(sinal_carros[2], LOW); 88 digitalWrite(sinal_carros[1], 89 HIGH); 90 91 delay(2500); 92 93 //Pedestrians sign opens 94 digitalWrite(sinal_carros[1], 95 LOW); 96 digitalWrite(sinal_carros[0], HIGH); 97 digitalWrite(sinal_pedestre[0], 98 LOW); 99 digitalWrite(sinal_pedestre[1], HIGH); 100 101 //Countdown to the 102 pedestrian signal close 103 t_0 = millis(); 104 t_aux = 0; 105 while (t_aux 106 < 5000) { 107 idle_display(); 108 t_aux = millis() - t_0; 109 } 110 111 112 for (int k = 9; k > 0; k--) { 113 number_display(k); 114 115 delay(1000); 116 117 } 118 //the signals are turned off for the cycle to start again 119 digitalWrite(sinal_carros[0], 120 LOW); 121 digitalWrite(sinal_pedestre[1], LOW); 122 123 t_0 = millis(); 124 125 } 126 127} 128//function that shows a number in the 7 segment display 129 130void 131 number_display(int m) { 132 133 for (int j = 2; j < 9; j++) { 134 digitalWrite(j, 135 disp[m][j - 1]); 136 } 137 138} 139 140void idle_display() { 141 turnOf_display(); 142 143 digitalWrite(3, 1); 144 delay(150); 145 digitalWrite(3, 0); 146 digitalWrite(8, 147 1); 148 delay(150); 149 digitalWrite(8, 0); 150 digitalWrite(6, 1); 151 delay(150); 152 153 digitalWrite(6, 0); 154 delay(150); 155 156} 157 158void turnOf_display() { 159 160 for (int i = 2; i < 9; i++) { 161 digitalWrite(i, 0); 162 } 163} 164
Downloadable files
Fritzing
Fritzing
Comments
Only logged in users can leave comments
natalmakers
0 Followers
•0 Projects
Table of contents
Intro
13
0