Pages: [1]   Go Down
Author Topic: How to use RAW data from IR detector  (Read 1244 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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


Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8853
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8853
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This part:
Code:
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:
if (results->decode_type == NEC && results->value == 0xFFE21DUL)
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: