ir remote - check if 2 hex codes are after each other

Hi Fellow Programmers,

I've a working code to move a servo with a remote. I push * and it moves to a position, i push # and it moves to another position.

My question is, how can I program, that the servo should only move if i push 2 specific buttons after each other? Like a secret combination.

Any ideas? My idea was to put the two pushed hex codes in an array, and check if they meet my criteria, but I dont know how to program it.

Thanks for any help! :slight_smile:

because there is no posted sketch or code, additional work needs to be done:

byte prevIR;
byte newIR;
void loop()
{

if(IR.available())
 newIR = IR.readByte();

if((newIR == 0x01) && (prevIR == 0x02)) //Adjust 0x01 and 0x02 to what you want it...
{
//Do your stuff here
}
prevIR = newIR;

wow, that was quick :slight_smile: Thanks!
I'll try it at home, and will get back with the full code.

cybercy:
will get back with the full code.

Next time, please start with that... The thing with getting a good reply is you need to provide good information, I don't have a crystal ball. And if I did had one, I surely wouldn't go around and checking other people's codes!!!

The IR values, if you are using Ken's IR library are unsigned longs, not bytes.

:frowning: I cannot get it to work. Here is my full code. There are no syntax errors, it compiles okay. But looks like it cannot compare my hex values? (I removed the servo moving part, instead there is just a string for testing)

#include <IRremote.h>
#include <IRremoteInt.h>
#include <Servo.h> 

Servo myservo;  
 
int pos = 30;    
int receiver = 2; 
int newIR = 0;    
int prevIR = 0; 

IRrecv irrecv(receiver);           
decode_results results;           

void setup()   {
  Serial.begin(9600);
  Serial.println("serial monitor works");
  irrecv.enableIRIn(); 
   myservo.attach(13);  
}

void loop()   
{
  if (irrecv.decode(&results)) 
  {
    newIR = results.value;
            if((newIR == 0xFF42BD) && (prevIR == 0xFF52AD))
                    {
                    Serial.println("this should show up if it works"); 
                    }
    prevIR = newIR;
    Serial.println(results.value, HEX); 
    Serial.println("this button was pressed"); 
    Serial.println(newIR, HEX); 

    delay(500); 
    irrecv.resume();
  }  
}

okay, finally I managed to figure out, using the draft of C-F-K. Thanks again. Here is my working code:

#include <IRremote.h>
#include <IRremoteInt.h>
#include <Servo.h> 

Servo myservo;  
 
int pos = 30;    
int receiver = 2; 
String newIR = "";    
String prevIR = ""; 


IRrecv irrecv(receiver);           
decode_results results;           

void setup()   {
  Serial.begin(9600);
  Serial.println("serial monitor works");
  irrecv.enableIRIn(); 
   myservo.attach(13);  
}

void loop()   
{
  if (irrecv.decode(&results)) 
  {
    newIR = String(results.value);

   
            if((newIR == "16728765") && (prevIR == "16732845"))
                    {
                    
                    Serial.println(" open lock"); 
                    myservo.write(150);
                    }
            if((newIR == "16726215"))
                    {
                   
                    Serial.println(" close lock"); 
                    myservo.write(100);
                    }
    
    Serial.println(results.value, HEX); 
    Serial.println("this button was pressed"); 
    Serial.println(newIR); 

    delay(500); 
    irrecv.resume();
    prevIR = newIR;
  }  
}
String newIR = "";    
String prevIR = "";

Why are you converting an unsigned long to a string, and then wrapping it in a String object?

cybercy:
:frowning: I cannot get it to work. Here is my full code. There are no syntax errors, it compiles okay. But looks like it cannot compare my hex values? (I removed the servo moving part, instead there is just a string for testing)

Try it again, but use this instead:

unsigned long newIR = 0;
unsigned long prevIR = 0;

As what PaulS said, they need to be unsigned long int's. A normal int is only 2 bytes, so 0xFF42BD doesn't even fit. An unsigned long is 4 bytes, so it'll fit just snug!