Return matching array element value after check

I'm using this code to read in IR remote and check the decoded data against an array of values, but as it presently stands, it is returning 0 every time.

Anybody see anything obvious?

Thanks.

#include <IRremote.h>

const int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long irCodes[17] = 
{0xFFA25D, 0xFF629D, 0xFFE21D, 0xFF22DD, 0xFF02FD, 0xFFC23D, 0xFFE01F, 0xFFA857, 0xFF906F, 0xFF9867, 0xFF6897, 0xFFB04F, 0xFF18E7, 0xFF4AB5, 0xFF10EF, 0xFF5AA5, 0xFF38C7};

unsigned long receivedCode;
unsigned long currentCode;

void setup(){
  Serial.begin(115200);
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

int checkCode(unsigned long irCode){
  for (int i = 0; i < 16; i++) {
  if(irCodes[i]==irCode, HEX){
    return i;
  }
  else {
    return 0;
  }
  }
  }



void loop(){
  if (irrecv.decode(&results)){
    receivedCode = results.value;
    irrecv.resume();
      Serial.println(checkCode(receivedCode));

        //if(receivedCode != 0xFFFFFFFF){
       // currentCode = receivedCode;
        //Serial.println(currentCode, HEX);
        //}
        //if(receivedCode == 0xFFFFFFFF){
       // Serial.println(currentCode, HEX);
    }
  }


Perhaps that means your 'if' statement in the checkCode() functions never evaluates to TRUE?

I wonder what the ", HEX" does in that if statement.

Why don't you want to test for the last value?

0 through 16 is 17 values, is that incorrect?

Your 'for' loop terminates at 15.

I suppose <= is the correct syntax.

Now, here's a problem I've never seen. I wish I could say that about the solution rather than the problem, but I've got this code:

#include <IRremote.h>

const int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long irCodes[17] = 
{0xFFA25D, 0xFF629D, 0xFFE21D, 0xFF22DD, 0xFF02FD, 0xFFC23D, 0xFFE01F, 0xFFA857, 0xFF906F, 0xFF9867, 0xFF6897, 0xFFB04F, 0xFF18E7, 0xFF4AB5, 0xFF10EF, 0xFF5AA5, 0xFF38C7};

unsigned long receivedCode;
unsigned long currentCode;

void setup(){
  Serial.begin(115200);
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

int checkCode(unsigned long irCode){
  for (int i = 0; i <= 16; i++) {
if(irCode == irCodes[i]) {
  Serial.print(i);
}
  }

}


void loop(){
  if (irrecv.decode(&results)){
    receivedCode = results.value;
    irrecv.resume();
      Serial.println(checkCode(receivedCode));

        //if(receivedCode != 0xFFFFFFFF){
       // currentCode = receivedCode;
        //Serial.println(currentCode, HEX);
        //}
        //if(receivedCode == 0xFFFFFFFF){
       // Serial.println(currentCode, HEX);
    }
  }
 

And am getting this, every time the program detects IR data:

The more common method is:

for (int i = 0; i < 17; i++) {

The error message is correct. That function was deprecated with a major update about 18 months ago. See the ReadMe at the library's GitHub page.

try this code

#include <IRremote.hpp>
const byte IR_RECEIVE_PIN = 10;

void setup() {
  Serial.begin(115200);
  IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
  Serial.println(F("Ready"));
}

void loop() {
  if (IrReceiver.decode()) {
    IrReceiver.resume(); // Enable receiving of the next value
    Serial.print("IrReceiver.decodedIRData.command = 0x"); Serial.println(IrReceiver.decodedIRData.command, HEX);
    Serial.print("IrReceiver.decodedIRData.address = 0x"); Serial.println(IrReceiver.decodedIRData.address, HEX);
    if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) Serial.println("IS REPEAT FLAG");
    Serial.println("----");
  }
}

does it compile for you?
open the Serial monitor at 115200 bauds. What do you see when you press buttons on your remote?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.