Skip to content

Instantly share code, notes, and snippets.

@dexterlabora
Created September 17, 2015 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexterlabora/40237108b7f05abf9e5d to your computer and use it in GitHub Desktop.
Save dexterlabora/40237108b7f05abf9e5d to your computer and use it in GitHub Desktop.
IoL - Lego Track Switch - Servo and LEDs
//trackswitch.js
var five = require("johnny-five");
var board = new five.Board();
//Arduino board connection
board.on("ready", function() {
// Track Switch Components
var trackSwitchLedA = new five.Led(26); // Green for straight, Red for turn
var trackSwitchLedB = new five.Led(27); // Red for straight, Green for turn
var trackSwitchServo = new five.Servo(6); // Track Switch
var trackSwitchButton = new five.Button("A0");
// Add devices to REPL (optional)
this.repl.inject({
trackSwitchButton: trackSwitchButton,
trackSwitch: trackSwitch,
trackSwitchServo: trackSwitchServo
});
// Button
trackSwitchButton.on("down", function(){
console.log("trackSwitchButton pressed");
trackSwitch();
});
// Track Switch with Lights
var trackSwitchState = "straight";
function trackSwitch(){
if (trackSwitchState != "straight"){
console.log("track switched: Straight");
trackSwitchLedA.on();
trackSwitchLedB.off();
trackSwitchServo.to(90);
trackSwitchState = "straight";
} else {
console.log("track switched: Turn");
trackSwitchLedA.off();
trackSwitchLedB.on();
trackSwitchServo.to(130);
trackSwitchState = "turn";
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment