I've been trying to get my IR receiver to work, but I can't for the life of me figure out how to get it to work.
I've been trying to use the keycodes that I got using the following code:
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
delay(100);
irrecv.resume(); // Receive the next value
}
}
I would get codes like:
83228976
8322807F
8322837C
83228F70
8322837C
8322817E
83228778
I tried putting them in a switch like so:
#include <IRremote.h>
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop() {
if(irrecv.decode(&results)) {
switch(results.value, HEX) {
case 83228B74:
Serial.println("You pressed: 1");
irrecv.resume();
break;
case 83228F70:
Serial.println("You pressed: 2");
irrecv.resume();
break;
default:
Serial.print("Button not recognized");
irrecv.resume();
break;
}
}
}
But that didnt work so i've tried putting the cases in quotes, using #define, and making variables for them, and using if/else if's, that didn't work either.
Tried using raw values, and some more things.
Also tried multiple remotes and boards.
Can anyone please help me figure this out.
If i'm missing any info, do let me know.
void loop() {
if(irrecv.decode(&results)) {
switch(results.value) { // get rid of HEX
case 0x83228B74: // 0x is the prefix for hex numbers
I've tried that too, sorry for not mentioning.
When I tried this it wasn't responding to any on the buttons. I also tried outputting keycodes without the , HEX and I would get codes like: 2200079220
Always post the complete code under discussion.
I did post the complete code, just crtl+A, ctrl+c'd it. Also edited the previous reply because I made a mistake with the code to reproduce the error.
NO, you didn't. Post the code that corresponds to "I tried that too", which is obviously different.
Did you remove the HEX from the switch(results.value, HEX) { line?
This compiles with no errors or warnings and works with my Uno and IR receiver (codes are different because of my remote).
#include <IRremote.h>
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop()
{
if (irrecv.decode(&results))
{
switch (results.value)
{
case 0x1CE3807F:
Serial.println("You pressed: 1");
break;
case 0x1CE340BF:
Serial.println("You pressed: 2");
break;
case 0xFFFFFFFF:
Serial.println("Repeat code");
break;
default:
Serial.println("Button not recognized");
break;
}
irrecv.resume();
}
}
Oops, sorry, this doesn't gives me errors though, this just doesn't respond to button presses.
#include <IRremote.h>
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop() {
if(irrecv.decode(&results)) {
switch(results.value) {
case 0x83228B74:
Serial.println("You pressed: 1");
irrecv.resume();
break;
case 0x83228F70:
Serial.println("You pressed: 2");
irrecv.resume();
break;
default:
Serial.print("Button not recognized");
irrecv.resume();
break;
}
}
}
Okay fixed it, it wasn't recognizing because I copied the code to a different window of vsc.
In that window the baud-rate was not set to 9600. So I changed that and it worked.