Pages: [1] 2   Go Down
Author Topic: (Solved) ir remote relay control  (Read 1774 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
  }
}
« Last Edit: October 15, 2012, 05:28:00 pm by wikitjuggla » Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the problem?

Isn't it working if you do...
Code:
else if (results.value == WHATEVER) {
  if (millis() - last > 250) {
    on2 = !on2;
    digitalWrite(relay2, on2 ? HIGH : LOW);
  }
  last = millis();
}

?

Also a little tip, change:
Code:
int relay1 = 4;
...
int on = 0;
...
on = !on;
digitalWrite(relay1, on ? HIGH : LOW);

to
Code:
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 smiley-wink
« Last Edit: October 14, 2012, 11:08:17 pm by guix » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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();
 
}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  if (irrecv.decode(&results))
  {
and
Code:
    irrecv.resume(); // Receive the next value
  }
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


  }
}






Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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:
    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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
« Last Edit: October 15, 2012, 04:32:36 pm by guix » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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








 
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hehe, glad to help smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
you sir, are a genius. that on2 on3 thing worked...thank you much
Wasn't that what I said in Reply #5?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: