Traffic Signal

My Lego City is moving now! We are going to need more than a stop sign to keep the traffic in order. Let’s build a traffic light!

Goal:

– Build a traffic signal that sequences through the traffic lights.

Challenges:

– Construct the traffic signal.

– Multiple LED circuit.

  • Code the sequence.

Components:

  • Raspberry Pi (or any NodeJS host)

  • Arduino (I used the Mega 2560 but any will do)

  • 1.8v LEDs

  • breadboard

  • 220 Ohm resistors

 

 

Schematic:

Because we only need signal lighting, we can use low voltage (~1.8v) LEDs and use the Arduino to power them. We still need some 220 ohm resistors inline to manage the current and protect the circuit.

 

Traffic Signal LEDs 220 Ohm resistors

Traffic Signal LEDs
220 Ohm resistors

Code:

We will need to initialize the board as usual and then define the three LED pins for our lights. Because I’m using British style signaling (or close enough), there will be a yellow light before and after the red and green lights. This is handy because you could easily connect the alternate red/green color and have both signals (6 lights) using three pins.

Basic Flow

  • Red – Green – 6 seconds
  • Yellow – Yellow – 2 seconds
  • Green – Red – 6 seconds
  • Yellow – Yellow – 2 seconds
  • Loop

The setTimeout() function is something we will use often  because it allows us to delay an action. It consists of two basic parameters, the callback function and the delay in milliseconds.

Filename

trafficsignal.js

//trafficsignal.js var five = require("johnny-five"); var board = new five.Board(); //Arduino board connection board.on("ready", function() { // Traffic Signal LEDs var trafficSignalLedRG = new five.Led(49); //Signal Red & Green var trafficSignalLedY = new five.Led(50); //Signal Yellow var trafficSignalLedGR = new five.Led(51); //Signal Green & Red var trafficSignalState = 0; // Initialize Signal State function trafficSignal(){ if (trafficSignalState == 0){ // Red Green console.log("traffic crossing RG"); trafficSignalLedRG.on(); trafficSignalLedY.off(); trafficSignalLedGR.off(); trafficSignalState = 1; setTimeout(trafficSignal,6000); } else if (trafficSignalState == 1){ // Yellow Yellow console.log("traffic crossing Y"); trafficSignalLedRG.off(); trafficSignalLedY.on(); trafficSignalLedGR.off(); trafficSignalState = 2; setTimeout(trafficSignal,2000); } else if (trafficSignalState == 2){ // Green Red console.log("traffic crossing GR"); trafficSignalLedRG.off(); trafficSignalLedY.off(); trafficSignalLedGR.on(); trafficSignalState = 3; setTimeout(trafficSignal,6000); } else if (trafficSignalState == 3){ // Yellow Yellow console.log("traffic crossing Y"); trafficSignalLedRG.off(); trafficSignalLedY.on(); trafficSignalLedGR.off(); trafficSignalState = 0; setTimeout(trafficSignal,2000); } } trafficSignal(); // Start Traffic Signal });

Now save and close your file.

Note: If this is your first project, you will need to initialize the folder and add the johhny-five module.
Run the following two commands before launching your new app.
npm init
npm install johnny-five --save
Now run the code
node trafficsignal.js
You should now see this on your console and your LEDs should be sequencing!
root@thethingbox:~/IoL# node trafficsignal.js 
1442332839007 Device(s) /dev/ttyACM0 
1442332839092 Connected /dev/ttyACM0 
1442332842044 Repl Initialized 
>> traffic crossing RG
traffic crossing Y
traffic crossing GR
traffic crossing Y
traffic crossing RG

Lego: