Traffic Light.
Maker:Somashekar.n
Get this project by agnivohneb (https://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/)
place:Banjarapalya e4d maker specs.
Materials:Arduino
-10 LED´s
--2 red
--2 green
--2 yellow
--2 small red
--2 small green
-- 2 switch
--2 1k resister
--jumper wire
Breadboard
Fix the LED's.
Take red LED fix it to the breadboard and take yellow LED fix it to the breadboard and take green LED fix it to the breadboard as shown in the image.
Fix the Wire and Switch.
Take two wire and switch and fix it to the breadboard . Take two wire one wire fix to the switch to breadboard negative side and another wire fix to the breadboard positive side as shown in the image.
Fix the Wire to the ARDUINO.
First take LED's top part take one jumper wire to fix the red LED to ARDUINO to pin number 4,and take another wire to fix yellow LED to ARDUINO pin number 3,and green LED to ARDUINO pin number 2,and small red LED ARDUINO pin number 7,small green ARDUINO pin number 6.
Take LED's down part red LED ARDUINO pin number 10,yellow LED to ARDUINO pin number 9,green LED to ARDUINO pin number 8,and small red LED to ARDUINO pin number 13,small green LED to ARDUINO pin number 12.as shown image.
Fix the ARDUINO
Take one wire to fix the breadboard positive to ARDUINO pin ground.and take another wire to fix ARDUINO 3.3V to breadboard positive.as shown the image.
ARDUINO PROGRAM.
// Traffic Lights
// By Ben Hovinga http://ben.hovinga.me
/ Lights {GREEN, YELLOW, RED, PEDGREEN, PEDRED}
int light [2][5] = {{2,3,4,6,7},{8,9,10,12,13}};
// Buttons {light[0],light[1]}
int btn [] = {5,11};
// Timers
int tgreen = 30000; // 30s
int tyellow = 3000; // 3s
int tred = 3000; // 3s
int tpedwarn = 5000; // 5s (must be less than tgreen)
int tpedwarnint = 500; // 0.5s (must be less than tpedwarn)
// Other Vals. Don't Change
int btnval = 0;
boolean pedwait [] = {false,false};
boolean pedactive [] = {false,false};
int direct = 0;
int stp = 1;
boolean stpset = false;
unsigned long now = 0;
unsigned long changeat = -1;
unsigned long changeatped = -1;
boolean pedredon = true;
// SETUP
void setup() {
// Assign all Pins
for (int x = 0; x < 2; x++){
for (int y = 0; y < 5; y++){
pinMode(light[x][y], OUTPUT);
}
// Start everything stopped (red)
digitalWrite(light[x][2], HIGH);
digitalWrite(light[x][4], HIGH);
}
// Assign buttons
for (int x = 0; x < 2; x++){
pinMode(btn[x], INPUT);
}
}
// RUN
void loop() {
// Update Time
now = millis();
// Check if button pressed
for (int x = 0; x < 2; x ++){
btnval = digitalRead(btn[x]);
if (btnval == HIGH){
pedwait[x] = true; // We have someone waiting to cross
}
}
// Get into it
switch (stp){
// Green
case 1:
// Setup
if (stpset == false){
changeat = now + tgreen - tpedwarn; // Set timer
// Turn on Ped
if (pedwait[direct] == true){
// Lights change for ped
digitalWrite(light[direct][3], HIGH);
digitalWrite(light[direct][4], LOW);
// Set some vars
pedwait[direct] = false;
pedactive[direct] = true;
pedredon = false;
}
// Turn on Green
digitalWrite(light[direct][0], HIGH);
// Turn off Red
digitalWrite(light[direct][2], LOW);
stpset = true; // We are setup
}
// Run
else{
if (now > changeat){ // Times up
if (pedactive[direct] == true){
// Turn off Ped
digitalWrite(light[direct][3], LOW);
digitalWrite(light[direct][4], HIGH);
pedredon = true;
}
// Next step
stp++;
stpset = false;
}
}
break;
// Warn ped (if possible)
case 2:
// Setup
if (stpset == false){
changeat = now + tpedwarn;
changeatped = now + tpedwarnint;
stpset = true;
}
// Run
else{
// Flash Ped Red
if (pedactive[direct] == true){
if (pedredon == true && changeatped < now){
digitalWrite(light[direct][4], LOW);
pedredon = false;
changeatped = now + tpedwarnint;
}
if (pedredon == false && changeatped < now){
digitalWrite(light[direct][4], HIGH);
pedredon = true;
changeatped = now + tpedwarnint;
}
}
if (now > changeat){ // Times up
// Turn off
digitalWrite(light[direct][0], LOW);
digitalWrite(light[direct][4], HIGH);
pedredon = true;
pedactive[direct] = false;
// Next step
stp++;
stpset = false;
}
}
break;
// Yellow
case 3:
// Setup
if (stpset == false){
changeat = now + tyellow;
digitalWrite(light[direct][1], HIGH);
stpset = true;
}
// Run
else{
if (now > changeat){ // Times up
// Turn off
digitalWrite(light[direct][1], LOW);
// Next step
stp++;
stpset = false;
}
}
break;
// Red
case 4:
// Setup
if (stpset == false){
changeat = now + tred;
digitalWrite(light[direct][2], HIGH);
stpset = true;
}
// Run
else{
if (now > changeat){ // Times up
// Start over
stp = 1;
stpset = false;
// Change Direction
if (direct == 1){
direct = 0;
}
else {
direct = 1;
}
}
}
break;
}
}