Infrared Remote - Two Servos Control

I use the code in http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434 from user David Cuartielles and servo.h library to control two servos with a parallax infra red remote control,

here is the code :

#include <Servo.h>

int ir_pin = 4;      
int debug = 1;
int start_bit = 2000;
int bin_1 = 1000;
int bin_0 = 400;
Servo servo1;
Servo servo2;

void setup()
{
  pinMode(ir_pin, INPUT);
  servo1.attach(2);
  servo2.attach(3);
}


int getIRKey() {
 int data[12];

 while(pulseIn(ir_pin, LOW) < 2200) { 
 }
 data[0] = pulseIn(ir_pin, LOW);       
 data[1] = pulseIn(ir_pin, LOW);
 data[2] = pulseIn(ir_pin, LOW);
 data[3] = pulseIn(ir_pin, LOW);
 data[4] = pulseIn(ir_pin, LOW);
 data[5] = pulseIn(ir_pin, LOW);
 data[6] = pulseIn(ir_pin, LOW);
 data[7] = pulseIn(ir_pin, LOW);
 data[8] = pulseIn(ir_pin, LOW);
 data[9] = pulseIn(ir_pin, LOW);
 data[10] = pulseIn(ir_pin, LOW);
 data[11] = pulseIn(ir_pin, LOW);
 if(debug == 1) {
 }
 for(int i=0;i<=11;i++) {              
   if(data[i] > bin_1) {                
       data[i] = 1;
   }  else {
       if(data[i] > bin_0) {           
         data[i] = 0;
       } else {
        data[i] = 2;                   
       }
   }
 }

 for(int i=0;i<=11;i++) {            
   if(data[i] > 1) {
       return -1;            
   }
 }

 int result = 0;
 int seed = 1;
 for(int i=11;i>=0;i--) {            
   if(data[i] == 1) {
       result += seed;
   }
   seed = seed * 2;
 }
 return result;                            
}

void move(int s1, int s2){
      servo1.write(s1);
      servo2.write(s2); 
      Servo::refresh();
}

void loop()
{
  int key = getIRKey();
    if(key == 2064){  // up
      for(int t=0;t<100;t++){
        move(0,180);
        }
    }
    if(key == 528){ //down
      for(int t=0;t<100;t++){
        move(180,0);
      }
    }
    if(key == 1040 || key == 3088){ //left
      for(int t=0;t<100;t++){
        move(0,0);
      }
    }
    if(key == 16 || key == 2576){ //right
      for(int t=0;t<100;t++){
        move(180,180);
      }
    }
  
}

I think is better someone who knows, make a library for infra red receiving and transmitting …

Does it work?

Does it work?

yeap, almost perfectly, cause of continuity calling of method/function move(int s1, int s2) ... it was my idea. i dont know if this is correct... but it works !!! :sunglasses:

You can download a small video from here : DOWNLOAD