Sunday, January 8, 2012

IR TV Remote Controlled Arduino Robot!

I put that TV remote to good use again! This time used it to control a two continuous rotation servo robot. I had previously recorded the IR codes for all the remote buttons, so I made the robot check the received IR code with one in a array and then moves the servos accordingly. I used the keypad so that i could use keys 2, 4, 6, and 8 for forward, left, right, and backwards. then 1, 3, 7, 9 for forward/left, forward/right, back/left, back/right.

But for some reason i cant get the servo to vary their speed(I can stop, reverse, and forward though). The servos take values 0 to 180; 0 being reverse, 90 stop, 180 forward. If i were to set a servo to 135 for say, the servo still goes like its set to 180. same if i set it to 91, which is only barely above stop...

Pics:
Remote I used.


This is the balance bot, but it was the same basic frame and controller...


Code:

#include <IRremote.h>
long int Buttons[49] = {
  10037463,  //0Play  //
  10059903,  //1Repeat
  10088463,  //2Stop  //
  10057863,  //3PrevTrack  //
  10082853,  //4NextTrack  //
  10029303,  //5Eject //
  10055823,  //6F_REV
  10049703,  //7F_FWD
  10078263,  //8Pause
  10084893,  //9Vol_Neg  //
  10031853,  //10Vol_Pos  //
  10051743,  //11Mute //
  10054293,  //12Subtitle
  10042053,  //13Up
  10058373,  //14Left
  10091013,  //15Right
  10074693,  //16Down
  10063983,  //17Num1
  10074183,  //18Num2
  10090503,  //19Num3
  10072143,  //20Num4
  10066023,  //21Num5
  10082343,  //22Num6
  10061943,  //23Num7
  10070103,  //24Num8
  10086423,  //25Num9
  10045623,  //26Num0
  10053783,  //27Plus_10
  10056333,  //28Info
  10044093,  //29Dimmer
  10037973,  //30Angle
  10027263,  //31PlayMode
  10080303,  //32Index
  10068063,  //33TimeSearch
  10043583,  //34Resume
  10076223,  //35Remain
  10070613,  //36Zoom
  10086933,  //37SlowFwd
  10072653,  //38Setup
  10084383,  //39Guide
  10039503,  //40Menu
  10047663,  //41GoBack
  10027773,  //42Clear
  10035423,  //43A_B
  10066533,  //44Pitch_b
  10033893,  //45Pitch_S
  10033383,  //46Sound
  10078773,  //47Audio
  4294967295};  //48BTNHOLD

//-----Robot Stuff-------------------------------------------------//
int LmtrSpd=0;
int RmtrSpd=0;
#define LServoMtrPin 2
#define RServoMtrPin 3
#include <Servo.h>
Servo LServoMtr;
Servo RServoMtr;
//----------------------------------------------------------------//

//-----IR-Stuff---------------------------------------------------//

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
int BtnPressed = 0;
//----------------------------------------------------------------//
int Ttime = 0;
int Ftime = 0;
void setup()
{
  Serial.begin(9600);
  LServoMtr.attach(LServoMtrPin);
  LServoMtr.write(90);
  RServoMtr.attach(RServoMtrPin);
  RServoMtr.write(90);

  irrecv.enableIRIn(); // Start the receiver
}

void dump(decode_results *results)
{
  if (results->decode_type == NEC)
  {
    unsigned long store = (results->value);
    //Serial.println(store);
    for(int x=0; x<49; ++x)
    {
      if(store == Buttons[x])
      {
        BtnPressed = x;
       // Serial.println(x);
      }
    }
  }
}

void loop()
{
  Ttime = millis();
  if (irrecv.decode(&results))
  {
    dump(&results);
    if(BtnPressed == 18) //Fwd
    {
      Serial.println("FWD");
      LmtrSpd=180;
      RmtrSpd=0;
    }
    if(BtnPressed == 19) //Fwd Right
    {
      Serial.println("FWD+RIGHT");
      LmtrSpd=180;
      RmtrSpd=23;
    }
    if(BtnPressed == 17) //Fwd left
    {
      Serial.println("FWD+LEFT");
      LmtrSpd=113;
      RmtrSpd=0;
    }
    else if(BtnPressed == 24) //Back
    {
      Serial.println("BACK");
      LmtrSpd=0;
      RmtrSpd=180;
    }
    else if(BtnPressed == 25) //back right
    {
      Serial.println("BACK+RIGHT");
      LmtrSpd=0;
      RmtrSpd=113;
    }
    else if(BtnPressed == 23) //Back left
    {
      Serial.println("BACK+LEFT");
      LmtrSpd=23;
      RmtrSpd=180;
    }
    else if(BtnPressed == 20) //Left
    {
      Serial.println("LEFT");
      LmtrSpd=0;
      RmtrSpd=0;
    }
    else if(BtnPressed == 22) //Right
    {
      Serial.println("RIGHT");
      LmtrSpd=180;
      RmtrSpd=180;
    }
 
    else if(BtnPressed == 21) //stop
    {
      Serial.println("STOP");
      LmtrSpd=90;
      RmtrSpd=90;
    }

    LServoMtr.write(LmtrSpd);
    RServoMtr.write(RmtrSpd);

    irrecv.resume(); // Receive the next value
  }
  int Ftime = millis();
  delay(25-(Ftime-Ttime));
}

4 comments:

  1. Replies
    1. cant really understand what you're saying, but you can use geared DC motors

      Delete
  2. the above code is applicable only for servos only what about geared motar?

    ReplyDelete
    Replies
    1. what sort of driver are you using for the DC motors?

      Delete