What I have:
- 1 x 8 channel relay
- 1 x IR receiver
- 1x Arduino Uno
- 1x momentary switch
- 1x breadboard
- 1x 10K resister
I think the wiring is all ok as for the most part things are working as they should..ish.
Just a few bugs I need to fix.
The goal was to toggle just 2 relays (at the moment) on/off with the hardwired switch and individually toggle the a relay using an IR remote control.
Like I said this sort of works....
If I press the momentary button it does indeed toggle both relays...yeah! then as long as the 2 relays are toggled to the on position by the momentary switch I can toggle each relay using buttons on a remote.
However if I turn the relays off with the momentary button I can't turn them on with the remote. Also if I turn off one of the relays with the remote and then press momentary switch it alternates the toggle between the 2 relays rather than just turning the other one off.
I have been using various example sketches to achieve all this and just trying to stitch them together as best I can but something is telling me that the approach is all wrong and that I should maybe be looking into Boolean's perhaps?
Hope you can shed some light on this for me.
#include <IRremote.h>
#define irPin 8
IRrecv irrecv(irPin);
decode_results results;
const int buttonPin = 2;
const int relay1 = 13;
const int relay2 = 12;
int relay1State;
int relay2State;
int buttonState;
int lastButtonState;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(buttonPin, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay1, relay1State);
digitalWrite(relay2, relay2State);
}
void loop() {
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFF30CF:
Serial.println("Button 1 Pressed");
relay1State = ~relay1State;
digitalWrite(relay1, relay1State);
delay(250);
break;
case 0xFF18E7:
Serial.println("button 2 pressed");
relay2State = ~relay2State;
digitalWrite(relay2, relay2State);
delay(250);
break;
}
irrecv.resume();
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
relay1State = !relay1State;
relay2State = !relay2State;
}
}
}
digitalWrite(relay1, relay1State);
digitalWrite(relay2, relay2State);
lastButtonState = reading;
}
~
to!
. Now just clarify us one thing. Suppose you start arduino and press the the buttonPin once. You make both relays ON. Now with remote you make relay 1 OFF. So current state is relay1 OFF and relay2 ON. Now you press the buttonPin again. What do you want the new relay states to be? (not what is happening right now, what you intend to be)