1

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

1 Answer 1

1

Look again at your logic here:

if (num == 16130609){
    mySwitch.disableReceive();
    
    if (statusled == LOW) statusled = HIGH;
}
else
{
    statusled = LOW;
}

When you press A you will get num with the value 16130609. In the if you turn the LED on, if it is off currently. But you never turn it off there. You will always get in this if statement when pressing A. You don't turn the LED off there, so it doesn't turn off when you press A. When you press B you get another value for num. Thus the else clause will get executed, turning the LED off.

So the code is doing exactly, what you told it to do.

Solution: Since you want to toggle the LED on press of A, you should just toggle the value of statusled in there, instead of explicitly setting it. That would look like this:

if (num == 16130609){
    mySwitch.disableReceive();
    
    statusled = !statusled;
}

I removed the else clause, since you don't want button B to do anything.

4
  • Ohhh..now I see what went wrong with my if,else command. Thanks, your code worked. I didn't know (!) command existed to toggle. I thought it was just a comparator.
    – Kokachi
    Commented Jan 15, 2022 at 21:00
  • 2
    @Kokachi the ! is not a comparator ... it is a logical NOT
    – jsotola
    Commented Jan 15, 2022 at 23:48
  • 1
    As jsotola wrote its the logical NOT operator. It will negate the value of the operant, so making true to false and vice versa.
    – chrisl
    Commented Jan 16, 2022 at 0:36
  • I understand it now. Previously when I went through the documentation I saw ! in comparison operator. I think it was !=
    – Kokachi
    Commented Jan 16, 2022 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.