1

I use Rc switch library to transmit and receive data. It works fine.

The part that sends the data is as follows :

#include <Arduino.h>
#include <RCSwitch.h>

RCSwitch transmitter = RCSwitch();

void setup()
{
    Serial.begin(9600);
    transmitter.enableTransmit(10);
}

void loop()
{
    /* Send data in binary */

    // Go right
    transmitter.send("010101010101010100000011");
    delay(1000);

    // Go left
    transmitter.send("010101010101010100001100");
    delay(1000);
}

And the receiving part is like this:

#include <RCSwitch.h>

RCSwitch receiver = RCSwitch();
int rightLED = 4;
int leftLED = 7;
unsigned long receivedValue;
unsigned long goRight = 5592323;
unsigned long goLeft = 5592332;

void setup()
{
    Serial.begin(9600);
    receiver.enableReceive(0); // Receiver on interrupt 0
    pinMode(rightLED, OUTPUT);
    pinMode(leftLED, OUTPUT);
}

void moveRight()
{
    Serial.println("I'm going right");
}

void moveLeft()
{
    Serial.println("I'm going left");
}

void loop()
{
    if (receiver.available())
    {
        receivedValue = receiver.getReceivedValue();
        if (receivedValue == goRight)
        {
            moveRight();
        }
        if (receivedValue == goLeft)
        {
            moveLeft();
        }
        receiver.resetAvailable();
    }
}

The problem is, when I open a serial monitor and look the output of the received values moveRight and moveLeft functions are called multiple times.

I'm going right
I'm going right
I'm going right
I'm going right
I'm going left
I'm going left
I'm going left
I'm going left
I'm going right
I'm going right
I'm going right
I'm going right

What kind of change should I do in the receiver part so that moveRight and moveLeft functions are called only once.

I'm going right
I'm going left
I'm going right

2 Answers 2

2

You can save the previous received value, and only print if it is different from what you just received.

Something like this:

// near the top
const int rightLED = 4;
const int leftLED = 7;
const unsigned long goRight = 5592323;
const unsigned long goLeft = 5592332;

// to hold the state
unsigned long previousValue = 0;

// ...

// loop code
void loop()
{
  if (receiver.available())
  {
    unsigned long receivedValue = receiver.getReceivedValue();
    if (receivedValue != previousValue) {
      // something changed
      if (receivedValue == goRight)
      {
        moveRight();
      }
      if (receivedValue == goLeft)
      {
        moveLeft();
      }
      // remember the current value
      previousValue = receivedValue;
    }
    receiver.resetAvailable();
  }
}
1
  • Depending on you application, you might also want to store the time of the last received value, and compare that. Otherwise, when pressing the same button twice, it will ignore the last press.
    – Gerben
    Nov 14, 2020 at 13:36
2

Old post, I know but I have to add to it. The statement "transmitter.enableTransmit(10);" causes the RCSwitch library to send the message 10 times. You are receiving 4 messages; you should be receiving 10. I have found that when using "receiver.available()" I also get about half of what is expected. Just a hunch, but I think the "receiver.available()" is getting every other message. The RCSwitch library may have a dwell period after a message is received that holds the receiver in suspense causing every other message to be lost. "Serial.begin(9600)" also is a bit slow, but I think the buffer can handel it.

The attached image shows my transmitter power consumption. You can see that 10 messages are sent in about 200 mS. Your delay(1000) leaves plenty of time for completion of all messages received.

transmitterPowerDemand

Your Answer

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