Created
          July 25, 2015 17:32 
        
      - 
      
- 
        Save robtarr/9c908bda5c70d6da5fb4 to your computer and use it in GitHub Desktop. 
    Electric Imp Robot
  
        
  
    
      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'), | |
| Imp = require("imp-io"), | |
| keypress = require('keypress'), | |
| board = new five.Board({ | |
| repl: false, | |
| debug: false, | |
| io: new Imp({ | |
| agent: 'FPKHeMBXeuim' | |
| }) | |
| }), | |
| stdin = process.stdin, | |
| moving = false, | |
| turning = false, | |
| leftSpeed = 0, | |
| rightSpeed = 0; | |
| keypress(process.stdin); | |
| stdin.setRawMode(true); | |
| stdin.resume(); | |
| board.on('ready', function() { | |
| var motors = new five.Motors([ | |
| { pins: { pwm: 1, dir: 2 } }, | |
| { pins: { pwm: 8, dir: 9 } } | |
| ]), | |
| leftMotor = motors[0], | |
| rightMotor = motors[1]; | |
| board.pinMode(1, board.MODES.PWM); | |
| board.pinMode(8, board.MODES.PWM); | |
| console.log('Ready!'); | |
| stdin.on('keypress', function(chunk, key) { | |
| if (!key) return; | |
| if (key.ctrl && key.name == 'c' || key.name == 'q') { | |
| console.log('Stopping'); | |
| leftMotor.stop(); | |
| rightMotor.stop(); | |
| process.exit(); | |
| } | |
| switch (key.name) { | |
| case 'up': | |
| console.log('forward'); | |
| motors.fwd(255); | |
| moving = true; | |
| break; | |
| case 'down': | |
| console.log('backward'); | |
| motors.rev(255); | |
| moving = true; | |
| break; | |
| case 'space': | |
| console.log('stopd'); | |
| motors.stop(); | |
| moving = false; | |
| leftSpeed = 0; | |
| rightSpeed = 0; | |
| break; | |
| case 'right': | |
| console.log('right'); | |
| if (moving) { | |
| motors[0].speed(150); | |
| motors[1].speed(255); | |
| } else { | |
| motors[1].fwd(175); | |
| motors[0].rev(175); | |
| } | |
| break; | |
| case 'left': | |
| console.log('left'); | |
| if (moving) { | |
| motors[1].speed(150); | |
| motors[0].speed(255); | |
| } else { | |
| motors[0].fwd(175); | |
| motors[1].rev(175); | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| }); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment