• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeRFIDTrain Track Switch – Servos, LEDs, PubNub and John ...
Previous Next

Train Track Switch – Servos, LEDs, PubNub and Johnny-five.io

Posted by: Olva , September 19, 2016

Train Track Switch – Servos, LEDs, PubNub and Johnny-five.io

Powered by Arduino using Johnny-five.io on a Raspberry Pi running NodeJS

Powered by Arduino using Johnny-five.io on a Raspberry Pi running NodeJS

LEGO Train Track Switch

The 8:45 express train is coming and we need somebody to switch the tracks quickly! If only we had a way of doing this remotely.

Goal:

– Remotely switch the train track and use lights to indicate the direction.

Challenges:

– Construct the traffic signals with micro LEDs to fit in my LEGO lights.

– Construct the mechanics to switch the track. This switch is very tough, so power will be an issue.

– Control a servo motor to move the track to position.

– Send a command remotely to switch the track.

Supplies:

– Raspberry Pi (or any NodeJS host)

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

– 2x micro SMD LEDs

– 12v power supply

– 2x NPN transistors

– servo

– breadboard

– 2x resistors

– button

Circuit:

LED

The SMD LEDs fit nicely in the LEGO lights and I found these on eBay for a good price.

SMD LED

Servo Motor

I will be using a 5v hobby servo with 180 degrees of rotation to control the switch arm.

hobby servo

Push Button

A simple push button will be used to toggle the track locally.

Push-Button-Switch

Fritzing Circuit Diagram

train track switch fritzing

The LEDs will actually be positioned so that only one is lit above each track. Since there are only two states, I have combined the four lights into two circuits.

Programming:

We will need to initialize the board as usual and then define the two LED pins for our lights and the servo for the track switch. Then we need to define the two states that the switch will be in; straight or turn. Each state will represent the current lights and position of the servo.

To understand what position we set the servo at will take a lot of trial and error. I have tried several LEGO mechanical ideas to provide enough torque to switch the track. Each version resulted in different servo positions. This is where the REPLcomes in really handy. You can easily adjust the position of the servo without exiting the code. Once its dialed in, you just have to update your code once.

Basic Flow

  • Set initial state “straight”
  • Listen for server or REPL command
  • Toggle servo and LEDs to proper state configuration

 

Source Code

The code uses NodeJS and the Johnny-Five.io robotics framework to control the devices.

PubNub is an enterprise messaging service using the Publish/Subscription model, which is ideal for IoT projects. We will use this service to send commands from the Internet.

 

  1. // traintrackswitch.js
  2. /****************************
  3. // Lego Train Track Switch
  4. // This app uses the Johnny-Five.io framework to control a servo and lights
  5. // The servo will switch the track to the desired location and update the direction lights
  6. // Commands can be sent from the “iol” PubNub channel
  7. // {“command”:”track_straight”}
  8. // {“command”:”track_turn”}
  9. // {“command”:”track_toggle”}
  10. // Written by Cory Guynn
  11. // www.InternetOfLego.com
  12. // 2016
  13. ****************************/
  14. /****************************
  15. // Server Code
  16. ****************************/
  17. // The config file will be used to store API details. Its separate for security reasons.
  18. var config = require(‘./config’);
  19. var pubnub = require(“pubnub”)({
  20. ssl : true, // <- enable TLS Tunneling over TCP
  21. publish_key : config.pubnub.publish_key, // These will be YOUR PubNub API keys
  22. subscribe_key : config.pubnub.subscribe_key
  23. });
  24. // Publish Messages
  25. var message = { “news” : “Train Track Switch online!” };
  26. pubnub.publish({
  27. channel : ‘iol’,
  28. message : message,
  29. callback : function(e) { console.log( “Server: PubNub SUCCESS!”, e ); },
  30. error : function(e) { console.log( “Server: PubNub FAILED! RETRY PUBLISH!”, e ); }
  31. });
  32. /****************************
  33. // Circuit Code
  34. ****************************/
  35. // Arduino Johnny-Five
  36. var five = require(“johnny-five”);
  37. var board = new five.Board();
  38. board.on(“ready”, function() {
  39. // Track Switch Components
  40. var trackSwitchLedA = new five.Led(26); // Green for straight, Red for turn
  41. var trackSwitchLedB = new five.Led(27); // Red for straight, Green for turn
  42. var trackSwitchServo = new five.Servo(6); // Track Switch
  43. var trackSwitchButton = new five.Button(2); // Toggle Button
  44. // Add devices to REPL (optional)
  45. this.repl.inject({
  46. trackSwitch: trackSwitch, // example: trackSwitch(Straight)
  47. trackSwitchServo: trackSwitchServo // example: trackSwitchServo.to(90);
  48. });
  49. // Button
  50. trackSwitchButton.on(“down”, function(){
  51. console.log(“trackSwitchButton pressed”);
  52. trackSwitch();
  53. });
  54. // Track Switch with Lights
  55. // Accepts a direction, or will toggle if no parameters sent
  56. var trackSwitchState = “straight”;
  57. function trackSwitch(trackSwitchState){
  58. if (trackSwitchState == “straight”){
  59. console.log(“track switched: Straight”);
  60. trackSwitchLedA.on();
  61. trackSwitchLedB.off();
  62. trackSwitchServo.to(90);
  63. trackSwitchState = “straight”;
  64. } else {
  65. console.log(“track switched: Turn”);
  66. trackSwitchLedA.off();
  67. trackSwitchLedB.on();
  68. trackSwitchServo.to(130);
  69. trackSwitchState = “turn”;
  70. }
  71. }
  72. /* —————————————————————————
  73. Listen for PubNub Messages
  74. ————————————————————————— */
  75. pubnub.subscribe({
  76. channel : “iol”,
  77. callback : function(msg) {
  78. console.log( “Server: PubNub Received message and sending to circuit: “, msg );
  79. if (msg.command === “track_straight”){
  80. trackSwitch(“straight”);
  81. }
  82. if (msg.command === “track_turn”){
  83. trackSwitch(“turn”);
  84. }
  85. if (msg.command === “track_toggle”){
  86. trackSwitch();
  87. }
  88. }
  89. });
  90. }); //end board.on()

Source Code Download: Gist

Now save and close your file.

Note: If this is your first project, you will need to initialize the folder and add thejohhny-five and pubnub module.
Run the following two commands before launching your new app.
npm init
npm install johnny-five pubnub --save
Now run the code
node traintrackswitch.js

Node-RED

To easily interact with the train track switch, we will use Node-RED. I am runningTheThingBox image on my Raspberry Pi, so Node-RED is already installed. To use it with PubNub, I just installed the additional NPM module by running this command on the Raspberry Pi from within the .node-red folder.

npm install node-red-contrib-pubnub

Now I can quickly create this flow to send messages to my LEGO track.

Train Track Switch - NodeRED

The function blocks are very basic:

traintrackswitch nodered function block

LEGO Train Track Switched!

the original post is from http://www.internetoflego.com/train-track-switch-servos-leds-pubnub-and-johnny-five-io/

Share!
Tweet

Olva

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors