Skip to content

Instantly share code, notes, and snippets.

@jadonk
Last active August 13, 2019 20:28
Show Gist options
  • Save jadonk/7b2d335364b5474d566463d9b45360fa to your computer and use it in GitHub Desktop.
Save jadonk/7b2d335364b5474d566463d9b45360fa to your computer and use it in GitHub Desktop.
var b = require('bonescript');
var step = (function() {
var M1 = 1;
var M2 = 4;
var state = 0;
var enabled = 0;
return function(direction) {
if(!enabled) {
process.stdout.write("Initializing motors\n");
b.rcMotor("ENABLE");
enabled = 1;
}
if(direction > 0) {
state++;
} else if (direction < 0) {
state--;
}
if(state > 8) state = 0;
else if(state < 0) state = 7;
switch(state) {
case 0:
default:
b.rcMotor(M1,1);
b.rcMotor(M2,1);
break;
case 1:
b.rcMotor(M1,0);
b.rcMotor(M2,1);
break;
case 2:
b.rcMotor(M1,-1);
b.rcMotor(M2,1);
break;
case 3:
b.rcMotor(M1,-1);
b.rcMotor(M2,0);
break;
case 4:
b.rcMotor(M1,-1);
b.rcMotor(M2,-1);
break;
case 5:
b.rcMotor(M1,0);
b.rcMotor(M2,-1);
break;
case 6:
b.rcMotor(M1,1);
b.rcMotor(M2,-1);
break;
case 7:
b.rcMotor(M1,1);
b.rcMotor(M2,0);
break;
}
}
})();
var updateStepper = (function() {
var position = 0;
var direction = 1;
return function() {
// do stepping here
if(position >= 128) {
b.rcLED("GREEN",0);
b.rcLED("RED",1);
direction = -1;
} else if(position <= -128) {
b.rcLED("GREEN",1);
b.rcLED("RED",0);
direction = 1;
}
step(direction);
position += direction;
process.stdout.write("Position: " + position + " \r");
}
})();
setInterval(updateStepper, 10);
@silver2row
Copy link

@jadonk

Okay...I just say this reply. Thank you.

Seth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment