Last active
October 24, 2021 08:42
-
-
Save ghtomcat/b97659bc9fca64a9acadab14572f9714 to your computer and use it in GitHub Desktop.
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 keypress = require('keypress'); | |
var five = require("johnny-five"); | |
var EtherPortClient = require("etherport-client").EtherPortClient; | |
var board = new five.Board({ | |
port: new EtherPortClient({ | |
host: "192.168.1.235", | |
port: 3030 | |
}), | |
timeout: 1e5, | |
repl: false | |
}); | |
board.on("ready", function() { | |
console.log("READY!"); | |
var leftWheel= new five.Motor({ | |
pins: { | |
pwm: 14, | |
dir: 12 | |
} | |
}); | |
var rightWheel= new five.Motor({ | |
pins: { | |
pwm: 15, | |
dir: 13 | |
} | |
}); | |
leftWheel.setPin(12,0); | |
leftWheel.setPWM(14,0); | |
leftWheel.setPin(15,0); | |
leftWheel.setPWM(13,0); | |
keypress(process.stdin); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.setRawMode(true); | |
console.log("Press a key to control the Bot"); | |
process.stdin.on('keypress', function (ch, key) { | |
if ( !key ) { return; } | |
if ( key.name === 'q' ) { | |
console.log('Quitting'); | |
process.exit(); | |
} else if ( key.name === 'up' ) { | |
console.log('Forward'); | |
leftWheel.setPin(12,1); | |
leftWheel.setPWM(14,1500); | |
rightWheel.setPin(13,1); | |
rightWheel.setPWM(15,1500); | |
} else if ( key.name === 'down' ) { | |
console.log('Backward'); | |
leftWheel.setPin(12,0); | |
leftWheel.setPWM(14,1500); | |
rightWheel.setPin(13,0); | |
rightWheel.setPWM(15,1500); | |
} else if ( key.name === 'space' ) { | |
console.log('Stopping'); | |
leftWheel.setPin(12,0); | |
leftWheel.setPWM(14,0); | |
rightWheel.setPin(13,0); | |
rightWheel.setPWM(15,0); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment