Skip to content

Instantly share code, notes, and snippets.

@jadonk
Created May 14, 2014 07:15
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 jadonk/62c2dd8c2c014ec56712 to your computer and use it in GitHub Desktop.
Save jadonk/62c2dd8c2c014ec56712 to your computer and use it in GitHub Desktop.
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