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