0
votes

I have a code not written by me that controls robotic arm by leap motion and arduino. using johnny five and leapjs. i tried it and it works and the way i move my hand the robot arm moves. no problem. but i want to make it to move by using keyboard.. like when i press a specific key one of the servo rotate right as long as i hold that key and so on for a total of four servos and each to have its specific keyboard key ,

example ( for servo 1 pressing "a" for left, "b" for right. for servo 2 "z"for left,"c" for right)

I really didn't write the original code that uses leap motion but i got it to run it and it worked for all servos.. now i want to make it keyboard control. can someone help ? i don't know the code to make the robot arm ( 4servos) controlled by keyboard

1

1 Answers

0
votes

I recommend the keypress module:

var five = require("johnny-five");
var keypress = require("keypress");

// Set up keypress
keypress(process.stdin);

// Initialize a new Board object
var board = new five.Board();

board.on("ready", function() {
  // Create two servos, on pins 9 and 10
  var servo1 = new five.Servo(9);
  var servo2 = new five.Servo(10);

  // Set up stdin to work correctly with the REPL
  process.stdin.resume();
  process.stdin.setEncoding("utf8");
  process.stdin.setRawMode(true);

  // When keys are pressed...
  process.stdin.on("keypress", function(ch, key) {
    if (!key) {
      return;
    }

    if (key.name === "a") {
      // Change 0 to whatever degree is "left"
      servo1.to(0);
    } 
    if (key.name === "b") {
      // Change 0 to whatever degree is "right"
      servo1.to(0);
    }
    if (key.name === "z") {
      // Change 0 to whatever degree is "left"
      servo1.to(0);
    } 
    if (key.name === "c") {
      // Change 0 to whatever degree is "right"
      servo1.to(0);
    }
  });
});