using RF to trigger different sequences

I've got a 4 button 433mhz remote control - i need to be able to trigger 4 different "programs" on an arduino depending on which button is pressed.

Previously I've done this with a 4 channel receiver pulling input pins and then the arduino simply running the appropriate sequence based on which pin is triggered but this is all very clunky and inefficient. I've been playing around with RCSwitch and realised this is the route I should be going down and have got a receiver board strapped to an uno that's happily listening for the binary signal from the keyfob but now I've hit a brick wall.

I need it to listen and when button 1 is pressed (ie it receives the binary code "100001010101101101110001") it turns on a light (via relay) & runs a motor
if it receives a button 2 code then it tells a servo to move to position 1 for 2 seconds, then position 2 for 3 seconds
if it receives a button 3 code then it flashes an LED
etc
and these "programs" keep looping for as long as the button is pressed.

Can anyone point me towards a sketch or tutorial that's doing this sort of thing so I can pick through it and get an understanding of how i should be coding this? I'm not a natural coder; i can see something else, study and work out how to modify it but if someone says "just write an array table and compare the subroutines" then i'm useless.

Any help? https://www.instructables.com/Decoding-and-sending-433MHz-RF-codes-with-Arduino-/

tommoore:
"just write an array table and compare the subroutines"[/color]

I'm stealing that!

Sadly not - that's like most of the instructions i've found on my searches so far - just using the arduino either as a bridge between different networks or as a transmitter not actually doing any processing itself. There's also lots of instructables for using the 4 pins / relays of an off the shelf receiver to drive input pins on the arduino (basically what i've done in the past) but nothing about how to take the binary "buttons" signal and depending on their value run different programs or sequences in response.

two months on and i've still not got this licked - any pointers gratefully received

#include <RCSwitch.h>

#define RF_PIN       2   // pin number 2 on arduino


RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive((RF_PIN - 2));  // Receiver on interrupt 0 => that is pin #2
  pinMode(LED_BUILTIN, OUTPUT);
}
 

void loop() {
  if (mySwitch.available()) {
    
    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );
    if(mySwitch.getReceivedValue() == "5632259") //value button a
      void (); 
      {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);                       // wait for a second
}}
  (mySwitch.getReceivedValue() == "5632268")  //value button B 
      void ();
      {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(2000);                       // wait for 2 second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(2000);                       // wait for 2 second
}
          
    }

If i press button A (sending 5632259) It must run a certain sequence and then stop
if i press button B (sending 5632268) it must run a different sequence and then stop

(theoretically i'd like it to be able to cope with commands for button C & D too)
I just can't get it to work - what am i doing wrong and what is the silly mistake in the code that i'm missing?

tommoore:
I just can't get it to work

Have you gotten as far as the Arduino printing the received value?

This looks suspect:

if(mySwitch.getReceivedValue() == "5632259") //value button a

Double quotes denotes a char array but (==) is not used for char comparisons. See memcpy/strcmp

investigate using switch case instead of if then

getReceivedValue returns an unsigned long, not a string.

So try replacing your loop function with this:

void loop()
{
  if (mySwitch.available())
  {
    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );
    if (mySwitch.getReceivedValue() == 5632259UL) //value button a
    {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);                       // wait for a second
    }
    if (mySwitch.getReceivedValue() == 5632268UL) //value button B
    {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(2000);                       // wait for 2 second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(2000);                       // wait for 2 second
    }
  }
}

Not tested or compiled (don't have the library).

dougp:
Have you gotten as far as the Arduino printing the received value?

This looks suspect:

if(mySwitch.getReceivedValue() == "5632259") //value button a

Double quotes denotes a char array but (==) is not used for char comparisons. See memcpy/strcmp

yup, that's how i'm sniffing the button value.

wildbill:
getReceivedValue returns an unsigned long, not a string.

So try replacing your loop function with this:

void loop()

{
 if (mySwitch.available())
 {
   Serial.print("Received ");
   Serial.print( mySwitch.getReceivedValue() );
   Serial.print(" / ");
   Serial.print( mySwitch.getReceivedBitlength() );
   Serial.print("bit ");
   Serial.print("Protocol: ");
   Serial.println( mySwitch.getReceivedProtocol() );
   if (mySwitch.getReceivedValue() == 5632259UL) //value button a
   {
     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
     delay(1000);                       // wait for a second
     digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
     delay(1000);                       // wait for a second
   }
   if (mySwitch.getReceivedValue() == 5632268UL) //value button B
   {
     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
     delay(2000);                       // wait for 2 second
     digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
     delay(2000);                       // wait for 2 second
   }
 }
}



Not tested or compiled (don't have the library).

with a little bit of tweaking that is correctly reading the RF value and following the appropriate command.

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver input on interrupt 0 (D2)
  pinMode(7, OUTPUT); // D7 as output- Optional
}
void loop()
{
  if (mySwitch.available())
  {
    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );
    if (mySwitch.getReceivedValue() == 6124962UL) //value button a
    {
      digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);                       // wait for a second
    }
  }
  if (mySwitch.getReceivedValue() == 1622914UL) //value button B
  {
    digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(2000);                       // wait for 2 second
    digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
    delay(2000);                       // wait for 2 second
  }
}

However it now just loops through that flash sequence eternally. I've spent the last 2 days trying to break and stop and if-else and can't find anyway to STOP the sequence once started. I want to press button a and it do a series of flashes then then go back to standby until it gets another button press / start command. I'm sure there's something stupidly simple i'm missing but google is failing me too; how do i make these sequences run just once? I know i could put it all in the void setup area but then i'd have to reset the board after every cycle which seems far too messy.

I see this in the library example:

mySwitch.resetAvailable();

Maybe try that after you've done the flashing once.

THANKYOU!

yes that once single command has solved the whole problem, thankyou thankyou!!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.