i am trying to use the IRremote library so I can get started with remote controlling my robot.
The problem is that when I download the code below I get the error message "IRrecv irrecv(RECV_PIN);"
The code:
#include <IRremote.h>
const int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
}
void loop() {
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print("NEC: ");
} else if (results.decode_type == SONY) {
Serial.print("SONY: ");
} else if (results.decode_type == RC5) {
Serial.print("RC5: ");
} else if (results.decode_type == RC6) {
Serial.print("RC6: ");
} else if (results.decode_type == UNKNOWN) {
Serial.print("UNKNOWN: ");
}
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
As I never downloaded libraries myself I don´t know if I dit it right. For example, I don´t see the IRremote examples in the Arduino Idle. However I can see the IRremote folder downloaded.
if you don't see the library when you go to the scetch>>import library it is not installed right. lookn in your arduino scetch folder for a libraries folder. put the IRremote folder there
Thanks groundfungus for your help. You were right, I downloaded the IRremote library again and now everything is working well.
Now I am running this code:
/*
IR_remote_detector sketch
An IR remote receiver is connected to pin 2.
The LED on pin 13 toggles each time a button on the remote is pressed.
*/
#include <IRremote.h> //adds the library code to the sketch
const int irReceiverPin = 12; //pin the receiver is connected to
const int ledPin = 13;
IRrecv irrecv(irReceiverPin); //create an IRrecv object
decode_results decodedSignal; //stores results from IR detectorvoid setup()
void setup ()
{
pinMode(ledPin, OUTPUT);
irrecv.enableIRIn(); // Start the receiver object
}
boolean lightState = false; //keep track of whether the LED is on
unsigned long last = millis(); //remember when we last received an IR message
void loop()
{
if (irrecv.decode(&decodedSignal) == true) //this is true if a message has
//been received
{
if (millis() - last > 250) { //has it been 1/4 sec since last message?
lightState = !lightState; //Yes: toggle the LED
digitalWrite(ledPin, lightState);
}
last = millis();
irrecv.resume(); // watch out for another message
}
}
As I press any key on the remote control a led goes on and off. It all works well. However I want now to light the sensor only when I press a specific kew, let´s say the one with the code 1CE350AF. How should I modify the if statement in this code to do so? Thanks!
.
What I did was make a map of my remote by pushing each button and noteing the program output. On mine the last 2 digits were unique to a particular button and the two digits before were unique to the device selected on my universal remote. So for your 1CE350AF AF is the key and 50 is the device. I don't know if that's the way it is supposed to be but it works for me. Then i use the lowByte() function to get the button. then you can use if statments to perfor actions based on the key code. Even better is to use switch case to selct actions.
switch(lowByte(deCodedSignal)) {
case(175): //decimal equivalent of AF hex 0xaf might work to
digitalWrite(ledPin, LOW); // or high whichever sets the state you want
break;
}
You may need to translate form hex string to decimal, i'm not sure. it has been a while since I did the remote stuff and i'm away from my code library.
With all the help I think I am almost there. However, when I run the code with the mods in it I get the error message "error: invalid suffix "CE350AF" on integer constant".
Code;
/*
IR_remote_detector sketch
An IR remote receiver is connected to pin 12.
The LED on pin 13 toggles each time a button on the remote is pressed.
*/
#include <IRremote.h> //adds the library code to the sketch
const int irReceiverPin = 12; //pin the receiver is connected to
const int ledPin = 13;
IRrecv irrecv(irReceiverPin); //create an IRrecv object
decode_results decodedSignal; //stores results from IR detectorvoid setup()
void setup ()
{
pinMode(ledPin, OUTPUT);
irrecv.enableIRIn(); // Start the receiver object
}
boolean lightState = false; //keep track of whether the LED is on
unsigned long last = millis(); //remember when we last received an IR message
void loop()
{
switch(lowByte(1CE350AF)) {
case(175): //decimal equivalent of AF hex 0xaf might work to
digitalWrite(ledPin, LOW); // or high whichever sets the state you want
break;
}
{
if (millis() - last > 250) { //has it been 1/4 sec since last message?
lightState = !lightState; //Yes: toggle the LED
digitalWrite(ledPin, lightState);
}
last = millis();
irrecv.resume(); // watch out for another message
}
}
I know that the problem should be something about the sentence "switch(lowByte(1CE350AF))". Yet I don´t know how to get it sorted as the code 1CE350AF is the one I get from the remote control when I press the botton.
here is the code for my remote reader function. variable "device' holds which device (tv, cd, vcr) is selected on the remote. "command" holds the key. you need global variables : int device, int command. this works for my remote and should for yours since 1CE350AF is the code for channel up on my remote (RCA universal). make sure to set the RECV_PIN correctly. to map your remote just do Serial.print for command and device and note the codes for each key then set up cases for them.
#include <IRremote.h>
int device = 0;
int command = 0;
int RECV_PIN = 7; //set to pin connected to IR decoder
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if(getIR()) {
switch(command) {
case(147):
Serial.println("channel up");
break;
case(19):
Serial.println("channel down");
break;
}
}
}
boolean getIR() {
boolean valid = 0;
if (irrecv.decode(&results)) {
device = results.value >>24;
command = lowByte(results.value - device); //evaluates to 147
irrecv.resume(); // Receive the next value
if (device == 28) { // 28 is decimal for 1C of 1C E350AF
valid = 1;
}
else(valid = 0);
}
return (valid);
}
You were right regarding case(147) for Channel Up and case(19) for Channel down working well on my remote control. However, as hard as I have tried, I couldn´t get the other key values. I don´t know how to do Serial.print for command and device to get the other key codes. If you can help me also with this I will be more than grateful.
give the following a try. upload to board and then open serial monitor. if you press a key and nothing shows up then that key doesn't have a function for that device.
#include <IRremote.h>
int device = 0;
int command = 0;
int RECV_PIN = 7; //set to pin connected to IR decoder
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if(getIR()) {
Serial.print("device number ");
Serial.println(device);
Serial.print("key code ");
Serial.println(command);
Serial.println();
}
}
boolean getIR() {
boolean valid = 0;
if (irrecv.decode(&results)) {
device = results.value >>24;
command = lowByte(results.value - device); //evaluates to 147
irrecv.resume(); // Receive the next value
if (device == 28) { // 28 is decimal for 1C of 1C E350AF
valid = 1;
}
else(valid = 0);
}
return (valid);
}
The latest code you sent me to find the key codes worked fine. So I got those key codes and in a few minutes I was able to build my first remote operated rover. I'm so excited about that! Thanks a lot for your help.
Just one more question if I may: when I tried other remote controls it didn´t show any key code in serial monitor. Can yo tell me please what should I change in that code to try other remote controls and get the key codes?
Look int the examples foler of the IRremote library. try the IRrecvDump sketch. it will show the type encoding your remote uses, the 32 bit code of the key you press and some raw data that I don't know what means. Glad it worked for you. Keep experimenting!!
a word about how i decode the key presses. look at the most significant and least significant byte of the 32 bit key code. for example the code 20FFFF3A. the hsb (20) is what I use for device. 3A is the command. I have found that i don't have to fully decode. as you look at the keycodes in IRrecvDump you will see what i mean. the device (hsb) code doesn't change much but for every key a unique code. (lsb). when you want to use a different remote you need to change the line in getIR() that reads 'if(device == 28) {' replace the 28 with the decimal equivalent of the device code noted in IRrecvDump (which is a hex value) or just put 0x in front of it. per the above example it would be if(device == 32) or if(device==0x20). you can use device in switch statements, too.
When I try the IRrecvDump code with the remote control I was working with (let's call it RC1) it works fine. I get the following values:
With RC1:
Key #1:
1CE3A05F
FFFFFFFF
Key #2:
1CE3906F
FFFFFFFF
Something is different with other remote controls. For example:
With RC2
Key #1:
6CC8DCC6
4F9606B3
86E3C1BF
Key #2:
86E3C1BF
86E3C1BF
55C33DBA
This means that with the RC1 I always get the first two values ( 1C that translates rom HEX to decimal to 28). When I run the code to get device and command I get the code for each specific key just as you said. However, with RC2 it´s not so clear what the first two digits are. I tryied with 86 that translates from HEX to decimal to 134. But when I run the code to see device and commant I always get 134 as device and 57 as command even if I press different keys. So I am not able to determine command for each key. The same happens with other remote controls.
I don´t know if you can help me also on this so I am able to use other remote controls. However, two days ago I didn´t know how a remote control worked with Arduino and now I am able to run a little robot with RC1. WOW. Your help really made a difference. THANKS!
with the rc2 remote, was the encoding recognized in IRrecvDump? the first line returned has the type of encodding. have only tried a couple of remotes and both showed the type of codes that the sketch i sent uses. If it is a universal remote make sure the keys are a function that the selected device uses. by that i mean is, volume keys would not be appropriate for a DVD player. i am glad to help.
So I think IRrecvDump doesn´t recognice the encodings of RC2 and RC3. All the remote controls I use aren´t universal but one model TV remote controls instead.