Traffic lights


Description

In this tutorial is simulated a traffic light using an Arduino UNO WiFi.
In particular it is shown how to manage two lights directly by Smartphone/PC

Warning:
Please, it's important to upgrade to the latest ESP firmware, using the Arduino UNO firmware updater tool, before to continue this tutorial.

Hardware 

You can use also the components of the WorkShop kit or use 6 Tinkerkit LEDs.
Thinkerkit Led

For more details visit our Store.

Circuit

Use a breadboard to realize your circuit.
Follow these indications for the connections:
The Led has two legs called anode and cathode, as shown in the below image:
LED

For the first Traffic light:
-  Red led: connect the anode (normally the longer pin) at the 9 pin and the cathode (flat side) at the Ground (GND) via a 220 OHM resistor;
- Yellow led: connect the anode (normally the longer pin) at the 10 pin and the cathode (flat side) at the Ground (GND) via a 220 OHM resistor;
- Green led: connect the anode (normally the longer pin) at the 12 pin and the cathode (flat side) at the Ground (GND) via a 220 OHM resistor;

For the second Traffic light:
-  Red led: connect the anode (normally the longer pin) at the 2 pin and the cathode (flat side) at the Ground (GND) via a 220 OHM resistor;
- Yellow led: connect the anode (normally the longer pin) at the 4 pin and the cathode (flat side) at the Ground (GND) via a 220 OHM resistor;
- Green led: connect the anode (normally the longer pin) at the 8 pin and the cathode (flat side) at the Ground (GND) via a 220 OHM resistor;

For more details, look the below image:

traffic lights

Traffic lights circuit

Keep in mind that if you use the Tinkerkit Leds then follow these Indications to connect them at the board:
The Tinkerkit Led has three pins Vcc(+), the Signal and the Ground (-).
Connect the Vcc pin (+) to 5V, the Ground (-) to GND and the other one at the correspondent digital pin, use the wires female/male for the connections.

Code

Now connect the board to PC, using the USB cable, and upload the code below, using the Arduino IDE (click here for download the sketch):

Warning:
If you are using the Arduino IDE 1.7.x then continue to use the ArduinoWiFi.h library.
Instead if you are using the Arduino IDE 1.8.x then download the UNOWiFiDev.Edition library from Library Manager and modify the code, replacing #include <ArduinoWiFi.h> with #include <UnoWiFiDevEd.h>


/*
Traffic lights:
Upload the code and open your borwser on http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
for more information:
http://www.arduino.org/learning/tutorials/boards-tutorials/traffic-lights


created in November 2016
by sebba[at]arduino[dot]org
*/

//Import libraries
#include <Wire.h>
#include <ArduinoWiFi.h> // if you are using the Arduino IDE 1.8.x then: //#include <UnoWiFiDevEd.h>

int green_ped=12;
int yellow_ped=10;
int red_ped=9;

int green=8;
int yellow=4;
int red=2;

int value=99;
int fstep = 0;

//SET UP
void setup() {
pinMode(13,OUTPUT);
Wifi.begin();
Wifi.println("WebServer Server is up");

pinMode(green,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(red,OUTPUT);
pinMode(green_ped,OUTPUT);
pinMode(yellow_ped,OUTPUT);
pinMode(red_ped,OUTPUT);

digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
digitalWrite(green_ped,LOW);
digitalWrite(yellow_ped,LOW);
digitalWrite(red_ped,LOW);
}

//LOOP
void loop() {
while(Wifi.available()){
process(Wifi);
}
delay(50);

if(value==1){
semaforoAttivo(fstep++%4);
}else if(value==0){
fstep=0;
semaforoSpento();
}
}


/* FUNCTIONS */

//process()
void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');

// is "digital" command?
if (command == "webserver") {
WebServer(client); // call WebServer() function
}

if (command == "digital") {
digitalCommand(client); //call digitalCommand() function
}
}

