# Traffic Lights Controler with IR Remote
###### tags: `Traffic light` `light` `LEDs` `IR remote` `NewTone` `DSNA`
[ToC]
> Since watching Italian Job Movie an I dreamed to get control over Traffic lights systems.
> Here com one with IR Remote , the next is via Wifi /Cloud
> Unfortunatly I got to drop the code for the Oled Screen on I2C du tyo lak of memory in my UNO so you can well test it on MEga as I'll do.
## :memo: about The Project :
### Step 1: What is this Easy Thing:
Of course it sound and looks easy,but the logic is matter of complex!
Lights that turn on and off according to the traffic needs are not easy to control.



:rocket:
### Step 2: Easy to connect ?
I belive so for one Trafic Light but for 4 is not so easy with usual component.

here com the ¤ Ways Cross with Some Wire spaghetti.


{%youtube sQpUCpalooI %}
### Step 3: Sharing is caring here come 1.3K Lines code!
```
// File: TraficLightCross4Direction
// Summary: This Program is a simulation for Trafic Light in 3 or 4 ways ( Full Cross)
// We Can run this program using IR Remote to run it in 3 Different ways :
//1) Test the lights of trafic Lights.
//2) Strat Trafics rutines that start from A to all other exits , C to All Exites , F to all
//Exits , and H to all Exits
//3) Italian Job :) , at open and close as u like!
//The cod run also with Arduino Mega so we have Better Memory that why i Grey out the related code to Oled Screen!
//
// We consider trafic as North , West South , East like this :
// A NORTH I
// | |
// | | ^
// 2--> V V 1 |
// B------<-----|-----------|-----<----H
// | |
//WEST ^ | | EAST
// |3 | <--4 |
// C---->-------|-----------|----->----G
// V ^
// | |
// D South F
//
// So :
// TraficLight#1 control I->F ( South to North) Red stop , Green Go!
// TraficLight#2 control H->B (EAST to WEST) Red stop , Green Go!
// TraficLight#3 control A->D ( North to South) Red stop , Green Go!
// TraficLight#3 control C->G ( West to EAST ) Red stop , Green Go!
// The T Cross :
//
//
// A NORTH I
// | |
// | | ^
// 2--> V V 1 |
// B------<-----|-----------|-----<----H
// | |
//WEST | | EAST
// | <--4 |
// C---->-------|-----------|----->----G
//--------------------------------------
// Table of directions :
//------------|-------------||----------------|
// | 4 Cross || T Cross |
//--------------------------------------------|
//Direction | GREEN | RED || Green | RED |
//--------------------------------------------|
//S2N =N2S |3+1 |4+2 || NOT Applicabale|
//F2I A2D | | || |
//--------------------------||----------------|
//N2W |3+1+2 |4 || 3+4 | 2 |
//A2B | | || | |
//--------------------------------------------|
//N2E |3 |4+1+2|| 3 |2+4 |
//A2G | | || | |
//--------------------------------------------|
//S2W |1 |4+3+2 ||NOT Applicabale |
//F2B | | || | |
//--------------------------------------------|
//S2E |3+1+2 |4 ||NOT Applicabale |
//F2G | | || | |
//--------------------------------------------|
//E2W =W2E |4+2 |1+3 || 2+4 |3 |
//H2B C2G | | || |
//--------------------------------------------|
//W2N |4 |1+2+3 || 4+3 |2 |
//C2I | | || | |
//--------------------------------------------|
//E2N |2+3+4 |1 || 2+3+4 |NT |
//H2I | | || | |
//--------------------------------------------|
//W2S |4+2+1 |3 || NOT Applicabale|
//C2D | | || | |
//--------------------------------------------|
//E2S |2 |1+4+3 || NOT Applicabale|
//H2D | | || | |
//--------------------------------------------|
// Version: 1.0
// Owner: Ammar alnahhas DSNA22
// date : 2020-11-15
// ---------------------------------------------------------------
// update : : 2020-11-17 : Show results on i2C OLED screen
// //the Memorey of My UNO Was not enoguh for the screen so i keep the
// Code if you liketo test on Omega!
//---------------------------------------------------------------
// Next Upgrade : is to mange it from Mobile phone instead of IR Remote
// GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD LUCKKKKKKKKKKKKKKK
// Inti. Oled Screen :
/*#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>*/
/*///////////////OLED/////////////////////
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);*/
#include <IRremote.h> // include the IRremote library
#include <NewTone.h> // The Norma Tone has conflict with IRemote
//////////////////////IR REMOTE///////////////////////////////////////
#define RECEIVER_PIN 12 // define the IR receiver pin
IRrecv receiver(RECEIVER_PIN); // create a receiver object of the IRrecv class
decode_results results; // create a results object of the decode_results class
///////////////////////////////////////////////////////////////////////
///////////Trafic Lights Pins//////////////////////
// The Trafic Lights pins
// Trafic Light #1
#define TL1R 2 // Connected to Analog A1
#define TL1O 3
#define TL1G 4 // Connected to Analog A2
// Trafic Light #2
#define TL2R 5 // Connected to PIN#A3
#define TL2O 6
#define TL2G 7 //Connected to PIN#A4
// Trafic Light #3
#define TL3R 9 // Connected to PIN#9
#define TL3O 10
#define TL3G 11 // Connected to PIN#13
// Trafic Light #4
#define TL4R A0 // Connected to PIN#6
#define TL4O A1
#define TL4G A2 // Connected to PIN#5
// Waiting Times
#define Duration 20 // 20 Sec
/////////////////////////////////////////
#define myBuzzer 8
#define NOTE_Player_Wins 523
int ways=0;
void setup() {
Serial.begin(9600);
// Trafic Light #1
pinMode(TL1R,OUTPUT);
pinMode(TL1O,OUTPUT);
pinMode(TL1G,OUTPUT);
// Trafic Light #2
pinMode(TL2R,OUTPUT);
pinMode(TL2O,OUTPUT);
pinMode(TL2G,OUTPUT);
// Trafic Light #3
pinMode(TL3R,OUTPUT);
pinMode(TL3O,OUTPUT);
pinMode(TL3G,OUTPUT);
// Trafic Light #4
pinMode(TL4R,OUTPUT);
pinMode(TL4O,OUTPUT);
pinMode(TL4G,OUTPUT);
//IR Remote
Serial.begin(9600); // begin serial communication with a baud rate of 9600
receiver.enableIRIn(); // enable the receiver
receiver.blink13(true); // enable blinking of the built-in LED when an IR signal is received
/*// Screen output
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
//for(;;); // Don't proceed, loop forever
}*/
Serial.println(" Welcome to Cross Trafic Light Simulation");
Serial.println(" ========================================");
/* // Welcoming Screen on Oled
display.clearDisplay();
My_loop_Flash_text("TraficLight sim" ,10,10,1);
display.clearDisplay();
My_loop_Flash_text(" LET'S GO " ,0,10,2);
display.clearDisplay();
// Decide which Cross we have 3 or 4 ways*/
}
void loop() {
int input;
My_Main_Menu();
while (1)
{ways = input_Main_Menu( );
delay(1000);
if (ways==1 || ways==2)
{
Serial.print(" You choosed ways :");
Serial.println(ways);
break;
}
}
if (ways ==1)
{ ways =4;
My_SUB_Menu(ways);
while (1)
{
input = input_SUB_Menu( Duration , ways);
delay(5000);
if (input>=0)
{Serial.print(" The input is");
Serial.println(input);
break;
}
}
}
else if ( ways ==2)
{ ways =3;
while (1)
{
My_SUB_Menu(ways);
input= input_SUB_Menu( Duration , ways);
delay(5000);
if (input>=0)
{Serial.print(" The input is");
Serial.println(input);
break;
}
}
}
}
void My_SUB_Menu(int ways)
{
if (ways ==4){
Serial.println(" CH-: Go Ruond ");
// My_text (" CH-: Go Ruond",0,10,1);
Serial.println(" CH+: TEST Lights ");
// My_text (" CH+: TEST Lights ",0,10,1);
Serial.println(" 0: Open A to B");
// My_text (" 0: Open A to B",0,10,1);
Serial.println(" 1: Open A to D");
// My_text (" 1: Open A to D",0,10,1);
Serial.println(" 2: Open A to G");
// My_text (" 2: Open A to G",0,10,1);
Serial.println(" 3: Open C to D");
//My_text (" 3: Open C to D",0,10,1);
Serial.println(" 4: Open C to G");
//My_text (" 4: Open C to G",0,10,1);
Serial.println(" 5: Open C to I");
//My_text (" 5: Open C to I",0,10,1);
Serial.println(" 6: Open F to G");
//My_text (" 6: Open F to G",0,10,1);
Serial.println(" 7: Open F to I");
//My_text (" 7: Open F to I",0,10,1);
Serial.println(" 8: Open F to B");
//My_text (" 8: Open F to B",0,10,1);
Serial.println(" 9: Open H to I");
//My_text (" 9: Open H to I",0,10,1);
Serial.println(" -: Open H to B");
//My_text (" -: Open H to B",0,10,1);
Serial.println(" +: Open H to D");
//My_text (" +: Open H to D",0,10,1);
}
else if (ways ==3)
{
Serial.println(" CH-: Go Ruond ");
//My_text (" CH-: Go Ruond",0,10,1);
Serial.println(" CH+: TEST Lights ");
//My_text (" CH+: TEST Lights ",0,10,1);
Serial.println(" 0: Open A to B");
//My_text (" 0: Open A to B",0,10,1);
//Serial.println(" 1: Open A to D");
//My_text (" 1: Open A to D",0,10,1);
Serial.println(" 2: Open A to G");
//My_text (" 2: Open A to G",0,10,1);
//Serial.println(" 3: Open C to D");
//My_text (" 3: Open C to D",0,10,1);
Serial.println(" 4: Open C to G");
//My_text (" 4: Open C to G",0,10,1);
Serial.println(" 5: Open C to I");
//My_text (" 5: Open C to I",0,10,1);
//Serial.println(" 6: Open F to G");
//My_text (" 6: Open F to G",0,10,1);
//Serial.println(" 7: Open F to I");
//My_text (" 7: Open F to I",0,10,1);
//Serial.println(" 8: Open F to B");
//My_text (" 8: Open F to B",0,10,1);
Serial.println(" 9: Open H to I");
//My_text (" 9: Open H to I",0,10,1);
Serial.println(" -: Open H to B");
//My_text (" -: Open H to B",0,10,1);
// Serial.println(" +: Open H to D");
// My_text (" +: Open H to D",0,10,1);
}
}
void My_Main_Menu()
{
Serial.println(" 1: 4 way cross");
//My_text (" 1: 4 way cross",0,10,1);
Serial.println(" 2: 3 way cross");
//My_text (" 2: 3 way cross",0,10,1);
}
int input_SUB_Menu( int Dur , int ways)
{
unsigned long key_value = 0; // variable to store the key value
int My_Choice = -25; // NO Value
if (receiver.decode(&results)) { // decode the received signal and store it in results
if (results.value == 0xFFFFFFFF) { // if the value is equal to 0xFFFFFFFF
results.value = key_value; // set the value to the key value
return My_Choice;
}
else {
switch (results.value) { // compare the value to the following cases
case 0xFFA25D: // if the value is equal to 0xFD00FF
Serial.println("CH-"); // print "CH-" in the Serial Monitor and return 25
//My_text("CH-", 40,10,2); // Print on Oled Screen
Lets_go_round(Dur,ways);// input option
My_Choice = 25;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break;
/* case 0xFF629D:
Serial.println("CH");
My_text("CH", 40,10,2);
My_Choice = 24;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break;*/
case 0xFFE21D:
Serial.println("CH+");
// My_text("CH+", 40,10,2);
test_ALL_TL( ways);
My_Choice = 23;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break;
/*case 0xFF22DD:
Serial.println("|<< Previous");
My_text("|<< Previous", 10,10,1);
My_Choice = 22;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break;
case 0xFF02FD:
Serial.println(" >>| NEXT");
My_text(" >>| NEXT", 10,10,1);
My_Choice = 21;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFFC23D:
Serial.println(">|| PLAY PAUSE");
My_text(">|| PLAY PAUSE", 10,10,1);
My_Choice = 20;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;*/
case 0xFFE01F:
Serial.println("VOL-");
//My_text("VOL-", 40,10,2);
H2B(Dur);
My_Choice = 19;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFFA857:
Serial.println("VOL+");
//My_text("VOL+", 40,10,2);
H2D(Dur);
My_Choice = 18;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
/*case 0xFF906F:
Serial.println("EQ");
My_text("EQ", 40,10,2);
My_Choice = 17;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;*/
case 0xFF6897:
Serial.println("0");
// My_text("0", 40,10,3);
A2B(Dur);
My_Choice = 0;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
/*case 0xFF9867:
Serial.println("100+");
My_text("100+", 40,10,2);
My_Choice = 17;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;*/
case 0xFF30CF:
Serial.println("1");
// My_text("1", 40,10,3);
A2D(Dur);
My_Choice = 1;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF18E7:
Serial.println("2");
// My_text("2", 40,10,3);
A2G(Dur);
My_Choice = 2;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF7A85:
Serial.println("3");
// My_text("3", 40,10,3);
C2D(Dur);
My_Choice = 3;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF10EF:
Serial.println("4");
// My_text("4", 40,10,3);
C2G(Dur);
My_Choice = 4;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF38C7:
Serial.println("5");
// My_text("5", 40,10,3);
C2I(Dur);
My_Choice = 5;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF5AA5:
Serial.println("6");
// My_text("6", 40,10,3);
F2G(Dur);
My_Choice = 6;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF42BD:
Serial.println("7");
// My_text("7", 40,10,3);
F2I(Dur);
My_Choice = 7;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF4AB5:
Serial.println("8");
// My_text("8", 40,10,3);
F2B(Dur);
My_Choice = 8;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF52AD:
Serial.println("9");
// My_text("9", 40,10,3);
H2I(Dur);
My_Choice = 9;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
default :
Serial.println("ERROR: Not a value");
// My_text("ERROR-ERROR", 10,10,1);
key_value = results.value; // store the value as key_value
receiver.resume();
My_Choice = -1;
return My_Choice;
break;
}
}
}
return My_Choice;
}
int input_Main_Menu( )
{
unsigned long key_value = 0; // variable to store the key value
int My_Choice = -25; // NO Value
if (receiver.decode(&results)) { // decode the received signal and store it in results
if (results.value == 0xFFFFFFFF) { // if the value is equal to 0xFFFFFFFF
results.value = key_value; // set the value to the key value
return My_Choice;
}
else {
switch (results.value) { // compare the value to the following cases
case 0xFF30CF:
Serial.println("1");
// My_text("1", 40,10,3);
My_Choice = 1;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
case 0xFF18E7:
Serial.println("2");
// My_text("2", 40,10,3);
My_Choice = 2;
key_value = results.value; // store the value as key_value
receiver.resume();
return My_Choice;
break ;
default :
Serial.println("ERROR: Not a value");
// My_text("ERROR-ERROR", 10,10,1);
key_value = results.value; // store the value as key_value
receiver.resume();
My_Choice = -1;
return My_Choice;
break;
}
}
}
return My_Choice;
}
void test_ALL_TL(int ways)
{
if (ways ==4){
Serial.println(" TESTING 4 Ways LIGHTS");
Serial.println(" ================");
// My_loop_Flash_text("TESTING 4 Ways" ,0,10,1);
// display.clearDisplay();
// Test TL#1 ON Then OF
digitalWrite(TL1G,HIGH);
delay(500);
digitalWrite(TL1O,HIGH);
delay(500);
digitalWrite(TL1R,HIGH);
delay(500);
digitalWrite(TL1G,LOW);
delay(500);
digitalWrite(TL1O,LOW);
delay(500);
digitalWrite(TL1R,LOW);
delay(500);
// Test TL#2 ON Then OF
digitalWrite(TL2G,HIGH);
delay(500);
digitalWrite(TL2O,HIGH);
delay(500);
digitalWrite(TL2R,HIGH);
delay(500);
digitalWrite(TL2G,LOW);
delay(500);
digitalWrite(TL2O,LOW);
delay(500);
digitalWrite(TL2R,LOW);
delay(500);
// Test TL#3 ON sen OF
digitalWrite(TL3G,HIGH);
delay(500);
digitalWrite(TL3O,HIGH);
delay(500);
digitalWrite(TL3R,HIGH);
delay(500);
digitalWrite(TL3G,LOW);
delay(500);
digitalWrite(TL3O,LOW);
delay(500);
digitalWrite(TL3R,LOW);
delay(500);
// Test TL#4 ON sen OF
digitalWrite(TL4G,HIGH);
delay(500);
digitalWrite(TL4O,HIGH);
delay(500);
digitalWrite(TL4R,HIGH);
delay(500);
digitalWrite(TL4G,LOW);
delay(500);
digitalWrite(TL4O,LOW);
delay(500);
digitalWrite(TL4R,LOW);
delay(500);
/* My_loop_Flash_text(" ALL OK " ,0,10,2);
display.clearDisplay();
My_loop_Flash_text(" LET'S GO " ,0,10,2);
display.clearDisplay();*/
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
}
else if ( ways==3)
{
Serial.println(" TESTING 4 Ways LIGHTS");
Serial.println(" ================");
// My_loop_Flash_text("TESTING 4 Ways" ,0,10,1);
// display.clearDisplay();
// Test TL#1 ON Then OF
digitalWrite(TL1G,HIGH);
delay(500);
digitalWrite(TL1O,HIGH);
delay(500);
digitalWrite(TL1R,HIGH);
delay(500);
digitalWrite(TL1G,LOW);
delay(500);
digitalWrite(TL1O,LOW);
delay(500);
digitalWrite(TL1R,LOW);
delay(500);
// Test TL#2 ON Then OF
digitalWrite(TL2G,HIGH);
delay(500);
digitalWrite(TL2O,HIGH);
delay(500);
digitalWrite(TL2R,HIGH);
delay(500);
digitalWrite(TL2G,LOW);
delay(500);
digitalWrite(TL2O,LOW);
delay(500);
digitalWrite(TL2R,LOW);
delay(500);
/* Test TL#3 ON sen OF
digitalWrite(TL3G,HIGH);
delay(500);
digitalWrite(TL3O,HIGH);
delay(500);
digitalWrite(TL3R,HIGH);
delay(500);
digitalWrite(TL3G,LOW);
delay(500);
digitalWrite(TL3O,LOW);
delay(500);
digitalWrite(TL3R,LOW);
delay(500);*/
// Test TL#4 ON sen OF
digitalWrite(TL4G,HIGH);
delay(500);
digitalWrite(TL4O,HIGH);
delay(500);
digitalWrite(TL4R,HIGH);
delay(500);
digitalWrite(TL4G,LOW);
delay(500);
digitalWrite(TL4O,LOW);
delay(500);
digitalWrite(TL4R,LOW);
delay(500);
/* My_loop_Flash_text(" ALL OK " ,0,10,2);
display.clearDisplay();
My_loop_Flash_text(" LET'S GO " ,0,10,2);
display.clearDisplay();*/
Serial.println(" ALL OK ");
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
}
}
//////////////////////////////////////////////////////////////
void Lets_go_round(int Dur, int ways)
{
/*My_loop_Flash_text(" LET'S GO ROUND " ,0,10,1);
display.clearDisplay();*/
Serial.println(" LET'S GO ROUND ");
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
if (ways ==4)
{
A2B(Dur);
delay(1000);
A2D(20);
delay(1000);
A2G(20);
delay(1000);
C2D(20);
delay(1000);
C2G(20);
delay(1000);
C2I(20);
delay(1000);
F2G(20);
delay(1000);
F2I(20);
delay(1000);
F2B(20);
delay(1000);
H2I(20);
delay(1000);
H2B(20);
delay(1000);
H2D(20);
delay(1000);
}
else if (ways ==3)
{
A2B(Dur);
delay(1000);
// A2D(20);
//delay(1000);
A2G(20);
delay(1000);
// C2D(20);
//delay(1000);
C2G(20);
delay(1000);
C2I(20);
delay(1000);
// F2G(20);
// delay(1000);
//F2I(20);
//delay(1000);
// F2B(20);
// delay(1000);
H2I(20);
delay(1000);
H2B(20);
delay(1000);
// H2D(20);
// delay(1000);
}
}
//////////////////////////////////////////////////////////////
void A2B(int Dur_sec)
{
Serial.print (" Crossing from North to West within : ");
Serial.print (Dur_sec);
Serial.println ("second");
// My_text("N2W or A2B" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//Direction | GREEN | RED || Green | RED |
//--------------------------------------------|
//N2W |3+1+2 |4 || 3+4 | 2 |
//A2B | | || | |
//--------------------------------------------|
// Set RED @TL4 and Set green TL3 + TL1+TL2 för Dur_SEC
digitalWrite(TL4R,HIGH);
digitalWrite(TL3G,HIGH);
digitalWrite(TL1G,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL4R,LOW);
digitalWrite(TL4O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL4O,LOW);
digitalWrite(TL3G,LOW);
digitalWrite(TL1G,LOW);
digitalWrite(TL2G,LOW);
// delay befor ending the function
delay(200);
}
/////////////////////////////////////////////////////
void A2D(int Dur_sec)
{
Serial.print (" Crossing from North to South within : ");
Serial.println (Dur_sec);
Serial.println ("second");
//My_text("N2S or A2D" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//Direction | GREEN | RED || Green | RED |
//--------------------------------------------|
//S2N =N2S |3+1 |4+2 || NOT Applicabale|
//F2I A2D | | || |
//--------------------------------------------|
// Set RED @TL4+TL2 and Set green TL3 + TL1 for Dur_SEC
digitalWrite(TL4R,HIGH);
digitalWrite(TL3G,HIGH);
digitalWrite(TL1G,HIGH);
digitalWrite(TL2R,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL4R,LOW);
digitalWrite(TL2R,HIGH);
digitalWrite(TL4O,HIGH);
digitalWrite(TL2O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL4O,LOW);
digitalWrite(TL2O,LOW);
digitalWrite(TL3G,LOW);
digitalWrite(TL1G,LOW);
// delay befor ending the function
delay(200);
}
///////////////////////////////////////////////////////
void A2G(int Dur_sec)
{
Serial.print (" Crossing from North to East within : ");
Serial.println (Dur_sec);
Serial.println ("second");
// My_text("N2E or A2G" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//Direction | GREEN | RED || Green | RED |
//--------------------------------------------|
//N2E |3 |4+1+2|| 3 |2+4 |
//A2G | | || | |
//--------------------------------------------|
// Set RED @TL4 ,TL#1 and TL#2 Set green TL3
digitalWrite(TL4R,HIGH);
digitalWrite(TL3G,HIGH);
digitalWrite(TL1R,HIGH);
digitalWrite(TL2R,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
digitalWrite(TL4R,LOW);
digitalWrite(TL1R,LOW);
digitalWrite(TL2R,LOW);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL4O,HIGH);
digitalWrite(TL2O,HIGH);
digitalWrite(TL1O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL4O,LOW);
digitalWrite(TL2O,LOW);
digitalWrite(TL1O,LOW);
digitalWrite(TL3G,LOW);
digitalWrite(TL1G,LOW);
// delay befor ending the function
delay(200);
}
///////////////////////////////////////////////////////
void C2D(int Dur_sec)
{
Serial.print (" Crossing from West to South within : ");
Serial.println (Dur_sec);
Serial.println ("second");
// My_text("W2S or C2D" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//W2S |4+2+1 |3 || NOT Applicabale|
//C2D | | || | |
//--------------------------------------------|
// Set RED @TL3 and set green ,TL#1 , TL#4 and TL#2
digitalWrite(TL4G,HIGH);
digitalWrite(TL3R,HIGH);
digitalWrite(TL1G,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
digitalWrite(TL3R,LOW);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL3O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL3O,LOW);
digitalWrite(TL3G,LOW);
digitalWrite(TL1G,LOW);
// delay befor ending the function
delay(200);
}
///////////////////////////////////////////////////////
void C2G(int Dur_sec)
{
Serial.print (" Crossing from West to East within : ");
Serial.println (Dur_sec);
Serial.println ("second");
// My_text("W2E or C2G" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//E2W =W2E |4+2 |1+3 || 2+4 |3 |
//H2B C2G | | || |
//--------------------------------------------|
// Set RED @TL#3 and TL#1 set green TL#4 and TL#2
digitalWrite(TL4G,HIGH);
digitalWrite(TL3R,HIGH);
digitalWrite(TL1R,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
digitalWrite(TL3R,LOW);
digitalWrite(TL1R,LOW);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL3O,HIGH);
digitalWrite(TL1O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL3O,LOW);
digitalWrite(TL2O,LOW);
digitalWrite(TL4G,LOW);
digitalWrite(TL2G,LOW);
// delay befor ending the function
delay(200);
}
////////////////////////////////////////////////////////
void C2I(int Dur_sec)
{
Serial.print (" Crossing from West to North within : ");
Serial.println (Dur_sec);
Serial.println ("second");
//My_text("W2N or C2I" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//W2N |4 |1+2+3 || 4+3 |2 |
//C2I | | || | |
//--------------------------------------------|
// Set RED TL#1 TL#3 and TL#2 and set green @TL#4 and
digitalWrite(TL4G,HIGH);
digitalWrite(TL3R,HIGH);
digitalWrite(TL1R,HIGH);
digitalWrite(TL2R,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
digitalWrite(TL3R,LOW);
digitalWrite(TL1R,LOW);
digitalWrite(TL2R,LOW);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL3O,HIGH);
digitalWrite(TL2O,HIGH);
digitalWrite(TL1O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL3O,LOW);
digitalWrite(TL2O,LOW);
digitalWrite(TL1O,LOW);
digitalWrite(TL4G,LOW);
// delay befor ending the function
delay(200);
}
////////////////////////////////////////////////////////
void F2G(int Dur_sec)
{
Serial.print (" Crossing from South to East within : ");
Serial.println (Dur_sec);
Serial.println ("second");
// My_text("W2N or C2I" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//S2E |3+1+2 |4 ||NOT Applicabale |
//F2G | | || | |
//--------------------------------------------|
// Set RED TL#4 and set green @TL#3 TL#1 and TL#2
digitalWrite(TL4R,HIGH);
digitalWrite(TL3G,HIGH);
digitalWrite(TL1G,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *500 to convert to Msec then Red time
delay(Dur_sec*500);
digitalWrite(TL4R,LOW);
// Ornage time in sec *500 to convert to Msec then Ornage time
digitalWrite(TL4O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL4O,LOW);
digitalWrite(TL3G,LOW);
digitalWrite(TL1G,LOW);
digitalWrite(TL2G,LOW);
// delay befor ending the function
delay(200);
}
///////////////////////////////////////////////////////
void F2I(int Dur_sec)
{
Serial.print (" Crossing from South to North within : ");
Serial.println (Dur_sec);
Serial.println ("second");
// My_text("S2N or F2I" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//Direction | GREEN | RED || Green | RED |
//--------------------------------------------|
//S2N =N2S |3+1 |4+2 || NOT Applicabale|
//F2I A2D | | || |
//--------------------------------------------|
// Set RED @TL4+TL2 and Set green TL3 + TL1 for Dur_SEC
digitalWrite(TL4R,HIGH);
digitalWrite(TL2R,HIGH);
digitalWrite(TL1G,HIGH);
digitalWrite(TL3G,HIGH);
// Crossing time in sec *1000 to conver to Msec
delay(Dur_sec*1000);
digitalWrite(TL4R,LOW);
digitalWrite(TL2R,LOW);
digitalWrite(TL4O,HIGH);
digitalWrite(TL2O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL4O,LOW);
digitalWrite(TL2O,LOW);
digitalWrite(TL1G,LOW);
digitalWrite(TL3G,LOW);
// delay befor ending the function
delay(200);
}
////////////////////////////////////////////////////////
void F2B(int Dur_sec)
{
Serial.print (" Crossing from South to West within : ");
Serial.println (Dur_sec);
Serial.println ("second");
//My_text("S2W or F2B" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//S2W |1 |4+3+2 ||NOT Applicabale |
//F2B | | || | |
//--------------------------------------------|
// Set RED @TL#3 TL#4 and TL#2 and set green TL#1
digitalWrite(TL3R,HIGH);
digitalWrite(TL1G,HIGH);
digitalWrite(TL4R,HIGH);
digitalWrite(TL2R,HIGH);
// Crossing time in sec *1000 to conver to Msec
delay(Dur_sec*500);
digitalWrite(TL3R,LOW);
digitalWrite(TL4R,LOW);
digitalWrite(TL2R,LOW);
digitalWrite(TL3O,HIGH);
digitalWrite(TL4O,HIGH);
digitalWrite(TL2O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL3O,LOW);
digitalWrite(TL1G,LOW);
digitalWrite(TL4O,LOW);
digitalWrite(TL2O,LOW);
// delay befor ending the function
delay(200);
}
////////////////////////////////////////////////////////
void H2I(int Dur_sec)
{
Serial.print (" Crossing from East to North within : ");
Serial.println (Dur_sec);
Serial.println ("second");
//My_text("E2N or H2I" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//E2N |2+3+4 |1 || 2+3+4 |NT |
//H2I | | || | |
//--------------------------------------------|
// Set Green @TL#3 TL#4 and TL#2 , and set RED TL#1
digitalWrite(TL3G,HIGH);
digitalWrite(TL1R,HIGH);
digitalWrite(TL4G,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *1000 to conver to Msec
delay(Dur_sec*500);
digitalWrite(TL1R,LOW);
digitalWrite(TL1O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL3G,LOW);
digitalWrite(TL1O,LOW);
digitalWrite(TL4G,LOW);
digitalWrite(TL2G,LOW);
// delay befor ending the function
delay(200);
}
//////////////////////////////////////////////////////////
void H2B(int Dur_sec)
{
Serial.print (" Crossing from East to West within : ");
Serial.println (Dur_sec);
Serial.println ("second");
//My_text("E2W or H2B" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//E2W =W2E |4+2 |1+3 || 2+4 |3 |
//H2B C2G | | || |
//--------------------------------------------|
// Set RED @TL#3 and TL#1 set green TL#4 and TL#2
digitalWrite(TL3R,HIGH);
digitalWrite(TL1R,HIGH);
digitalWrite(TL4G,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *1000 to conver to Msec
delay(Dur_sec*500);
digitalWrite(TL3R,LOW);
digitalWrite(TL1R,LOW);
digitalWrite(TL3O,HIGH);
digitalWrite(TL1O,HIGH);
delay(Dur_sec*500);
// Trun off them now
digitalWrite(TL3O,LOW);
digitalWrite(TL1O,LOW);
digitalWrite(TL4G,LOW);
digitalWrite(TL2G,LOW);
// delay befor ending the function
delay(200);
}
////////////////////////////////////////////////////////
void H2D(int Dur_sec)
{
Serial.print (" Crossing from East to South within : ");
Serial.println (Dur_sec);
Serial.println ("second");
//My_text("E2S or H2D" , 10,10,1);
NewTone(myBuzzer,NOTE_Player_Wins);
delay(1000);
noNewTone(myBuzzer);
// looking for our table:
//--------------------------------------------|
//E2S |2 |1+4+3 || NOT Applicabale|
//H2D | | || | |
//--------------------------------------------|
// Set Green @TL#2 and set RED TL#4 and TL#3 , TL#1
digitalWrite(TL3R,HIGH);
digitalWrite(TL1R,HIGH);
digitalWrite(TL4R,HIGH);
digitalWrite(TL2G,HIGH);
// Crossing time in sec *1000 to conver to Msec
delay(Dur_sec*1000);
digitalWrite(TL3R,LOW);
digitalWrite(TL1R,LOW);
digitalWrite(TL4R,LOW);
digitalWrite(TL3O,HIGH);
digitalWrite(TL1O,HIGH);
digitalWrite(TL4O,HIGH);
// Trun off them now
digitalWrite(TL3O,LOW);
digitalWrite(TL1O,LOW);
digitalWrite(TL4O,LOW);
digitalWrite(TL2G,LOW);
// delay befor ending the function
delay(200);
}
/////////////////////////////////////////////////////////
/* void My_loop_Flash_text(const char *Text , int posX, int posY, int Font_Size)
{
display.clearDisplay();
display.setTextSize(Font_Size); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(posX, posY); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.write(Text);
display.display();
delay(2000);
display.invertDisplay(true);
delay(1000);
display.invertDisplay(false);
delay(1000);
}
void My_text(const char *Text , int posX, int posY, int Font_Size)
{
display.clearDisplay();
display.setTextSize(Font_Size); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(posX, posY); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.write(Text);
display.display();
delay(2000);
}*/
```
# DSNA22 OTHER Projects :
* [My Morse Code.](https://hackmd.io/@DSNA22/MorseCode)
* [Traffic Lights Controler with IR Remote.](https://hackmd.io/@DSNA22/Italian_Job)
* [Math Trainig with Arduino , LCD,and 4X4 Keypad.](https://hackmd.io/@DSNA22/Math_Trainig_with_Arduino)
* [Rock,Scissors or Paper game.](https://hackmd.io/@DSNA22/Rock_Scissors_Paper)
* [Connecting Quad Seven segment and DHT11 via WiFi and API.](https://hackmd.io/@DSNA22/4_Seven_segment_and_DHT11_via_WiFi)
* YouTube Channel : [ Or you can visit my YouTube channel Here.](https://www.youtube.com/c/EngAmmarALNahas/videos)
# Last but not least Thanks :
Thanks for [Thomas Berggren](https://www.linkedin.com/in/tomas-berggren/) for all knowledge and experience that looks clearly in backscenes of this project!