Pages: [1] 2 3   Go Down
Author Topic: Control Arduino with Universal Remote  (Read 30305 times)
0 Members and 1 Guest are viewing this topic.
Columbus, Ohio USA
Offline Offline
Full Member
***
Karma: 3
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a library that allows the Arduino to receive IR codes from a universal remote:

http://wikiputer.org/arduino/NECIRrcv.zip

The library implements the NEC IR protocol, so set up the universal remote for any NEC device (TV, DVD, VCR, ...) and the 32-bit codes should be received.

The example sketch provided just prints out IR codes as they're received, so it's good to run that sketch to see what codes the remote is sending, so they can later be used to trigger specific actions:

Code:
// look for IR codes and print them as they are received

#include <NECIRrcv.h>
#define IRPIN 4    // pin that IR detector is connected to

NECIRrcv ir(IRPIN) ;

void setup()
{
  Serial.begin(9600) ;
  Serial.println("NEC IR code reception") ;
  ir.begin() ;
}

void loop()
{
  unsigned long ircode ;
  
  while (ir.available()) {
    ircode = ir.read() ;
    Serial.print("got code: 0x") ;
    Serial.println(ircode,HEX) ;
  }
}

The library is implemented to be similar to the Serial library functions 'begin,' 'available,' 'flush' and 'read.' Each IR code is 32-bits long (fits in an 'unsigned long' data type) and there's a buffer that can hold up to 8 codes before overflowing.

Timer 2 is used by the library.

