ir remote relay control

Need help with code

I like too do this:

I press on the IR remote button (1) then :

// Reley going too turn on and off
digitalWrite(rele1, HIGH);
digitalWrite(rele2, HIGH);
delay(5000);
digitalWrite(rele2, LOW);
digitalWrite(rele1, LOW);
delay(5000);

Then Repeat from the start. Until I press the button again (IR remote) or press other button

LED blink script + IR remote too turn the script on and off

Here is code:

#include <IRremote.h>

int RECV_PIN = 11;
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5;

int on = 0;
int on1 = 0;
int on2 = 0;
int on3 = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(relay4, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(13, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}


unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results))
  {
    if (results.value == 0xF171) 
    { // Remote Control Power Code
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) 
      {
        on = !on;
        digitalWrite(relay1, on ? HIGH : LOW);
      }
      last = millis();
    }    




    else if (results.value == 0xF1B1) 
    {
      if (millis() - last > 250) 
      {
        on1 = !on1;
        digitalWrite(relay2, on1 ? HIGH : LOW);
      }
      last = millis();
    }


    else if (results.value == 0xF1A1) 
    {
      if (millis() - last > 250) 
      {
        on2 = !on2;
        digitalWrite(relay3, on2 ? HIGH : LOW);
      }
      last = millis();




    }


    else if (results.value == 0xF121) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay4, on3 ? HIGH : LOW);
      }
      last = millis();

    }
    irrecv.resume(); // Receive the next value
  }
}

This should get you started:

#include <IRremote.h>

int RECV_PIN = 2; //IR receive pin goes here
int relay_1 = 13; //relay driver goes here

IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long last = millis();
boolean flag = false;

void setup()
{
  pinMode(relay_1, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) 
  {
    if (flag == false && results.value == your_HEX_code)
    {
      digitalWrite(relay_1, HIGH);  
      last = millis();
      flag = true;
    }
    irrecv.resume(); // Receive the next value
  }

  if(flag == true && millis() - last >= 5000)
  {
    digitalWrite(relay_1, LOW);  
    flag = false;
  }

  // other non blocking code goes here


} //END of loop()

Then Repeat from the start. Until I press the button again (IR remote) or press other button

You'll need to hold the remote button for a long time.

Hi. Is not working. Is not turn relay on and of and on etc. But just turn it on.

The shuld be like a led blink script just use IR remote too turn it on or off

LarryD:
This should get you started:

#include <IRremote.h>

int RECV_PIN = 2; //IR receive pin goes here
int relay_1 = 13; //relay driver goes here

IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long last = millis();
boolean flag = false;

void setup()
{
  pinMode(relay_1, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results))
  {
    if (flag == false && results.value == your_HEX_code)
    {
      digitalWrite(relay_1, HIGH); 
      last = millis();
      flag = true;
    }
    irrecv.resume(); // Receive the next value
  }

if(flag == true && millis() - last >= 5000)
  {
    digitalWrite(relay_1, LOW); 
    flag = false;
  }

// other non blocking code goes here

} //END of loop()

Works fine here. - What changes have you made to the sample sketch? - How do you have things wired?

It work. But the led/reley is not turn it on and off and on and off etc. Just turn on and off.

I like too turn the on. Then the automatic turn the relay on and off and repeat that too I turn it off

There:

#include <IRremote.h>

int RECV_PIN = 11; //IR receive pin goes here
int relay_1 = 2; //relay driver goes here

IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long last = millis();
boolean flag = false;

void setup()
{
  pinMode(relay_1, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) 
  {
    if (flag == false && results.value == 0xFF18E7)
    {
      digitalWrite(relay_1, HIGH);  
      last = millis();
      flag = true;
    }
    irrecv.resume(); // Receive the next value
  }

  if(flag == true && millis() - last >= 5000)
  {
    digitalWrite(relay_1, LOW);  
    flag = false;
  }

  // other non blocking code goes here


} //END of loop()

Try this.
What do you see on the serial monitor?

#include <IRremote.h>

int RECV_PIN = 11; //IR receive pin goes here
int relay_1 = 2; //relay driver goes here

IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long last = millis();
boolean flag = false;

