AVOID DUPLICATED INPUTS FROM RF 433Mhz REMOTE CONTROL TRANSMITTER

Hi,

First of all I apologize for some mistakes or confused sentences, but I’m Portuguese and I speak English better than I write.

I’m a newbie to Arduino and programming and I need some help to solve this issue.

I’m building an project to make the lights of an 1:18 car miniature to light up with an 4 channel RF 433Mhz remote control. Everytime you press one button it lights the Head lights, or the left turn, right turn, etc… and two of the buttons have more than one function.

Everything is working allright except the fact that sometimes the remote send’s the frequency more than onetime, and this makes the Arduino execute two functions in a row.

Example:

Button B: First click - Lights Rear and Low Beam lights / Second click - Lights High Beam Lights / Third click - Turn off the lights

What is happening is that sometimes the remote send the frequency two times and what happens is that it lights Low Beam, Rear and High Beam at the sametime. Or it execute the first click ok, and on the second it execute second and third click because of receiving two times the frequency.

Can some one help me to make the Arduino just keep the first frequency and ignore the duplicated?

Here is my code:

/*

BOTAO A - 25553 / 24bit Protocol: 5
BOTAO B - 25554 / 24bit Protocol: 5
BOTAO C - 25556 / 24bit Protocol: 5
BOTAO D - 25560 / 24bit Protocol: 5

*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

int inputvariableRF = 0;

const int ledPin1 =  12; // Connect Led - Luzes Traseiras
const int ledPin2 =  11; // Connect Led - Médios
const int ledPin3 =  10; // Connect Led - Máximos
const int ledPin4 =  9; // Connect Led - Pisca Esquerda Ft
const int ledPin5 =  8; // Connect Led - Pisca Esquerda Tz
const int ledPin6 =  6; // Connect Led - Luz Interior
const int ledPin7 =  5; // Connect Led - Pisca Direita Ft
const int ledPin8 =  4; // Connect Led - Pisca Direita Tz

int led1delaymax =  2000; // Delay MAX Led 1
int led2delaymax =  2000; // Delay MAX Led 2
int led3delaymax =  2000; // Delay MAX Led 3
int led4delaymax =  2000; // Delay MAX Led 4
int led5delaymax =  2000; // Delay MAX Led 5
int led6delaymax =  2000; // Delay MAX Led 6
int led7delaymax =  2000; // Delay MAX Led 7
int led8delaymax =  2000; // Delay MAX Led 6

int x = 0;
int y = 0;
int z = 0;

int brightness = 0;

void setup() 
{
 Serial.begin(9600);
 mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2
 
 pinMode(ledPin1, OUTPUT); 
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT); 
 pinMode(ledPin4, OUTPUT);
 pinMode(ledPin5, OUTPUT); 
 pinMode(ledPin6, OUTPUT);
 pinMode(ledPin7, OUTPUT); 
 pinMode(ledPin8, OUTPUT);  
 
}

void loop() 
{
 if (mySwitch.available()) 
 {
   //Debug Serial Print    
   Serial.print("Received ");
   Serial.print(mySwitch.getReceivedValue());  
   
   inputvariableRF = mySwitch.getReceivedValue();
   
   mySwitch.resetAvailable();
   
   led(inputvariableRF);
 }
}

void led(int data)
{
 if (data == 25553)
{
  if (y == 0)
  {   
   analogWrite(6, brightness = 255);
   y = 1; 
  }
 
  else if (y == 1)
  {     
   for (brightness = 255; brightness >= 0; brightness -= 5) 
   {
    analogWrite(6, brightness);
    delay(50); // Wait for 50 millisecond(s)
    y = 2;
    }  
  }
  
  else if (y == 2)
  {          
    for(int x = 0; x <= 9; x = x + 1)
    { 
    digitalWrite(ledPin4, HIGH);
       digitalWrite(ledPin5, HIGH);
    digitalWrite(ledPin7, HIGH);
       digitalWrite(ledPin8, HIGH);       
    delay(300);
    digitalWrite(ledPin4, LOW);
       digitalWrite(ledPin5, LOW);
    digitalWrite(ledPin7, LOW);
       digitalWrite(ledPin8, LOW);       
    delay(500);
    y = 0;
    } 
  }
 } 
 
 else if (data == 25554)
 {  
  if (z == 0)
   { 
    //Led 1 Power ON
     digitalWrite(ledPin1, HIGH);
     digitalWrite(ledPin2, HIGH);      
     z = 1;
   }

   else if (z == 1)
   {
     //Led 2 Power ON
     digitalWrite(ledPin3, HIGH);
     z = 2;
    }      
   
   else if (z == 2)
   {
     //Led 1 & 2 Power OFF
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, LOW);      
     z = 0;   
   }
 }
 else if (data == 25556)
 {
  for(int x = 0; x <= 9; x = x + 1)
  { 
   //Led 4 Power ON
   digitalWrite(ledPin4, HIGH);
   digitalWrite(ledPin5, HIGH);   
   delay(500);
   digitalWrite(ledPin4, LOW);
   digitalWrite(ledPin5, LOW);
   delay(500); 
  }      
 }  

 else if (data == 25560)
 {
  for(int x = 0; x <= 9; x = x + 1)
  { 
   //Led 5 Power ON
   digitalWrite(ledPin7, HIGH);
   digitalWrite(ledPin8, HIGH);    
   delay(500);
   digitalWrite(ledPin7, LOW);
   digitalWrite(ledPin8, LOW);    
   delay(500); 
  }      
 }  
 
}

All the help or suggestions to improve the code will be welcome.

Thank you

DS.ino (3.48 KB)

You could try putting the call of mySwitch.resetAvailable() after mySwitch.resetAvailable() instead of before it as you have.

You could fix the code so that if 2 consecutive calls of mySwitch.getReceivedValue() within X milliseconds return the same value, the second call is ignored. However, you have some long delays which may interfere with that.

Use code tags when you post code here.

Thanks for the answer, but I'm having some trouble to understand this:

6v6gt: You could try putting the call of mySwitch.resetAvailable() after mySwitch.resetAvailable() instead of before it as you have.

Can you please be more explicit?

About the delays, I thought they where not a problem, because the purpose is only to pass to another function, when the first one is over.

For example, click button A, the light lights up, wait a little, click again it goes out, click again the Turning Signals flashes 10 times, and go off.

Only then you press another button. That's the idea and why I have the delays.