//WebServer()
void WebServer(WifiData client) {
client.println(F("<html><head><style>"));

//Stylesheet parameters
client.println(F("input[type=button]{font-size:62px;color:#FFF;width:50%;margin:20% 0 0 25%;}"));
client.println(F(".off{background:#84c1c3}"));
client.println(F(".on{background:#00979C}"));
client.println(F("</style><script>"));

/*
JavaScript functions that creates an Ajax request to the URL
URL+'/arduino/digital/'+pin+'/'+value"
where URL is the IP of the Uno WiFi, pin is the pin to change and value is the logic level
of the pin
*/
// This function create the command for the AJAX request
client.println(F("function command(event){"));
client.println(F("var value = document.getElementById(event.id).value == 'START' ? 1 : 0"));
client.println(F("ajax(event.id, value)}"));

//Create the AJAX request
client.println(F("function ajax(elm, val){"));
client.println(F("var el = document.getElementById(elm)"));
client.println(F("var URL_array = document.URL.split('/');"));;
client.println(F("var URL = URL_array[0] + '//' + URL_array[2]"));
client.println(F("url = URL + '/arduino/digital/' + val"));
client.println(F("xmlhttp=new XMLHttpRequest()"));
client.println(F("xmlhttp.onreadystatechange=function(){"));
client.println(F("if(xmlhttp.readyState == 4 && xmlhttp.status == 200)"));
client.println(F("var resp = xmlhttp.responseText"));
client.println(F("if(typeof resp === 'undefined') return"));
client.println(F("el.className=el.className=='on'?'off':'on'"));
client.println(F("el.value=el.value=='START'?'STOP':'START'}"));
client.println(F("xmlhttp.open('GET',url,true)"));
client.println(F("xmlhttp.send()"));
client.println(F("}</script>"));
client.println(F("</head><body>"));

//Print the HTML Code with the button.
client.print(F("<input id=button class=on type=button onClick='command(this)' value='START'> "));
client.println(F("</body></html>"));
client.print(DELIMITER);
client.flush();
}

// digitalCommand() function
void digitalCommand(WifiData client) {
// If the next character is a '/' it means we have an URL with a value like: "/digital/1"
value = client.parseInt();

// Send feedback to client
client.println(F("HTTP/1.1 200 OK"));
client.println("Content-Type: text/html \n");
client.println(F("Light"));
client.print(EOL);
}


/* semaforoAttivo() function
Manages the leds of trafficlight
*/
void semaforoAttivo(int st)
{
switch (st){
case 0:
digitalWrite(red,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);

digitalWrite(red_ped,LOW);
digitalWrite(yellow_ped,LOW);
digitalWrite(green_ped,HIGH);

delay(3000);
break;

case 1:
digitalWrite(red,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);

digitalWrite(red_ped,LOW);
digitalWrite(yellow_ped,HIGH);
digitalWrite(green_ped,LOW);
delay(1000);
break;
case 2:
digitalWrite(red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,HIGH);

digitalWrite(red_ped,HIGH);
digitalWrite(yellow_ped,LOW);
digitalWrite(green_ped,LOW);
delay(3000);
break;
case 3:
digitalWrite(red,LOW);
digitalWrite(yellow,HIGH);
digitalWrite(green,LOW);

digitalWrite(red_ped,HIGH);
digitalWrite(green_ped,LOW);
delay(1500);
break;
}
}

/* semaforoSpento() function
It set in "warning" mode the trafficlight
*/
void semaforoSpento()
{
digitalWrite(red,LOW);
digitalWrite(red_ped,LOW);
digitalWrite(green,LOW);
digitalWrite(green_ped,LOW);
delay(600);
digitalWrite(yellow,HIGH);
digitalWrite(yellow_ped,HIGH);
delay(600);
digitalWrite(yellow,LOW);
digitalWrite(yellow_ped,LOW);
}
If you want to use an external webserver, for example Apache, then use this other code.

OUTPUT

  • Click Start button to active the traffic lights.
start button
  • Click Stop button to send out service the traffic lights.
stop button 

Note: 
If you haven't configured your board then click here.