I have used code from Ken Shirriff to read the raw dump from an IR remote.
He also have a simple sketch to toggle a relay, but the relay toggles no matter what button I press on the remote. How can I use the raw dump received from a specific button only to toggle the relay, or make other things happen? Basically I want to read data from a remote, and that data must be able to control a gadget. I do get HEX dumps from the different remote buttons. I could'nt really care if it decodes the correct remote, all I want is if say I receive FFE21D, then toggle the relay.
Below is a typical output from the relay toggling code.
Perhaps instead of printing out the decoded and raw data you could change the program to compare the decoded data to the data you got for the button you want to act on. Then if the data matches, toggle the relay. Perhaps if you posted your sketch we could make more specific suggestions.
Here is the sketch. I have very little knowledge of what is actually happening behind the scenes, this code is straight from Ken Shirrif. All I know is the last couple of lines is what toggles the relay.
What does this mean "result->value?" I'm not familiar with the symbol between the words.
/*
IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
An IR detector/demodulator must be connected to the input RECV_PIN.
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) { Serial.println("Could not decode message");
}
else {
if (results->decode_type == NEC) { Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) { Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) { Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) { Serial.print("Decoded RC6: ");
} Serial.print(results->value, HEX); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)");
} Serial.print("Raw ("); Serial.print(count, DEC); Serial.print("): ");
for (int i = 0; i < count; i++) {
if ((i % 2) == 1) { Serial.print(results->rawbuf_*USECPERTICK, DEC);_
} *
else {* Serial.print(-(int)results->rawbuf_USECPERTICK, DEC);_
_ }_ _ Serial.print(" ");_
_ }_ _ Serial.println("");_
_}_ void setup()
_{_
pinMode(RELAY_PIN, OUTPUT);
_ pinMode(13, OUTPUT);_ _ Serial.begin(9600);_
_ irrecv.enableIRIn(); // Start the receiver*_ } int on = 0; unsigned long last = millis(); void loop() { * if (irrecv.decode(&results)) {* * // If it's been at least 1/4 second since the last* * // IR received, toggle the relay* * if (millis() - last > 250) {* * on = !on;* * digitalWrite(RELAY_PIN, on ? HIGH : LOW);
_ digitalWrite(13, on ? HIGH : LOW);_
_ dump(&results);_
_ }_
_ last = millis(); _
_ irrecv.resume(); // Receive the next value*_ * }* } [/quote]
results->value mean "The 'value' element of the data structure whose address is stored in the pointer named 'results'.
This part:
void dump(decode_results *results)
means that the value being passed to the function is a pointer to a variable of type 'dumpcode_results' and will be known within the function as 'results'.
A structure is just a way to group several variables together. The elements of a structure can be any type and name. Look in IRremote.h to see the declaration for the 'dumpcode_results' type, the elements it contains, and the names and types of those elements.
If you are looking for the NEC-encoded button with the code FFE21D you can say this:
if (results->decode_type == NEC && results->value == 0xFFE21DUL)
In the example code I was given the data structures were all referred to using dots, i.e.
if(results.value == 0x123abc99)
Etc
Trouble is we couldn't get the comparison to work. The relay clicked for any press of the remote.
No complaints from the compiler so we assumed we are referencing the data correctly.
I'm confused by the pointer use above.
void loop() {
if (irrecv.decode(&results)) {
!!!!!!!!!!!
// IR received, toggle the relay
if (millis() - last > 250) {
on = !on;
digitalWrite(RELAY_PIN, on ? HIGH : LOW);
digitalWrite(13, on ? HIGH : LOW);
dump(&results);
}
last = millis();
irrecv.resume(); // Receive the next value
}
Where !!!!!!!! is, you need another if statement like
if (results.value == 0xFFE21DUL ){
I use switch-case for my IR receiver since there's a bunch of inputs to recognize.
switch (results.value) {
case 0x00FF906F:
digitalWrite(ledPin, ledState);
break;