void setup()
{
  Serial.begin(9600);
  pinMode(relay_1, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) 
  {
    Serial.println(results.value,HEX);
    
    if (flag == false && results.value == 0xFF18E7)
    {
      digitalWrite(relay_1, HIGH);  
      last = millis();
      flag = true;
    }
    irrecv.resume(); // Receive the next value
  }

  if(flag == true && millis() - last >= 5000)
  {
    digitalWrite(relay_1, LOW);  
    flag = false;
  }

  // other non blocking code goes here


} //END of loop()

This if I turn it on:

FFFF18E7 FFFFFFFF

But the led/reley is not turn it on and off and on and off etc. Just turn on and off.

Please explain this a bit more.

Try:

#include <IRremote.h>

int RECV_PIN = 11; //IR receive pin goes here
int relay_1 = 2; //relay driver goes here

IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long last = millis();
boolean flag = false;

void setup()
{
  pinMode(relay_1, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) 
  {
    if (flag == false && results.value == 0xFF18E7)
    //if (flag == false && results.value == 0xFF9867)
    {
      digitalWrite(relay_1, HIGH);  
      last = millis();
      flag = true;
    }
    irrecv.resume(); // Receive the next value
  }

  if(flag == true && millis() - last >= 1000)
  {
    digitalWrite(relay_1, !digitalRead(relay_1));  
    last =last + 1000;
    //flag = false;
  }

  // other non blocking code goes here


} //END of loop()

My english is bad. but i try.

  1. I going too turn on a (script)
  2. Script going too turn on different relay and led, like this:

// just a example
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(Relay HIGH);
digitalWrite(Saftpump, HIGH);
digitalWrite(Vannpump, HIGH);
delay(5000);
digitalWrite(Vannpump, LOW);
digitalWrite(Saftpump, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(Relay, HIGH);
digitalWrite(BLUE_PIN, LOW);

  1. Then it automatic loop this script.

  2. Then I turn it off on the same button then i turn it on

If you understanad.

And the code you send me. Is just turn the relay on (5000) and then it turn off again. I like it too loop it :slight_smile:


It work :smiley: But I can`t turn it off

Not sure what you want.
In this version the IR code starts the flashing then IR button stops the flashing.

#include <IRremote.h>

int RECV_PIN = 11; //IR receive pin goes here
int relay_1 = 2; //relay driver goes here

IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long last = millis();
boolean flag = false;

void setup()
{
  pinMode(relay_1, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) 
  {
    //if (flag == false && results.value == 0xFF18E7)
    if (results.value == 0xFF18E7)
    {
      digitalWrite(relay_1, LOW);  
      last = millis();
      flag = !flag;
    }
    irrecv.resume(); // Receive the next value
  }

  if(flag == true && millis() - last >= 1000)
  {
    digitalWrite(relay_1, !digitalRead(relay_1));  
    last =last + 1000;
  }

  // other non blocking code goes here


} //END of loop()

Sorry if (flag == false && results.value == 0xFF18E7) This line was wrong, it should be if (results.value == 0xFF18E7)

Thx it works :D

Now the last question.

How do I add more button/script like you made? Going too have 2-3 more.

like this:

if (results.value == 0xFF18E7) { digitalWrite(relay_1, LOW); last = millis(); flag = !flag; } irrecv.resume(); // Receive the next value }

if(flag == true && millis() - last >= 1000) { digitalWrite(relay_1, !digitalRead(relay_1)); last =last + 1000;

How do I add more button/script like you made? Going too have 2-3 more.

That is for your homework ;)

Test time. What does "flag" do?

Yes :smiley:

I dont know. can you explain? :slight_smile:
This is loop the relay off

if(flag == true && millis() - last >= 1000)
{
digitalWrite(relay_1, !digitalRead(relay_1));
last =last + 1000;
//flag = false;
}

if (results.value == 0xFF18E7) { . . . flag = !flag; {

The above toggles flag when the IR button is pushed. That is: flag = !flag; If flag was true it becomes false. If flag was false it becomes true. When you press a remote key you want to start flashing (flag == true) then stop flashing on the second push (flag == false) . . .

When: - flag is true it will ENABLE timing in the next if() section - flag is false, it will DISABLE timing in the next if() section

if(flag == true && millis() - last >= 1000) //when flag == true you allow the if function to test milli() - last . . .

What does this do? digitalWrite(relay_1, !digitalRead(relay_1));

Aah:) So then it becomes difficult to make more.

hmm digitalWrite(relay_1, !digitalRead(relay_1));

Think. DigitalRead get the LOW/HIGH verdi and put it in digitalWrite