I'm making a project where a 2 channel RF keyfob (433Mhz) controls an led using arduino nano. As for the hardware everything works and I have tested it with the example decoder code and it works fine. Library I'm using is the RCSwitch.h for receiving.
I have modified the library example code a bit to suit my project. What I'm basically trying to achieve is make an led turn on when I press button A on the keyfob and after a while if I press button A again then the led must turn off. However with my current code, what happens is pressing button A makes the led turn on and pressing button A again does nothing, but if I press button B then the led turns off. So where did I go wrong?
Here is my code
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
int LED = LED_BUILTIN;
boolean statusled = LOW;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
}
void loop() {
if (mySwitch.available())
{
Serial.print(mySwitch.getReceivedValue());
unsigned long int num = mySwitch.getReceivedValue();
Serial.print(num);
if (num == 16130609){
mySwitch.disableReceive();
if (statusled == LOW)
statusled = HIGH;
}
else
{
statusled = LOW;
}
digitalWrite(LED, statusled);
unsigned long time_now = millis();
int ck = 500;
while(millis() <time_now + ck)
{;}
mySwitch.resetAvailable();
mySwitch.enableReceive(0);
}
}
16130609 is the value I get from decoding button A
16130610 is the value from button B