Skip to content

Instantly share code, notes, and snippets.

@buymeasoda
Created August 16, 2012 09:43
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 buymeasoda/3368911 to your computer and use it in GitHub Desktop.
Save buymeasoda/3368911 to your computer and use it in GitHub Desktop.
Arduino IDE and Johnny-five based test scripts of button and led
/*
Running the following script via the Arduino IDE and uploading it to the board works correctly.
The in-built LED on pin 13 lights up when the button is pressed and turns off when the button is released.
*/
const int buttonPin = 7;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
/*
Loading the standard firmata onto the board and running the same board configuration with the following johnny-five script doesn't echo to the console or activate the led.
By manually firing led.on(), or button.emit('down') via the REPL, the actions log and occur, but not via the button press.
*/
var five = require("johnny-five"),
board = new five.Board(),
button, led;
board.on("ready", function() {
button = new five.Button(7);
led = new five.Led(13);
board.repl.inject({
button: button,
led: led
});
button.on('down', function () {
console.log('down');
led.on();
});
button.on('up', function () {
console.log('up');
led.off();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment