Go Down

Topic: (Solved) ir remote relay control (Read 1 time) previous topic - next topic

wikitjuggla

Oct 15, 2012, 05:13 am Last Edit: Oct 16, 2012, 12:28 am by wikitjuggla Reason: 1
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

Code: [Select]
#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
 }
}

guix

#1
Oct 15, 2012, 05:58 am Last Edit: Oct 15, 2012, 06:08 am by guix Reason: 1
What is the problem?

Isn't it working if you do...
Code: [Select]

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


?

Also a little tip, change:
Code: [Select]

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


to
Code: [Select]

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 ;)

wikitjuggla

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
Code: [Select]
#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();
 
}
}

PaulS

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:
Code: [Select]
  if (irrecv.decode(&results))
  {

and
Code: [Select]
    irrecv.resume(); // Receive the next value
  }


wikitjuggla

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

Code: [Select]
#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();


  }
}







PaulS

Quote
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.

Code: [Select]
  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
Code: [Select]
    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.

wikitjuggla

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

wikitjuggla

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

guix

#8
Oct 15, 2012, 11:29 pm Last Edit: Oct 15, 2012, 11:32 pm by guix Reason: 1
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 :)

wikitjuggla

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

Code: [Select]
#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
  }
}








 

guix


PaulS

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

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

wikitjuggla

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

Wiki

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


Code: [Select]

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

PaulS

Quote
but the result is nor stable

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

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview