OK, so with this sketch when I press button 1 it turns the relay on and the when i press button 1 again it turns the relay off. But that is it, the relay won't turn on again no matter how I press the button. It's almost as if the sketch isn't looping completely or something?
#include <RCSwitch.h>
#define RELAY1 5
int val = 0;
int old_val = 0;
int state = 1;
int RELAY = 5;
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
pinMode(RELAY1, OUTPUT);
mySwitch.enableReceive(0);
}
void loop() {
if (mySwitch.available()) {
unsigned long value = mySwitch.getReceivedValue();
if (value == 5592323UL) {
val = digitalRead(RELAY1);
if ((val == HIGH) && (old_val == LOW)) {
state = 1 - state;
}
old_val = val;
if (state == 1) {
digitalWrite(RELAY1, HIGH);
} else {
digitalWrite(RELAY1, LOW);
}
mySwitch.resetAvailable();
}
}
}