Controlling Combinations of Relay with IR Remote

I have a remote where there are 3 buttons and 8 relay module. I want such a condition where when 1st button is pressed then 1st relay should get energize and when 2nd button is pressed within 3 sec then 2nd relay should get energize.
But for 2nd button if it exceeds more than 3 sec, the 1st relay should get deenergize.
I am using millis() but it is not working.

//1st button===33444015
//2nd button===33478695
//3rd button===33486855

#include <IRremote.h>

const int RPIN = 7;
const unsigned int interval = 3000;
unsigned long mm = 0;

const byte Relay1Pin = 3;
const byte Relay2Pin = 4;
const byte Relay3Pin = 5;

IRrecv irrecv(RPIN);
decode_results results;

void setup()
{
  pinMode(Relay1Pin, OUTPUT);
  pinMode(Relay2Pin, OUTPUT);
  pinMode(Relay3Pin, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop()
{
  unsigned long currentTime = millis();

  if (irrecv.decode(&results))
  {
    Serial.println(results.value);
    delay(10);

    if (results.value == 33444015)
    {
      digitalWrite(Relay1Pin, LOW);

      mm = currentTime;

      if (currentTime - mm <= interval)
      {
        if (results.value == 33478695)
        {
          digitalWrite(Relay2Pin, LOW);
        }
      }
    }
    digitalWrite(Relay1Pin, HIGH);
    digitalWrite(Relay2Pin, HIGH);

    if (results.value == 33444015)
    {
      digitalWrite(Relay1Pin, LOW);

      mm = currentTime;

      if (currentTime - mm <= interval)
      {
        if (results.value == 33486855)
        {
          digitalWrite(Relay3Pin, LOW);
        }
      }
    }
    digitalWrite(Relay1Pin, HIGH);
    digitalWrite(Relay3Pin, HIGH);
    irrecv.resume();
  }
}

Please let me know if any solution for this.

Well done posting code in code tags.

1 Like

Your second if( value == ) statement is inside your first if (value == ) statement so there is no way it will ever evaluate to true.

Think of your problem more like a state machine. At startup, your state = 0, After the first button is pressed, your state goes to 1 and you note the time this occurred. If you receive your second button press, you check state and elapsed time and to the correct thing.

As an FYI... the IRRemote library is out with a new 3x version which uses different functions than .decode()... You might want to consider upgrading now rather than later.

1 Like

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