Hardware required in addition to the Arduino is a universal remote control (any cheap one will do) and an IR detector (I used Vishay TSOP1138, $4 from http://www.rentron.com/remote_control/PNA4602M.htm#1138). The recommended circuit for the TSOP1138 includes a 4.7 uF cap and a 100-ohm resistor (see: http://www.rentron.com/Files/TSOP11xx.pdf. Pin 3 of the detector (active low) is directly connected to an Arduino input pin.

For example, I set up a $6 GE universal remote to one of its NEC TV modes and get this output from the example sketch after pressing the numbers 0-9 on the remote:

Code:
NEC IR code reception

got code: 0xB24DE718
got code: 0xB946E718
got code: 0xB54AE718
got code: 0xB748E718
got code: 0xB847E718
got code: 0xB44BE718
got code: 0xB649E718
got code: 0xB04FE718
got code: 0xB34CE718
got code: 0xB14EE718

Joe

« Last Edit: May 08, 2008, 05:51:11 am by jmknapp » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the Library!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

this is pretty sweet.. i can make a remote control option for my robot. smiley-grin
Logged

http://lroy12.blogspot.com/ - Arduino Robot Build!

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello i am a noob. how do i use the downloaded library file? do i have to put it somewhere in the program files so the arduino program can use it?
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.arduino.cc/en/Reference/Libraries
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a particular version of the Arduino IDE that this library works with, because I can't run the library with 0010 or 0011 without getting an Error building library "NECIRrcv" then it gives me the following:

In file included from C:\Documents and Settings\User\My Documents\arduino-0011\hardware\cores\arduino/WProgram.h:6,
                 from NECIRrcv.cpp:4:
c:/documents and settings/user/my documents/arduino-0011/hardware/tools/avr/bin/../avr/include/avr/signal.h:36:2: warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>."
NECIRrcv.cpp: In member function 'void NECIRrcv::begin()':
NECIRrcv.cpp:15: error: 'TCCR2A' was not declared in this scope
NECIRrcv.cpp:20: error: 'TCCR2B' was not declared in this scope
NECIRrcv.cpp:25: error: 'TIMSK2' was not declared in this scope

Any ideas what is going wrong?

Thanks for the help in advance.
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 572
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ignoring the warning you got (that always comes up when signal.h is used) the errors are saying that the various registers are not named correctly.

This means either

(a) the library was written for an arduino with an AtMega168 chip and you are using an arduino with an AtMega8 chip
or
(B) the library was written for an arduino with an AtMega8 chip and you are using an arduino with an AtMega168 chip

You can either go through the datasheets for the chips and correct the register names (not that hard, and you'll have a better understanding of what goes on in that black bit of plastic) or you can swap what chip you are using.
Logged

Seattle
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This should be enough for you to get the code to compile:

http://gonium.net/md/2007/04/18/tweaking-the-code/

I haven't tested this; but it's the information to resolve the differences in timer naming between the atmega8 and atmega168.

-harry
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This library works great for me using the sample program to print out the remote codes.
But, when I try to integrate it into my object-avoiding robot, things go bad.  One of two motors stops completely, the other becomes intermittent.  The stepper motor used for vision control "drifts" when not being driven, like it is being disconnected for short periods of time.  I am using the servotimer1 library, but that just uses timer 1 and it looks like NECIRrcv is using timer 2.

Any idea what could be going wrong?
Any idea how much RAM this library uses?

Thanks!
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any idea how much RAM this library uses?

Here is a thread with 2 different functions to determine how much ram a program is using if that helps.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720;start=all
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I don't think it is a memory problem.  I tried two different memory tests down in the innermost loop.  One showed 638 bytes free, the other, 697.  

For reference, here's my code, I'd sure appreciate the help.  I wonder if the NECIRrcv library is colliding with servoTimer1 in some way.  When I uncomment the ir.begin() line, things go bonkers.


#include <ServoTimer1.h>
#include <NECIRrcv.h>

int eyePin = 0;
int servoPin = 10;
int bumperPin = 7;
int motor_leftdig=4;
int motor_leftpwm=5;
int motor_rightdig=12;
int motor_rightpwm=11;
int irPin = 8;

/* Object constants */
#define DANGER_DISTANCE     500
#define MAX_SAFE_DISTANCE   400

/* Servo motor contstants */
#define SERVO_STEP_SIZE     5      
#define NUM_SERVO_STEPS    36
#define STRAIGHT_AHEAD     18  

/* Motor speeds.  PWM output pins turn 0-255 into 0-5v */
#define  NO_SPEED      0
#define  HALF_SPEED  127
#define  FULL_SPEED  255

#define  ON  0
#define OFF  1

int view[NUM_SERVO_STEPS];
ServoTimer1 servo;
NECIRrcv ir(irPin);

void setup() {
  pinMode(motor_leftdig, OUTPUT);
  pinMode(motor_rightdig, OUTPUT);
  servo.attach(servoPin);
  moveServo(STRAIGHT_AHEAD);
//  Serial.begin(9600);  /* For debugging purposes only */
//  ir.begin();

}

void loop() {
  static int remoteControl = OFF;

  if (remoteControl == ON) {
      stopMoving();
  }
  else {  // Remote control = OFF

    /* If we hit something with the bump switch, back up and turn around */
    if (digitalRead(bumperPin) == HIGH) {
      stopMoving();
      reverse();
      delay(1000);
      stopMoving();
      turnAround(bestDirection());
    }
  
    /* Debounce the Sharp IR sensor.  I suspect a capacitor would work just as well... */
    int distance = analogRead(eyePin);
    while (abs(distance - analogRead(eyePin)) > 20) {
      distance = analogRead(eyePin);  /* Debounce */
    }

    /* Too close to something?  Stop and turn around */
    if (distance > DANGER_DISTANCE) {
      stopMoving();
      turnAround(bestDirection());
    }
    
    /* Getting close to something, slow down */
    else if (distance > MAX_SAFE_DISTANCE) {
      halfSpeed();
    }
    
    /* All clear ahead, full on */
    else {    
      moveStraight();
    }
  }
}

void moveServo(int distance) {
  servo.write(distance*SERVO_STEP_SIZE);
  delay(50);  /* For the servo to settle */
}



/*
* Routine to figure out where to rotate to get the most unobstructed angle.
 */
int bestDirection() {
  /*
   * Rotate the servo, building a map of the obstacles we see in front of us.
   * Record the position that is most unobstructed, scaled up to use as a delay factor when turning.
   */
   refresh_view(0, NUM_SERVO_STEPS);
   int safest = furthestView();
   return ((safest - STRAIGHT_AHEAD) * 50);
}


/* Routine to return the least number in the view[] array. */
int furthestView() {
   int furthest=view[0];
   int servo_position=0;
  
   for (int i=0; i<NUM_SERVO_STEPS; i++)  {
     if (view < furthest) {
       furthest = view;
       servo_position = i;
     }  
    }
   return(servo_position);  
}

/* Routine to scan around, recording what we see where */
void refresh_view(int start, int end) {  
  for (int i=start; i<end; i++) {
    moveServo(i);
    /* Debounce the Sharp IR sensor */
    while (abs(analogRead(eyePin) - view) > 10) {
      view = analogRead(eyePin);
    }
  }
  moveServo(STRAIGHT_AHEAD);
}  


/* Negative turnAmount means turn left, positive turnAmount means turn right */
void turnAround(int turnAmount) {
  if (turnAmount < 0) {
    setSpeed(-FULL_SPEED, FULL_SPEED);
  }
  else {
    setSpeed(FULL_SPEED, -FULL_SPEED);
  }
  delay(500);  /* Arbitrary delay factor, it just works well */
}

void moveStraight() {
  setSpeed(FULL_SPEED, FULL_SPEED);
}

void stopMoving() {
  setSpeed(NO_SPEED, NO_SPEED);
  delay(500);
}

void halfSpeed() {
  setSpeed(HALF_SPEED, HALF_SPEED);
}


void reverse() {
  setSpeed(-FULL_SPEED, -FULL_SPEED);
  delay(500);
}

void setSpeed(int left, int right) {
   if (left >= 0) {  /* Forward */
      digitalWrite(motor_leftdig, LOW);
      analogWrite(motor_leftpwm, left);
    }
    else {  /* Reverse */
      digitalWrite(motor_leftdig, HIGH);
      analogWrite(motor_leftpwm, FULL_SPEED+left);
    }


    if (right >= 0) {  /* Forward */
      digitalWrite(motor_rightdig, LOW);
      analogWrite(motor_rightpwm, right);
    }
    else {  /* Reverse */
      digitalWrite(motor_rightdig, HIGH);
      analogWrite(motor_rightpwm, FULL_SPEED+right);
    }
}

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does this work with a infrared LED reciever?   what resistor should i use??
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, i am very new to arduino and programming. i have attempted to replicate the code above but for some unknown reason i get this really weird error message:
"
In file included from C:\Documents and Settings\Adam\Desktop\arduino-0012\hardware\cores\arduino/WProgram.h:4,


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected unqualified-id before 'int'


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected `)' before 'int'


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected `)' before 'int'


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected identifier before '(' token


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected `)' before '(' token


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected ',' or '...' before '(' token


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:144: error: expected initializer before ')' token


c:/documents and settings/adam/desktop/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:176: error: '__compar_fn_t' has not been declared


In file included from C:\Documents and Settings\Adam\Desktop\arduino-0012\hardware\cores\arduino/WProgram.h:6,

"
and i dont know what is going on because it doesnt even mention anything to do with the NECIRrcv library.
any possible ideas on how to overcome this?
thanks
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe a syntax error of some kind?  Did you cut and paste the code exactly as is, or type it in yourself?  If the latter, post your code and maybe I can spot the problem.  By the way, I got this working perfectly, basically by making sure the servo was stopped anytime I was reading the receiver.  I think there was some interference going on.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, sorry i didnt mean to confuse but i copied and pasted the original code posted by jmknapp  and for some reason recieved those errors. i copied and pasted exactly and cannot get it to work.
any ideas?
Logged

Pages: [1] 2 3   Go Up
Jump to: