Offline
Newbie
Karma: 0
Posts: 18
|
 |
« on: October 14, 2012, 10:13:00 pm » |
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 } }
|
|
|
|
« Last Edit: October 15, 2012, 05:28:00 pm by wikitjuggla »
|
Logged
|
|
|
|
|
France
Offline
God Member
Karma: 19
Posts: 613
Scientia potentia est.
|
 |
« Reply #1 on: October 14, 2012, 10:58:59 pm » |
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 
|
|
|
|
« Last Edit: October 14, 2012, 11:08:17 pm by guix »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #2 on: October 15, 2012, 01:42:24 pm » |
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(); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #3 on: October 15, 2012, 01:52:54 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #4 on: October 15, 2012, 02:28:00 pm » |
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();
} }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: October 15, 2012, 02:35:37 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #6 on: October 15, 2012, 03:17:57 pm » |
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
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #7 on: October 15, 2012, 03:58:57 pm » |
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
God Member
Karma: 19
Posts: 613
Scientia potentia est.
|
 |
« Reply #8 on: October 15, 2012, 04:29:20 pm » |
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 
|
|
|
|
« Last Edit: October 15, 2012, 04:32:36 pm by guix »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #9 on: October 15, 2012, 05:26:34 pm » |
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 } }
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
God Member
Karma: 19
Posts: 613
Scientia potentia est.
|
 |
« Reply #10 on: October 15, 2012, 05:42:45 pm » |
Hehe, glad to help 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: October 16, 2012, 05:10:26 am » |
you sir, are a genius. that on2 on3 thing worked...thank you much Wasn't that what I said in Reply #5?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #12 on: October 16, 2012, 10:33:55 am » |
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
Newbie
Karma: 0
Posts: 2
|
 |
« Reply #13 on: November 17, 2012, 02:50:30 pm » |
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; } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: November 17, 2012, 03:57:56 pm » |
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
|
|
|
|
|
|