Control Arduino with Universal Remote

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:

// 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:

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

Thanks for the Library!

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

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?

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.

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.

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

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!

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

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(distanceSERVO_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);
_ }
}*_

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

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

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.

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?

The code was written for version 0011 of the IDE. You are running version 0012.

There are a few fixes floating around on the forum. Check the last post here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1221741304/52#52

But also do some searching around.

ladyada has a version of servotimer1 that works with 0012 here: http://www.ladyada.net/media/mshield/ServoTimer1-fixed.zip

No that code worked fine for me, make sure you've got the NECIR library installed.

I have the NECIRrcv.h library added to the Arduino sw but still get compiling errors when compiling the small program that is listed above:

In file included from C:\Program Files\arduino-0012\hardware\cores\arduino/WProgram.h:4,

c:/program files/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected unqualified-id before 'int'

c:/program files/arduino-0012/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:80: error: expected `)' before 'int'

......

c:/program files/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:\Program Files\arduino-0012\hardware\cores\arduino/WProgram.h:6,

Do I need some other library? WProgram?

Please help... I am totally new and "desperately" need a working NEC IR receiving sketch.

Thanx...

did you check out the what trialex posted above?