ir remote help

how would i go about using an ir remote with the arduino i already have the nec library or what ever its called but when i try to use the code that gets printed out to the serial port something goes wrong.

heres the code

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

#include <WProgram.h>
#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) ;
  }
}

when i try to use the code that gets printed out to the serial port something goes wrong.

Really? Something goes wrong?

Was it you who launched the missles from North Korea into the Pacific Ocean? Or did you start the earthquake in New Ginuea? Oh I know, when you plug it in.. you shot Kennedy!

So, what does "something goes wrong" mean? What happens? What have you tried to fix it?

:)

i tried 2 other sketches for different remotes and cant get them to work at all and i try to ad an if statement but sometimes it wont compile and when it does it wont do any thing when the code is recived except print it out like its supposed to do. im using an old small credit card style dvd remote and i get the sam code each time i press the button so i dont know why it wont work. but im new at this

when it does it wont do any thing when the code is recived except print it out like its supposed to do.

So... wow, I'm so confused? lol

It's working like it's supposed to, but you're trying to add some if statements? Could you explain what you're trying to do.

You say that each time you push a button you get the same code... .... that's correct. Unless you hold the button down, some remotes will send a different code when held down. Does it show a different code when you push a different button?

sorry if im not explaing it clearly it is reciveing the code but it wont do anything. im trying to move a servo and turn a light on and off

Okay, that makes more sense.

How about posting the code you have so far, and we'll see what we can do. But I suggest you work on 1 thing at a time.. LEDs would be your best bet... after you're able to light that up, then advance to servos.

But you'll want to try something like, after you read from the receiver... you get the code or whatever, then you do an if statement..

if(ircode == '04A1cBe1') { digitalWrite(led, HIGH); }

just an idea. That's probably not the right code for your remote.. and if it is, well, I feel special. :)

ok i got it to come on but when i try to add another if statement to turn it off it wont come on at all here is my code

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

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

NECIRrcv ir(IRPIN) ;
int led = 2;
int ledlow = 3;

void setup()
{
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  digitalWrite(ledlow, LOW);
  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) ;
    if(ircode == 'FC0300FF'); 
    digitalWrite(led, HIGH);

     if(ircode == 'EC0200FF'); 
    digitalWrite(led, LOW);
}
  
}

Try move the if statements outside of the While statement.

Or, even better, use a Switch statement, but make sure it's outside of the While statement.

Look under File->Examples->Control, and look at the two switch statement examples.

so how would i go about maping my remote as far as i can see the serial example only reads the first byte the is recived

The serial example is.....

only for that.. serial.

You can do a switch on almost anything... Just an example:

switch(ircode){ case 'FC0300FF': digitalWrite(led, HIGH); break; // break just ends the case-statement, so it will pass all the others below. case 'EC0200FF': digitalWrite(led, LOW); break; }

i get this error

In function 'void loop()': error: duplicate case value but my 2 values are different by 1 number

switch(ircode){
   case 'FC0300FF':
      digitalWrite(led, HIGH);
      break;  // break just ends the case-statement, so it will pass all the others below.
   case 'FC0200FF':
      digitalWrite(led, LOW);
      break;

You declared ircode to be an unsigned long. Therefore, the case statement values are being converted to unsigned longs. The conversion of both strings to unsigned longs results in a value of 0.

Oh wow.. totally forgot about that..

You're going to need to use if statements.. somebody explained why you can use the switch statement with the values.. but I don't recall exactly.

You might be able to get around it by setting the values at the top: byte ledOn = 'FC0300FF'; byte ledOff = 'blahblah'; Again, not sure if that will work.

But if not, then just go back to using if-statements, but outside of the while statement. :)

so how would i chane that just change it to an int ?

and how do i move the if statement out of the while? i am still new to the arduino platform but i appreciate all the help

You are only reading one byte. The ircode variable then should be a byte. The actual value received will be printed. It's those values that you see printed that are the case values.

If you see "got code 0x14" in the serial monitor, then the case statement would read "case 0x14:".

ok so i just add those 2 chars to my case statement and it should work?

Good Captain the use of:-

byte ledOn = 'FC0300FF';

Will not work a byte is only 8 bits that is two hex digits. To get 0xFC0300FF into a variable you need either 4 bytes or 2 ints or 1 long int.

ok so how do i go about adding any functions with this ir code ? it seems as if everyone is saying something different

it seems as if everyone is saying something different

Well if you will cross post you will get what you deserve. >:(