Created
August 6, 2013 21:53
-
-
Save joshuakfarrar/6169079 to your computer and use it in GitHub Desktop.
The start of the game I was making on my Arduino at Nodebots Day DC. I had to leave early due to camping, so I didn't get to finish :(
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var five = require('johnny-five') | |
, board | |
, pot | |
, button, buttonStatus | |
, pin = 9 // starting pin | |
, animation | |
board = new five.Board(); | |
var speed = 100 | |
board.on('ready', function() { | |
button = new five.Button(13); | |
button.on('down', function() { | |
buttonStatus = true; | |
flash(pin); | |
}); | |
button.on('up', function() { | |
buttonStatus = false; | |
}); | |
pot = new five.Sensor({ | |
pin: 'A3', | |
freq: 250 | |
}); | |
pot.on('read', function(err, val) { | |
speed = (val * 2000 / 1023) + 50; | |
}); | |
turnOn(); | |
}); | |
var flash = function(pin) { | |
clearInterval(animation); | |
var on = false; | |
animation = setInterval(function() { | |
if (on) { | |
clear(); | |
on = false; | |
} | |
else { | |
pinsOn(pin); | |
on = true; | |
} | |
}, speed); | |
setTimeout(turnOn, 2000) | |
} | |
var clear = function() { | |
for (var i = 2; i <= 9; i++) | |
(new five.Led(i)).off(); | |
} | |
var pinsOn = function(pin) { | |
for (var i = 9; i >= pin; i--) | |
(new five.Led(i)).on(); | |
} | |
var turnOn = function() { | |
clearInterval(animation); | |
animation = setInterval(function() { | |
(new five.Led(pin)).on(); | |
if (pin == 2) { | |
clearInterval(animation); | |
turnOff(); | |
} | |
pin--; | |
}, speed); | |
} | |
var turnOff = function() { | |
clearInterval(animation); | |
animation = setInterval(function() { | |
(new five.Led(pin)).off(); | |
if (pin == 9) { | |
clearInterval(animation); | |
turnOn(); | |
} | |
pin++; | |
}, speed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment