Introduction
Our group’s project focuses on improving pedestrian crossing safety for the vulnerable populations. A traffic intersection prototype needs to be built for the setting. Therefore, in this tutorial, we will use simple electronic parts to set up a typical traffic intersection including traffic lights and pedestrian crossing signals.
Parts List
In order to accomplish this project, you will need:
- Arduino Uno
- Breadboards
- Jump wires and DuPont wires
- LCD Module Display
Setup and Wiring
- Black wires: GND
- LCD
- GND to GND
- VCC to 5V
- SDA to Pin A4
- SCL to Pin A5
4 Digit 7 Segment Display Reference
The 4 Digit part I purchased came with a shield that helps order the pins in alphabetical order, which greatly enhanced the efficiency. You may want to wire the part as the diagram above if you do not have this feature. For the purpose of saving spaces, I used the 7 Digit Display upside down in this project.
Note that not all digits are used in this project. Using the last two digits L1 and L2 can achieve the goal of counting down from 10 to 0.
Table for 4 Digit 7 Segment Display (Modified)
Workflow Diagram
The diagram explains the sequence of lighting pattern.
Run the Code
Setup the code based on how you wire the Arduino board, the INPUT and OUTPUT pins may be different. Be sure to modify the code accordingly before you run it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
/* Tutorial for building a Mini-intersection * Li Zhuang * The code is modified based on http://wiki.sunfounder.cc/index.php?title=I%C2%B2C_LCD1602 * and https://www.instructables.com/id/Arduino-7-segment-countdown-timer/ * * Basic Arduino Parts required for this project include: * 1. Arduino Traffic Light System Model: or Basic LEDs (Red, Yellow, Green) * 2. LCD Module Display for Arduino Uno * 3. 4-Digit 7-Segment Display Module * 4. Arduino Uno * 5. breadboard * 6. jumpwires (and Du-Pont Wires) * * For the purpose of saving spaces, I used the 7 Digit Display upside down in this project 7 Digit Display Reference for this tutorial: D5 --- C | | E 6 | G | 4 --- B | 2 | F 7 | | 3 --- A8 */ // Define the pins by names. //Traffic Light for the same direction of crossing signal const int Red_PIN = 13; const int Yellow_PIN = 12; const int Green_PIN = 11; //Traffic light for the other direction const int Red_PIN2 = A3; const int Yellow_PIN2 = A2; const int Green_PIN2 = A1; //A timer for the CountDown, to run the for loop 500 times, which turns out as 1 second. int timer = 500; // Include the library code for Liquid Crystal Screen. //Link for the library: http://wiki.sunfounder.cc/index.php?title=I%C2%B2C_LCD1602 #include <Wire.h> #include <LiquidCrystal_I2C.h> // set the LCD address to 0x27 for a 16 chars and 1 line display. LiquidCrystal_I2C lcd(0x27, 16, 1); // Pin 2-8 is connected to the 7 segments of the display. // We only need to count down from 10 to 0 so that only two digit L1 and L2 are used for this tutorial. int pinA = 8; int pinB = 7; int pinC = 6; int pinD = 5; int pinE = 4; int pinF = 3; int pinG = 2; int L1 = 10; int L2 = 9; void setup() { //Traffic Lights //Initialize the digital pins as outputs. pinMode(Red_PIN, OUTPUT); pinMode(Yellow_PIN, OUTPUT); pinMode(Green_PIN, OUTPUT); pinMode(Red_PIN2, OUTPUT); pinMode(Yellow_PIN2, OUTPUT); pinMode(Green_PIN2, OUTPUT); //Liquid Crystal Screen lcd.init(); //initialize the lcd. lcd.backlight(); //open the backlight. //7 Digit Display //Initialize the digital pins as outputs. pinMode(pinA, OUTPUT); pinMode(pinB, OUTPUT); pinMode(pinC, OUTPUT); pinMode(pinD, OUTPUT); pinMode(pinE, OUTPUT); pinMode(pinF, OUTPUT); pinMode(pinG, OUTPUT); pinMode(L1, OUTPUT); pinMode(L2, OUTPUT); } // Define the sequence of the pattern in this mini intersection. // When the computer gets to the end of the loop() function, it starts loop() again. void loop() { delay(500); // wait for 1/2 second to start blinking //One direction starts the traffic flow, the other direction holds the flow. digitalWrite(Green_PIN2, HIGH); // turn the LED Green 2 on digitalWrite(Red_PIN, HIGH); // turn the LED Red on lcd.setCursor(3, 0); // set the cursor to column 3, line 0 lcd.print("DO NOT CROSS"); // Print a message to the LCD delay(6000); // wait for 6 seconds (I shorten the real world traffic flow time for display purpose) digitalWrite(Green_PIN2, LOW); // turn the LED Green 2 off digitalWrite(Red_PIN, LOW); // turn the LED Red off //Transition: yellow lights turn on and off digitalWrite(Yellow_PIN, HIGH); // turn the yellow LED on digitalWrite(Yellow_PIN2, HIGH); // turn the yellow LED 2 on delay(1000); // wait for a second digitalWrite(Yellow_PIN, LOW); // turn the yellow LED off digitalWrite(Yellow_PIN2, LOW); // turn the yellow LED 2 off //Switch Traffic flow //One direction starts the traffic flow, the other direction holds the flow. digitalWrite(Red_PIN2, HIGH); // turn the red LED 2 on digitalWrite(Green_PIN, HIGH); // turn the green LED on //Change pedestrian crossing message //Delete previous message first lcd.setCursor(3, 0); // set the cursor to column 3, line 0 lcd.print(" "); // Print a message to the LCD delay(1); //Print new message lcd.setCursor(6, 0); // set the cursor to column 3, line 0 lcd.print("CROSS"); // Print a message to the LCD delay(5000); //Wait for 5 seconds //Start Count Down for pedestrian crossing CountDown(); //Pedestrian Crossing finish digitalWrite(Red_PIN2, LOW); // turn the red LED 2 off digitalWrite(Green_PIN, LOW); // turn the green LED off digitalWrite(Yellow_PIN, HIGH); // turn the yellow LED on digitalWrite(Yellow_PIN2, HIGH); // turn the yellow LED 2 on delay(1000); // wait for a second digitalWrite(Yellow_PIN, LOW); // turn the yellow LED off digitalWrite(Yellow_PIN2, LOW); // turn the yellow LED 2 off } //Pedestrian Crossing count down for last 10 seconds: from 10 to 0 void CountDown() { for (int i=0; i<timer; i++) { //10 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); //0 digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); delay(1); digitalWrite(L1, LOW); digitalWrite(L2, HIGH); //1 digitalWrite(pinA, HIGH); digitalWrite(pinB, HIGH); digitalWrite(pinC, HIGH); digitalWrite(pinD, HIGH); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); delay(1); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //9 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, HIGH); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //8 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //7 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, HIGH); digitalWrite(pinB, HIGH); digitalWrite(pinC, HIGH); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //6 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //5 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); delay(2); //The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //4 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, HIGH); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, HIGH); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //3 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); digitalWrite(pinC, HIGH); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //2 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, HIGH); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, HIGH); digitalWrite(pinG, LOW); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //1 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, HIGH); digitalWrite(pinB, HIGH); digitalWrite(pinC, HIGH); digitalWrite(pinD, HIGH); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); delay(2); // The for loop, for running the program 500 times. } for (int i=0; i<timer; i++) { //0 digitalWrite(L1, HIGH); digitalWrite(L2, LOW); digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); delay(2); // The for loop, for running the program 500 times. } //Turn off the 7 digit display digitalWrite(L1, LOW); digitalWrite(L2, LOW); delay(2); } |