For this tutorial, you’ll need:
- 2 x Arduino UNO
- 9 x Resistor 330E
- 9 x LED 5MM
- Jumper Wires
- 3x Mini BreadBoard
- 1 x Dip Switch
- 1 x Resistor 10K
Please note that the items needed will be updated with each part!
First Part: Explaining the project & Figure Out How to Know if a Led is Really ON?
Test if a LED is ON:
define led 8 define feedback 7 int i =0; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(led, OUTPUT); pinMode(feedback, INPUT); digitalWrite(led, LOW); } void loop() { // put your main code here, to run repeatedly: digitalWrite(led, HIGH); if (digitalRead(feedback) == 1) { Serial.println("The led is ON"); } else { Serial.println("The led is damaged"); } delay(500); }
Second Part: Simple Order Without Sensor Feedback
Simple Order Without Sensor Feedback Code:
define TLAR 3 // RED Traffic Light for Road A define TLAY 4 // Yellow Traffic Light for Road A define TLAG 5 // Green Traffic Light for Road A define TLBCR 6 // RED Traffic Light for Road B & Road C define TLBCY 7 // Yellow Traffic Light for Road B & Road C define TLBCG 8 // Green Traffic Light for Road B & Road C define TLDR 9 // RED Traffic Light for Road D define TLDY 10 // Yellow Traffic Light for Road D define TLDG 11 // Green Traffic Light for Road D define TrafficLedRoadA 1 //This is the reference number for the traffic lights of road A define TrafficLedRoadBC 2 //This is the reference number for the traffic lights of road B and C define TrafficLedRoadD 3 //This is the reference number for the traffic lights of road D define IntinialState 4 //This is the reference number for the initial State of the system define red 10 //This is the reference number for the red color define green 20 //This is the reference number for the green color define yellow 30 //This is the reference number for the yellow color define IntinialStateColor 50 //This is the reference number of the initiale state color lighted when the system in UP for the first time define ERRORLIGHTS 404 //This is the reference number for the error State define ERRORLIGHTSTIMEREPEAT 3 //This is the number of time the yellow lights will blink when there is an error define ERRORLIGHTSTIME 500 //This is the delay time between each ON & OFF for the error lights define WaitingTime 9000 //9 Seconds This is the time of the the red light will be ON or the GREEN light will be ON. define GreenTime 7000 //7 Seconds is the green time (of the 9 total seconds) define YellowTime 2000 //2 Seconds is the yellow time (of the 9 total seconds) define SecurityTime 1000 //1 Second is the time to switch the green light to another traffic light. It's when all teh traffic lights are RED ON. For security purposes int turn=IntinialState; //This variable is used to pass between the turns of the traffic lights void turnTrafficLedON (int TrafficLedNumber, int colorNumber); // This function will light the specified color based on the traffic light given /* Example: turnTrafficLedON(1, 10); This will trun RED light ON in TrafficLedRoadA and turn Yellow and Green Lights OFF on the same TrafficLedRoadA */ //the turn is ROAD D, ROAD A, ROAD B & C void setup() { // put your setup code here, to run once: //Define all the traffic lights as output - till now we don't have an input in this program pinMode(TLAR, OUTPUT); pinMode(TLAY, OUTPUT); pinMode(TLAG, OUTPUT); pinMode(TLBCR, OUTPUT); pinMode(TLBCY, OUTPUT); pinMode(TLBCG, OUTPUT); pinMode(TLDR, OUTPUT); pinMode(TLDY, OUTPUT); pinMode(TLDG, OUTPUT); //Turn OFF all the outputs digitalWrite(TLAG, LOW); digitalWrite(TLAY, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLDG, LOW); digitalWrite(TLDY, LOW); digitalWrite(TLDR, LOW); //start the serial communication for debugging purposes if needed // Serial.begin(9600); // Serial.println("Serial Communication started"); } void loop() { // put your main code here, to run repeatedly: switch (turn) { case IntinialState:turnTrafficLedON(IntinialState, IntinialStateColor); delay(WaitingTime); turn = TrafficLedRoadD; break; case TrafficLedRoadD: turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, green); delay(GreenTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, yellow); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); delay(SecurityTime); turn = TrafficLedRoadA; break; case TrafficLedRoadA: turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadA, green); delay(GreenTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadA, yellow); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); delay(SecurityTime); turn = TrafficLedRoadBC; break; case TrafficLedRoadBC: turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadBC, green); delay(GreenTime); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadBC, yellow); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); delay(SecurityTime); turn = TrafficLedRoadD; break; default: turnTrafficLedON(ERRORLIGHTS, ERRORLIGHTS); break;
} } //define the work of the functions declared in the beginning of the sketch void turnTrafficLedON (int TrafficLedNumber, int color) { //InitialSate if (TrafficLedNumber == IntinialState && color == IntinialStateColor) // The system is Up for the first time. { //All the Traffic Leds must be RED and the rest colors OFF//Turn Red Lights ON for all the traffic lights digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //Turn Yellow Lights OFF for all the traffic Lights digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); //Turn Green Lights OFF for all the traffic Lights digitalWrite(TLAG, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLDG, LOW);
} //RED if (TrafficLedNumber == TrafficLedRoadA && color == red) // The RED traffic lights on RoadA must be turned ON { digitalWrite(TLAG, LOW); digitalWrite(TLAY, LOW); digitalWrite(TLAR, HIGH); }if (TrafficLedNumber == TrafficLedRoadBC && color == red) // The RED traffic lights on RoadB & Road C must be turned ON
{ digitalWrite(TLBCG, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, HIGH); }if (TrafficLedNumber == TrafficLedRoadD && color == red) // The RED traffic lights on RoadD must be turned ON
{ digitalWrite(TLDG, LOW); digitalWrite(TLDY, LOW); digitalWrite(TLDR, HIGH); } //YELLOW if (TrafficLedNumber == TrafficLedRoadA && color == yellow) // The YELLOW traffic lights on RoadA must be turned ON { digitalWrite(TLAG, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLAY, HIGH); }if (TrafficLedNumber == TrafficLedRoadBC && color == yellow) // The YELLOW traffic lights on RoadB & Road C must be turned ON
{ digitalWrite(TLBCG, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLBCY, HIGH); }if (TrafficLedNumber == TrafficLedRoadD && color == yellow) // The YELLOW traffic lights on RoadD must be turned ON
{ digitalWrite(TLDG, LOW); digitalWrite(TLDR, LOW); digitalWrite(TLDY, HIGH); } //GREENif (TrafficLedNumber == TrafficLedRoadA && color == green) // The GREEN traffic lights on RoadA must be turned ON
{ digitalWrite(TLAY, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLAG, HIGH); }if (TrafficLedNumber == TrafficLedRoadBC && color == green) // The GREEN traffic lights on RoadB & Road C must be turned ON
{ digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLBCG, HIGH); }if (TrafficLedNumber == TrafficLedRoadD && color == green) // The GREEN traffic lights on RoadD must be turned ON
{ digitalWrite(TLDY, LOW); digitalWrite(TLDR, LOW); digitalWrite(TLDG, HIGH); } //ERROR VALUES if (TrafficLedNumber == ERRORLIGHTS && color == ERRORLIGHTS) // ERROR!! The YELLOW LIGHTS will Blink for 3 times { //Make sure all the LIGHTS are OFF//YELLOW OFF digitalWrite(TLAY, HIGH); digitalWrite(TLBCY, HIGH); digitalWrite(TLDY, HIGH); //RED OFF digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //GREEN OFF digitalWrite(TLAG, HIGH); digitalWrite(TLBCG, HIGH); digitalWrite(TLDG, HIGH); for (int i=0; i<ERRORLIGHTSTIMEREPEAT; i++) //BLINK The YELLOW LIGHTS indicating an error { digitalWrite(TLAY, HIGH); digitalWrite(TLBCY, HIGH); digitalWrite(TLDY, HIGH); delay(ERRORLIGHTSTIME); digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); }
} }
Third Part: Add a Control Room
Traffic Light Arduino Code:
#include <SoftwareSerial.h> //define RX and TX pins define RX A0 define TX A1 //define the codes to be sent define HelloMessage 50 //This is sent when the arduino is ON //define codes: define S0 1 //Initial State define S1 2 //Green Traffic Light for Road D & Red Traffic Light for Road BC & A define S2 3 //Yellow Traffic Light for Road D & Red Traffic Light for Road BC & A define S3 4 //Red Traffic Light for Road D & Red Traffic Light for Road BC & A define S4 5 //Green Traffic Light for Road A & Red Traffic Light for Road BC & D define S5 6 //Yellow Traffic Light for Road A & Red Traffic Light for Road BC & D define S6 7 //Red Traffic Light for Road A & Red Traffic Light for Road BC & D define S7 8 //Green Traffic Light for Road BC & Red Traffic Light for Road D & A define S8 9 //Yellow Traffic Light for Road BC & Red Traffic Light for Road D & A define S9 10 //Red Traffic Light for Road BC & Red Traffic Light for Road D & A define SE 100 //Error State //define the software serial: S using RX (A0) and TX (A1) SoftwareSerial s(RX,TX); //(RX = A0, TX= A1) define TLAR 3 // RED Traffic Light for Road A define TLAY 4 // Yellow Traffic Light for Road A define TLAG 5 // Green Traffic Light for Road A define TLBCR 6 // RED Traffic Light for Road B & Road C define TLBCY 7 // Yellow Traffic Light for Road B & Road C define TLBCG 8 // Green Traffic Light for Road B & Road C define TLDR 9 // RED Traffic Light for Road D define TLDY 10 // Yellow Traffic Light for Road D define TLDG 11 // Green Traffic Light for Road D define TrafficLedRoadA 1 //This is the reference number for the traffic lights of road A define TrafficLedRoadBC 2 //This is the reference number for the traffic lights of road B and C define TrafficLedRoadD 3 //This is the reference number for the traffic lights of road D define IntinialState 4 //This is the reference number for the initial State of the system define red 10 //This is the reference number for the red color define green 20 //This is the reference number for the green color define yellow 30 //This is the reference number for the yellow color define IntinialStateColor 50 //This is the reference number of the initiale state color lighted when the system in UP for the first time define ERRORLIGHTS 404 //This is the reference number for the error State define ERRORLIGHTSTIMEREPEAT 3 //This is the number of time the yellow lights will blink when there is an error define ERRORLIGHTSTIME 500 //This is the delay time between each ON & OFF for the error lights define WaitingTime 15000 //15 Seconds This is the time of the the red light will be ON or the GREEN light will be ON. define GreenTime 10000 //10 Seconds is the green time (of the 9 total seconds) define YellowTime 5000 //5 Seconds is the yellow time (of the 9 total seconds) define SecurityTime 5000 //1 Second is the time to switch the green light to another traffic light. It's when all teh traffic lights are RED ON. For security purposes int turn=IntinialState; //This variable is used to pass between the turns of the traffic lights void turnTrafficLedON (int TrafficLedNumber, int colorNumber); // This function will light the specified color based on the traffic light given /* Example: turnTrafficLedON(1, 10); This will trun RED light ON in TrafficLedRoadA and turn Yellow and Green Lights OFF on the same TrafficLedRoadA */ //the turn is ROAD D, ROAD A, ROAD B & C void setup() { // put your setup code here, to run once: s.begin(9600); //start the serial connection delay(100); //wait 100 ms to give it time s.write(HelloMessage); //send a Hello Message to the control Room. delay(100); //Define all the traffic lights as output - till now we don't have an input in this program pinMode(TLAR, OUTPUT); pinMode(TLAY, OUTPUT); pinMode(TLAG, OUTPUT); pinMode(TLBCR, OUTPUT); pinMode(TLBCY, OUTPUT); pinMode(TLBCG, OUTPUT); pinMode(TLDR, OUTPUT); pinMode(TLDY, OUTPUT); pinMode(TLDG, OUTPUT); //Turn OFF all the outputs digitalWrite(TLAG, LOW); digitalWrite(TLAY, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLDG, LOW); digitalWrite(TLDY, LOW); digitalWrite(TLDR, LOW); //start the serial communication for debugging purposes if needed // Serial.begin(9600); // Serial.println("Serial Communication started"); } void loop() { // put your main code here, to run repeatedly: switch (turn) { case IntinialState:turnTrafficLedON(IntinialState, IntinialStateColor); s.write(S0); delay(WaitingTime); turn = TrafficLedRoadD; break; case TrafficLedRoadD: turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, green); s.write(S1); delay(GreenTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, yellow); s.write(S2); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); s.write(S3); delay(SecurityTime); turn = TrafficLedRoadA; break; case TrafficLedRoadA: turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadA, green); s.write(S4); delay(GreenTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadA, yellow); s.write(S5); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); s.write(S6); delay(SecurityTime); turn = TrafficLedRoadBC; break; case TrafficLedRoadBC: turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadBC, green); s.write(S7); delay(GreenTime); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadBC, yellow); s.write(S8); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); s.write(S9); delay(SecurityTime); turn = TrafficLedRoadD; break; default: turnTrafficLedON(ERRORLIGHTS, ERRORLIGHTS); s.write(SE); break;
} } //define the work of the functions declared in the beginning of the sketch void turnTrafficLedON (int TrafficLedNumber, int color) { //InitialSate if (TrafficLedNumber == IntinialState && color == IntinialStateColor) // The system is Up for the first time. { //All the Traffic Leds must be RED and the rest colors OFF//Turn Red Lights ON for all the traffic lights digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //Turn Yellow Lights OFF for all the traffic Lights digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); //Turn Green Lights OFF for all the traffic Lights digitalWrite(TLAG, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLDG, LOW);
} //RED if (TrafficLedNumber == TrafficLedRoadA && color == red) // The RED traffic lights on RoadA must be turned ON { digitalWrite(TLAG, LOW); digitalWrite(TLAY, LOW); digitalWrite(TLAR, HIGH); }if (TrafficLedNumber == TrafficLedRoadBC && color == red) // The RED traffic lights on RoadB & Road C must be turned ON
{ digitalWrite(TLBCG, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, HIGH); }if (TrafficLedNumber == TrafficLedRoadD && color == red) // The RED traffic lights on RoadD must be turned ON
{ digitalWrite(TLDG, LOW); digitalWrite(TLDY, LOW); digitalWrite(TLDR, HIGH); } //YELLOW if (TrafficLedNumber == TrafficLedRoadA && color == yellow) // The YELLOW traffic lights on RoadA must be turned ON { digitalWrite(TLAG, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLAY, HIGH); }if (TrafficLedNumber == TrafficLedRoadBC && color == yellow) // The YELLOW traffic lights on RoadB & Road C must be turned ON
{ digitalWrite(TLBCG, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLBCY, HIGH); }if (TrafficLedNumber == TrafficLedRoadD && color == yellow) // The YELLOW traffic lights on RoadD must be turned ON
{ digitalWrite(TLDG, LOW); digitalWrite(TLDR, LOW); digitalWrite(TLDY, HIGH); } //GREENif (TrafficLedNumber == TrafficLedRoadA && color == green) // The GREEN traffic lights on RoadA must be turned ON
{ digitalWrite(TLAY, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLAG, HIGH); }if (TrafficLedNumber == TrafficLedRoadBC && color == green) // The GREEN traffic lights on RoadB & Road C must be turned ON
{ digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLBCG, HIGH); }if (TrafficLedNumber == TrafficLedRoadD && color == green) // The GREEN traffic lights on RoadD must be turned ON
{ digitalWrite(TLDY, LOW); digitalWrite(TLDR, LOW); digitalWrite(TLDG, HIGH); } //ERROR VALUES if (TrafficLedNumber == ERRORLIGHTS && color == ERRORLIGHTS) // ERROR!! The YELLOW LIGHTS will Blink for 3 times { //Make sure all the LIGHTS are OFF//YELLOW OFF digitalWrite(TLAY, HIGH); digitalWrite(TLBCY, HIGH); digitalWrite(TLDY, HIGH); //RED OFF digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //GREEN OFF digitalWrite(TLAG, HIGH); digitalWrite(TLBCG, HIGH); digitalWrite(TLDG, HIGH); for (int i=0; i<ERRORLIGHTSTIMEREPEAT; i++) //BLINK The YELLOW LIGHTS indicating an error { digitalWrite(TLAY, HIGH); digitalWrite(TLBCY, HIGH); digitalWrite(TLDY, HIGH); delay(ERRORLIGHTSTIME); digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); }
} }
Monitoring Room Arduino Code:
#include <SoftwareSerial.h> //define RX and TX pins define RX A1 define TX A0 //define the codes to be sent define HelloMessage 50 //This is sent when the arduino is ON //define codes: define S0 1 //Initial State define S1 2 //Green Traffic Light for Road D & Red Traffic Light for Road BC & A define S2 3 //Yellow Traffic Light for Road D & Red Traffic Light for Road BC & A define S3 4 //Red Traffic Light for Road D & Red Traffic Light for Road BC & A define S4 5 //Green Traffic Light for Road A & Red Traffic Light for Road BC & D define S5 6 //Yellow Traffic Light for Road A & Red Traffic Light for Road BC & D define S6 7 //Red Traffic Light for Road A & Red Traffic Light for Road BC & D define S7 8 //Green Traffic Light for Road BC & Red Traffic Light for Road D & A define S8 9 //Yellow Traffic Light for Road BC & Red Traffic Light for Road D & A define S9 10 //Red Traffic Light for Road BC & Red Traffic Light for Road D & A define SE 100 //Error State int HelloMessageStep1 = 0; void checkdata(int ); //Function to check the received data //define the software serial: S using RX (A0) and TX (A1) SoftwareSerial s(RX,TX); //(RX = A1, TX= A0) void setup() { // put your setup code here, to run once: Serial.begin(9600); s.begin(9600); //start the serial connection delay(100); //wait 100 ms to give it time while(HelloMessageStep1 == 0) { if(s.available()>0) { checkdata(s.read()); HelloMessageStep1 = 1; } } } void loop() { // put your main code here, to run repeatedly:if(s.available()>0) { checkdata(s.read()); }
} void checkdata(int data) //Function to check the received data { switch (data) { case 1: Serial.println("Initial State"); break;case 2: Serial.println(" "); Serial.println("State 1:"); Serial.println("Road D Green"); Serial.println("Road BC RED"); Serial.println("Road A RED"); Serial.println(" "); break; case 3: Serial.println(" "); Serial.println("State 2:"); Serial.println("Road D Yellow"); Serial.println("Road BC RED"); Serial.println("Road A RED"); Serial.println(" "); break; case 4: Serial.println(" "); Serial.println("State 3:"); Serial.println("Road D RED"); Serial.println("Road BC RED"); Serial.println("Road A RED"); Serial.println(" "); break; case 5: Serial.println(" "); Serial.println("State 4:"); Serial.println("Road A Green"); Serial.println("Road D RED"); Serial.println("Road BC RED"); Serial.println(" "); break; case 6: Serial.println(" "); Serial.println("State 5:"); Serial.println("Road A Yellow"); Serial.println("Road D RED"); Serial.println("Road BC RED"); Serial.println(" "); break; case 7: Serial.println(" "); Serial.println("State 6:"); Serial.println("Road A RED"); Serial.println("Road D RED"); Serial.println("Road BC RED"); Serial.println(" "); break; case 8: Serial.println(" "); Serial.println("State 7:"); Serial.println("Road BC Green"); Serial.println("Road A RED"); Serial.println("Road D RED"); Serial.println(" "); break; case 9: Serial.println(" "); Serial.println("State 8:"); Serial.println("Road BC Yellow"); Serial.println("Road A RED"); Serial.println("Road D RED"); Serial.println(" "); break; case 10: Serial.println(" "); Serial.println("State 9:"); Serial.println("Road BC RED"); Serial.println("Road A RED"); Serial.println("Road D RED"); Serial.println(" "); break; case 50: Serial.println(" "); Serial.println("Hello Message Received, System is UP"); Serial.println(" "); break; case 100: Serial.println(" "); Serial.println("ERROR ERROR ERROR ERROR"); Serial.println(" "); break;
} }
Fourth Part: Add Sensor for Road D
#include <SoftwareSerial.h> //define RX and TX pins #define RX A0 #define TX A1 //define the codes to be sent #define HelloMessage 50 //This is sent when the arduino is ON //define codes: #define S0 1 //Initial State #define S1 2 //Green Traffic Light for Road D & Red Traffic Light for Road BC & A #define S2 3 //Yellow Traffic Light for Road D & Red Traffic Light for Road BC & A #define S3 4 //Red Traffic Light for Road D & Red Traffic Light for Road BC & A #define S4 5 //Green Traffic Light for Road A & Red Traffic Light for Road BC & D #define S5 6 //Yellow Traffic Light for Road A & Red Traffic Light for Road BC & D #define S6 7 //Red Traffic Light for Road A & Red Traffic Light for Road BC & D #define S7 8 //Green Traffic Light for Road BC & Red Traffic Light for Road D & A #define S8 9 //Yellow Traffic Light for Road BC & Red Traffic Light for Road D & A #define S9 10 //Red Traffic Light for Road BC & Red Traffic Light for Road D & A #define SE 100 //Error State //define the software serial: S using RX (A0) and TX (A1) SoftwareSerial s(RX,TX); //(RX = A0, TX= A1) #define TLAR 3 // RED Traffic Light for Road A #define TLAY 4 // Yellow Traffic Light for Road A #define TLAG 5 // Green Traffic Light for Road A #define TLBCR 6 // RED Traffic Light for Road B & Road C #define TLBCY 7 // Yellow Traffic Light for Road B & Road C #define TLBCG 8 // Green Traffic Light for Road B & Road C #define TLDR 9 // RED Traffic Light for Road D #define TLDY 10 // Yellow Traffic Light for Road D #define TLDG 11 // Green Traffic Light for Road D #define SensorRoadD 12 //The sensor of ROAD D is Connected to the pin number 12 #define TrafficLedRoadA 1 //This is the reference number for the traffic lights of road A #define TrafficLedRoadBC 2 //This is the reference number for the traffic lights of road B and C #define TrafficLedRoadD 3 //This is the reference number for the traffic lights of road D #define IntinialState 4 //This is the reference number for the initial State of the system #define red 10 //This is the reference number for the red color #define green 20 //This is the reference number for the green color #define yellow 30 //This is the reference number for the yellow color #define IntinialStateColor 50 //This is the reference number of the initiale state color lighted when the system in UP for the first time #define ERRORLIGHTS 404 //This is the reference number for the error State #define ERRORLIGHTSTIMEREPEAT 3 //This is the number of time the yellow lights will blink when there is an error #define ERRORLIGHTSTIME 500 //This is the delay time between each ON & OFF for the error lights #define WaitingTime 15000 //15 Seconds This is the time of the the red light will be ON or the GREEN light will be ON. #define GreenTime 10000 //10 Seconds is the green time (of the 9 total seconds) #define YellowTime 5000 //5 Seconds is the yellow time (of the 9 total seconds) #define SecurityTime 5000 //1 Second is the time to switch the green light to another traffic light. It's when all teh traffic lights are RED ON. For security purposes int turn=IntinialState; //This variable is used to pass between the turns of the traffic lights void turnTrafficLedON (int TrafficLedNumber, int colorNumber); // This function will light the specified color based on the traffic light given /* Example: turnTrafficLedON(1, 10); This will trun RED light ON in TrafficLedRoadA and turn Yellow and Green Lights OFF on the same TrafficLedRoadA */ //the turn is ROAD D, ROAD A, ROAD B & C int isThereAnyoneOnRoadD(); int PreviousTurn = TrafficLedRoadA; //A variable to store the previous trun void setup() { // put your setup code here, to run once: s.begin(9600); //start the serial connection delay(100); //wait 100 ms to give it time s.write(HelloMessage); //send a Hello Message to the control Room. delay(100); //Define all the traffic lights as output - till now we don't have an input in this program pinMode(TLAR, OUTPUT); pinMode(TLAY, OUTPUT); pinMode(TLAG, OUTPUT); pinMode(TLBCR, OUTPUT); pinMode(TLBCY, OUTPUT); pinMode(TLBCG, OUTPUT); pinMode(TLDR, OUTPUT); pinMode(TLDY, OUTPUT); pinMode(TLDG, OUTPUT); pinMode(SensorRoadD, INPUT); //Turn OFF all the outputs digitalWrite(TLAG, LOW); digitalWrite(TLAY, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLDG, LOW); digitalWrite(TLDY, LOW); digitalWrite(TLDR, LOW); //start the serial communication for debugging purposes if needed // Serial.begin(9600); // Serial.println("Serial Communication started"); } void loop() { // put your main code here, to run repeatedly: switch (turn) { case IntinialState: turnTrafficLedON(IntinialState, IntinialStateColor); s.write(S0); delay(WaitingTime); turn = TrafficLedRoadD; break; case TrafficLedRoadD: turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, green); s.write(S1); delay(GreenTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, yellow); s.write(S2); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); s.write(S3); delay(SecurityTime); turn = PreviousTurn; break; case TrafficLedRoadA: if(isThereAnyoneOnRoadD()==1) { PreviousTurn = TrafficLedRoadA; turn = TrafficLedRoadD; break; } else { turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadA, green); s.write(S4); delay(GreenTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadA, yellow); s.write(S5); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); s.write(S6); delay(SecurityTime); turn = TrafficLedRoadBC; break; } case TrafficLedRoadBC: if(isThereAnyoneOnRoadD()==1) { PreviousTurn = TrafficLedRoadBC; turn = TrafficLedRoadD; break; } else { turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadBC, green); s.write(S7); delay(GreenTime); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); turnTrafficLedON(TrafficLedRoadBC, yellow); s.write(S8); delay(YellowTime); turnTrafficLedON(TrafficLedRoadBC, red); turnTrafficLedON(TrafficLedRoadA, red); turnTrafficLedON(TrafficLedRoadD, red); s.write(S9); delay(SecurityTime); turn = TrafficLedRoadA; break; } default: turnTrafficLedON(ERRORLIGHTS, ERRORLIGHTS); s.write(SE); break; } } //define the work of the functions declared in the beginning of the sketch void turnTrafficLedON (int TrafficLedNumber, int color) { //InitialSate if (TrafficLedNumber == IntinialState && color == IntinialStateColor) // The system is Up for the first time. { //All the Traffic Leds must be RED and the rest colors OFF //Turn Red Lights ON for all the traffic lights digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //Turn Yellow Lights OFF for all the traffic Lights digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); //Turn Green Lights OFF for all the traffic Lights digitalWrite(TLAG, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLDG, LOW); } //RED if (TrafficLedNumber == TrafficLedRoadA && color == red) // The RED traffic lights on RoadA must be turned ON { digitalWrite(TLAG, LOW); digitalWrite(TLAY, LOW); digitalWrite(TLAR, HIGH); } if (TrafficLedNumber == TrafficLedRoadBC && color == red) // The RED traffic lights on RoadB & Road C must be turned ON { digitalWrite(TLBCG, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, HIGH); } if (TrafficLedNumber == TrafficLedRoadD && color == red) // The RED traffic lights on RoadD must be turned ON { digitalWrite(TLDG, LOW); digitalWrite(TLDY, LOW); digitalWrite(TLDR, HIGH); } //YELLOW if (TrafficLedNumber == TrafficLedRoadA && color == yellow) // The YELLOW traffic lights on RoadA must be turned ON { digitalWrite(TLAG, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLAY, HIGH); } if (TrafficLedNumber == TrafficLedRoadBC && color == yellow) // The YELLOW traffic lights on RoadB & Road C must be turned ON { digitalWrite(TLBCG, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLBCY, HIGH); } if (TrafficLedNumber == TrafficLedRoadD && color == yellow) // The YELLOW traffic lights on RoadD must be turned ON { digitalWrite(TLDG, LOW); digitalWrite(TLDR, LOW); digitalWrite(TLDY, HIGH); } //GREEN if (TrafficLedNumber == TrafficLedRoadA && color == green) // The GREEN traffic lights on RoadA must be turned ON { digitalWrite(TLAY, LOW); digitalWrite(TLAR, LOW); digitalWrite(TLAG, HIGH); } if (TrafficLedNumber == TrafficLedRoadBC && color == green) // The GREEN traffic lights on RoadB & Road C must be turned ON { digitalWrite(TLBCY, LOW); digitalWrite(TLBCR, LOW); digitalWrite(TLBCG, HIGH); } if (TrafficLedNumber == TrafficLedRoadD && color == green) // The GREEN traffic lights on RoadD must be turned ON { digitalWrite(TLDY, LOW); digitalWrite(TLDR, LOW); digitalWrite(TLDG, HIGH); } //ERROR VALUES if (TrafficLedNumber == ERRORLIGHTS && color == ERRORLIGHTS) // ERROR!! The YELLOW LIGHTS will Blink for 3 times { //Make sure all the LIGHTS are OFF //YELLOW OFF digitalWrite(TLAY, HIGH); digitalWrite(TLBCY, HIGH); digitalWrite(TLDY, HIGH); //RED OFF digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //GREEN OFF digitalWrite(TLAG, HIGH); digitalWrite(TLBCG, HIGH); digitalWrite(TLDG, HIGH); for (int i=0; i<ERRORLIGHTSTIMEREPEAT; i++) //BLINK The YELLOW LIGHTS indicating an error { digitalWrite(TLAY, HIGH); digitalWrite(TLBCY, HIGH); digitalWrite(TLDY, HIGH); delay(ERRORLIGHTSTIME); digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); } } } int isThereAnyoneOnRoadD() { if (digitalRead(SensorRoadD) == 1) { return 1; } else { return 0; } }