Created
May 14, 2014 07:15
-
-
Save jadonk/62c2dd8c2c014ec56712 to your computer and use it in GitHub Desktop.
This file contains 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 b = require('bonescript'); | |
var MOTOR = 'P9_16'; | |
var FORWARD = 'P9_18'; | |
var BACKWARD = 'P9_22'; | |
var POT = 'P9_36'; | |
var BUTTON = 'P8_19' | |
var speed = 0; | |
var increment = 0.01; | |
var direction = 1; | |
b.pinMode(BUTTON, b.INPUT); | |
//b.pinMode(MOTOR, b.OUTPUT); | |
b.pinMode(FORWARD, b.OUTPUT); | |
b.pinMode(BACKWARD, b.OUTPUT); | |
b.digitalWrite(FORWARD, direction ? b.HIGH : b.LOW); | |
b.digitalWrite(BACKWARD, direction ? b.LOW : b.HIGH); | |
updateDuty(); | |
function updateDuty() { | |
b.analogWrite(MOTOR, speed, 60, scheduleNextUpdate); | |
} | |
function scheduleNextUpdate() { | |
setTimeout(doRead, 20); | |
} | |
function doRead() { | |
b.analogRead(POT, onRead); | |
} | |
function onRead(x) { | |
if(!x.err) { | |
speed = x.value; | |
} | |
doButtonRead(); | |
} | |
function doButtonRead() { | |
b.digitalRead(BUTTON, onButtonRead); | |
} | |
function onButtonRead(x) { | |
if(!x.err) { | |
if(direction != x.value) { | |
direction = x.value; | |
b.digitalWrite(FORWARD, direction ? b.HIGH : b.LOW); | |
b.digitalWrite(BACKWARD, direction ? b.LOW : b.HIGH); | |
} | |
} | |
updateDuty(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment