Go Down

Topic: How to use RAW data from IR detector (Read 2499 times) previous topic - next topic


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.

Code: [Select]
Decoded NEC: FF42BD (32 bits)
Raw (68): 25830 9050 -4450 600 -550 550 -600 550 -550 600 -500 600 -550 600 -550 550 -550 550 -550 600 -1700 500 -1700 600 -1700 550 -1650 550 -1750 550 -1700 550 -1600 600 -1750 550 -550 550 -1700 550 -600 500 -550 600 -550 600 -550 500 -1750 550 -550 550 -1650 600 -550 550 -1750 550 -1700 550 -1650 550 -1700 600 -550 500 -1700 600


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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


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.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com

#include <IRremote.h>

int RECV_PIN = 11;
int RELAY_PIN = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;

// 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(" ");

void setup()
  pinMode(13, OUTPUT);
  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);
    last = millis();     
    irrecv.resume(); // Receive the next value


results->value mean "The 'value' element of the data structure whose address is stored in the pointer named 'results'.

This part:
Code: [Select]
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:

Code: [Select]

if (results->decode_type == NEC && results->value == 0xFFE21DUL)
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


In the example code I was given the data structures were all referred to using dots, i.e.
if(results.value == 0x123abc99)
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.


are you working with the OP?

No technical PMs.
The last thing you did is where you should start looking.


Code is doing exactly what you told it to do.

If any results are received, toggle relay.

Code: [Select]
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);
    last = millis();     
    irrecv.resume(); // Receive the next value

Where !!!!!!!! is, you need another if statement like

Code: [Select]
if (results.value == 0xFFE21DUL ){

I use switch-case for my IR receiver since there's a bunch of inputs to recognize.

Code: [Select]
    switch (results.value) {
      case 0x00FF906F:
        digitalWrite(ledPin, ledState);

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131