Project 6 - Keyboard and servos (work in progress)


Two servos controlled from the keyboard.

I use the arrows keys of a laptop keyboard to move the servos of a pan/tilt. A servo is controlled by the up and down arrows keys. The other one is controlled by left and right arrows keys. So I can control a device like a camera directly from the laptop.

I can control the servos from the keyboard using a Processing program.

The up and down arrows control the servo on the right side, the right and left arrows control the other one. While the servos move the leds indicate the directions.




Processing sketch


 import processing.serial.*;
 color fillVal = color(0,0,0); // set variable 'fillVal'
 Serial port;
 
 void setup() {
 size(100, 100);
 println("Available serial ports:");
 println(Serial.list());
 port = new Serial(this, Serial.list()[2], 9600);  
 }
 
 void draw() {
   background(0,0,0);
   if (keyPressed == true) { // if key is pressed
   fill(fillVal);
   ellipse(50, 50, 50, 50); 
   }
   else {
    keyReleased(); 
   }
 }

void keyPressed() {
     if (key == CODED) {
       if (keyCode == UP) {
         fillVal = color(255,255,255); // 'fillVal' = white
       } else if (keyCode == DOWN) {
         fillVal = color(255,0,0); // ... red
       } else if (keyCode == LEFT){
         fillVal = color(0,255,0); // ... green1
       } else if (keyCode == RIGHT) {
         fillVal = color(0,255,150); // ... green2      
       }
     } else {
    fillVal = 0;
  }
  port.write(keyCode);
  if (keyPressed == false) {
    port.write('0');
  }
}

void keyReleased() {
  port.write('0'); 
}



Arduino sketch


/*  20/11/2011 - Massimo Pacilio
    
    This skectch is made to be used with 
    the Processing sketch.
    Form "Sweep" by BARRAGAN <http://barraganstudio.com>
    This example code is in the public domain.
*/
#include <Servo.h> 

Servo servo1, servo2;  // create servo object to control 2 servos 
 
int pos = 0;    // variable to store the servo position 

const unsigned int BAUD_RATE = 9600;

char key = 0;

void setup() {
  servo1.attach(2);  // attaches the servo1 on pin 5 to the servo object
  servo2.attach(4);  // attaches the servo2 on pin 6 to the servo object
  servo1.write(10);
  servo2.write(0);
  Serial.begin(BAUD_RATE);
}

void loop() {
  if (Serial.available()) {
     key = Serial.read();
     if (key == byte(37)) 
     { // left
       for(pos = 0; pos < 180; pos += 1)
         {                               // in steps of 1 degree 
            servo1.write(pos);           // tell servo to go to position in variable 'pos' 
            delay(15);                   // waits 15ms for the servo to reach the position      
         }
     } 
     else if (key == byte(39)) 
     { // right
       for(pos = 180; pos >=1; pos -= 1)
         {                               // in steps of 1 degree 
            servo1.write(pos);           // tell servo to go to position in variable 'pos' 
            delay(15);                   // waits 15ms for the servo to reach the position      
         }
     }
     else if (key == byte(38)) 
     { // up
       for(pos = 0; pos <180; pos += 1)
         {                               // in steps of 1 degree 
            servo2.write(pos);           // tell servo to go to position in variable 'pos' 
            delay(15);                   // waits 15ms for the servo to reach the position      
         } 
     }
     else if (key == byte(40)) 
     { // down
       for(pos = 180; pos >=1; pos -= 1)
         {                               // in steps of 1 degree 
            servo2.write(pos);           // tell servo to go to position in variable 'pos' 
            delay(15);                   // waits 15ms for the servo to reach the position      
         }
     } 
     else 
     {
        servo2.write(pos);    
     }     
   }
}





7 comments:

  1. Hello, thanks for sharing this !
    How did you get to know that Up key is byte(38) ? Can't find this information on the web !
    I'm searching for what could be the byte value for CTRL key, could you help me with that ?
    Thanks in advance!

    ReplyDelete
  2. I found these informations on the official arduino website, but i don't remember exactly where.

    ReplyDelete
  3. how to select port at processing 2.1.1

    ReplyDelete
  4. how to change port at processing 2.1.1

    ReplyDelete
    Replies
    1. You have to control the list of available ports in the Processing monitor (in the bottom of the window). These instructions allow you to select the right port. Verify which port is used by arduino and insert the relative number (starting from 0) in the row below.

      println("Available serial ports:");
      println(Serial.list());
      port = new Serial(this, Serial.list()[2], 9600);

      Delete
  5. it doesn't works for me, i just want to turn on a led with the keyboard arrows, but i don't know why the code doesn't works

    ReplyDelete
  6. ArrayIndexOutOfBondsException:2 plzzzz help me with this error

    ReplyDelete