Press and hold button on ir remote.

I saw a thread from pepelepoe: Press and Hold IR Remote Control Button - Sensors - Arduino Forum

And Martin-X had a good idea, but for only one button only. When replicating and modifying it, it doesn't seem to work now. Maybe somebody can help me?

Thank you

#include <IRLib.h>

#define RELAY_ON 1
#define RELAY_OFF 0

#define bluePin  23    //blue
#define redPin 25
#define yellowPin 27

#define mode 0xFF629D
#define power 0xFFA25D
IRrecv My_Receiver(2);//receiver on pin 11
IRdecode My_Decoder;//Decoder object

int state = 0;
unsigned long lastButtonTime = millis();
//byte IRcode;

const int timeoutDelay = 100;  // milliseconds - I'm guessing here, this needs to be longer than the IR repeat interval

void setup() {
  Serial.begin(9600);
  My_Receiver.enableIRIn(); // Start the receiver
  digitalWrite(bluePin, RELAY_OFF);
  pinMode(bluePin, OUTPUT);
  digitalWrite(redPin, RELAY_OFF);
  pinMode(redPin, OUTPUT);
  digitalWrite(yellowPin, RELAY_OFF);
  pinMode(yellowPin, OUTPUT);
  
  delay(10); //Check that all relays are inactive at Reset
}

void loop()
{
  //Serial.println(My_Decoder.value, HEX);
  if (My_Receiver.GetResults(&My_Decoder)) {
    My_Decoder.decode();
    if (My_Decoder.decode_type == NEC) {
      //Serial.println(My_Decoder.value, HEX);
      if (My_Decoder.value == mode)
      {
        digitalWrite(bluePin, RELAY_ON);
        state = 1;
        lastButtonTime = millis();
        //Serial.print("on");
      }

      else if (My_Decoder.value == REPEAT && state == 1)
      {
        lastButtonTime = millis();  // update the time-out
      }

    }
    My_Receiver.resume(); //Restart the receiver
  }

// Turn off relay...

  if ((millis() - lastButtonTime > timeoutDelay) && state == 1)
  {
    digitalWrite(bluePin, RELAY_OFF);
    //Serial.print("off");
    state = 0;
  }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Serial.println(My_Decoder.value, HEX);
  if (My_Receiver.GetResults(&My_Decoder)) {
    My_Decoder.decode();
    if (My_Decoder.decode_type == NEC) {
      //Serial.println(My_Decoder.value, HEX);
      if (My_Decoder.value == power)
      {
        digitalWrite(redPin, RELAY_ON);
        state = 1;
        lastButtonTime = millis();
        //Serial.print("on");
      }

      else if (My_Decoder.value == REPEAT && state == 1)
      {
        lastButtonTime = millis();  // update the time-out
      }

    }
    My_Receiver.resume(); //Restart the receiver
  }

// Turn off relay...

  if ((millis() - lastButtonTime > timeoutDelay) && state == 1)
  {
    digitalWrite(redPin, RELAY_OFF);
    //Serial.print("off");
    state = 0;
  }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //void loop()

Please remove your code from the other post and refer to this post. You should only have one post for a subject with description and questions to avoid multiple people looking into your issue and wasting time. It is called cross posting and against the forum rules. Your chance to fix it before the professional admins find out. :slight_smile: It is OK to have the link to the original post. You can make it a clickable link when you are at it.

Also please help people to help you. Show them you have done some work and let them know what works. Did you get the single button stuff working before you started modifying it?

It might be useful to comment the code in a way that it is easy to see what you modified. A simple schematic and a picture of your system might help people understand what you want to achieve. Make sure the images are shown in the post. Read the "How to post" at the beginning of each sub forum for additional tips. You used code tags, that is a good start. :slight_smile:

I'm sorry, I already deleted it.

I got the first one working without copying the code for the second button. But when directly copying it, it still won't give the same output. Still trying to analyze the code.