(Solved) ir remote relay control

i am trying to control 4 relays using a ir remote control. i successfully can control 1 of the relays by pressing any button i program the arduino pin to respond to. press the button once, turns pin on, press the same button again, turn the pin off. i need help writing a sketch to control 4 arduino pins, each with their own button on the ir remote. help would be great. thank you

#include <IRremote.h>

int RECV_PIN = 11;
int relay1 = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;

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

int on = 0;
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();
    }    
    irrecv.resume(); // Receive the next value
  }
}

What is the problem?

Isn't it working if you do...

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

?

Also a little tip, change:

int relay1 = 4;
...
int on = 0;
...
on = !on;
digitalWrite(relay1, on ? HIGH : LOW);

to

byte relaysPins[] = { 4, 5, 6, 7 }; //assuming relays are connected to those pins...
...
bool relaysStates[] = { LOW, LOW, LOW, LOW };
...
relaysStates[0] = !relaysStates[0];
digitalWrite(relaysPins[0], relaysStates[0]);

etc... Well, only a suggestion :wink:

i'm not really sure how to use that type of sketch set up, this ir stuff has me baffled too. i tried the else if once before and it didn't work but now i got it working except after i turn on any of the 4 relays i can turn on and off the first relay with a single button press but after it on, turning on the other 3 takes two button presses just to turn on. i'm pretty sure its miss placement of the irrecv.resume(); command but i'm not really sure where i need to put it. i tried putting it at the end of every (else if) statement but then the whole thing doesn't work. where i have it in this code is the only way i found so far that gives me close to perfect output results

#include <IRremote.h>

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

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
}

int on = 0;
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();
    }    
    irrecv.resume(); // Receive the next value
    
  }
  
  else if (results.value == 0xF1B1) {
  if (millis() - last > 250) {
    on = !on;
    digitalWrite(relay2, on ? HIGH : LOW);
  }
  last = millis();
 
   
}


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

  
  
}


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

If you put each { on a new line, and use Tools + Auto Format, you'll see that you have inserted the new if statements and block in the wrong place. They should be between the lines:

  if (irrecv.decode(&results))
  {

and

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

is this what you meant? it didn't affect my arduino

#include <IRremote.h>

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

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
}

int on = 0;
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();
    }    
    irrecv.resume(); // Receive the next value

  }

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


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




  }


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


  }
}

is this what you meant?

It's what I meant by putting each { on a new line and formatting. You did not move the added code into the right place, though.

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

and the similar blocks after it need to go BEFORE

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

You may well need to have different time and state variables for each button, too. It seems that you maybe want to prevent multiple presses of the same button within a specified period, but you should, I think, be able to press different buttons in that same period.

i can press one of the buttons over and over and the relay will turn on and off like it should with every press of the button but when i press the next button (even if i wait several seconds before i press it) it still takes two presses. now if i keep pressing the same button i just pushed it will work just fine. it takes two presses the first time i switch to a different button that corresponds to a different relay

also if i turn the relay on then back off then move on to a different relay it works just fine without having to push the button twice

Maybe because you use the same "on" variable for every relays..? So do like in my first post, an array to hold state of each relay, or create other variables "on2", "on3" if you find it easier :slight_smile:

you sir, are a genius. that on2 on3 thing worked...thank you much

#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
  }
}

Hehe, glad to help :slight_smile:

you sir, are a genius. that on2 on3 thing worked...thank you much

Wasn't that what I said in Reply #5?

oh i'm sorry i'm pretty new at this stuff so i didn't quite understand what you meant i guess, he drew it out in crayon for me

Hi

Im new to arduino, Im also working on same theme but with a diff code, the code works but the result is nor stable
can any one help

Im trying to switch on and off an led with a same button, Im using a sony remote

#include <IRremote.h>


int LED1 = 13;
int on = 0;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);
    irrecv.resume(); // Receive the next value
  }
  
  switch(results.value)
  {
    case 16:
    {
    on = !on;
    digitalWrite (LED1, on ? HIGH : LOW);
    }
    break;
  }
}

but the result is nor stable

You'll need to explain what this means. We can't see what you are observing as unstable.

I have assign button # 1 (Sony remote) to turn on/off the LED on pin 13
it works fine but some times the remote button is pressed for longer period and sometimes for short period
the IR receiver continuously receive signal from the remote, now when i press the remote button for a longer period it receives 3 to 5 signals and 2 to 3 signals when button is pressed for short period
like it receives
"
16 ----> ON
16 ----> OFF
16 ----> ON
16 ----> OFF
"
result is OFF

when the signal is received 3 times like
"
16 ----> ON
16 ----> OFF
16 ----> ON
"
result is ON

I want the receiver to receive signal only once if I press the remote button for a longer or shorter period

Hello and welcome Wiki,

First, just so you know, this:

digitalWrite (LED1, on ? HIGH : LOW);

Could be changed to this:

digitalWrite (LED1, on);

It's the same thing (but then I suggest renaming "int on" to "bool LED1_state" or something like that :slight_smile:

Then, to solve your problem, study this code: https://github.com/shirriff/Arduino-IRremote/blob/master/examples/IRrelay/IRrelay.ino

And next time, start your own topic :slight_smile: