Arduino program re-executing multiple times when holding button

  arduino, arduino-c++, arduino-uno, c++

I’m trying to control an RC Car using rcswitch library with this code. When you press the forward button on time, it should start and when you press it again it should stop.

Problem: When I hold the start button on my remote, the car keeps starting and stopping..the code seems to be re-executing multiple times a second. I want this to be executed only once when it is pressed.

Code:

#include <RCSwitch.h>

#include <U8x8lib.h>


/*Useful FONTS:
 * u8x8_font_artosserif8_r
 * u8x8_font_profont29_2x3_r
 * u8x8_font_profont29_2x3_n
 */


#define S1 10
#define SUP 3
#define SDN 4
#define LED1 11
#define LED2 12
#define pot A0
#define Rfor 11
#define Rrev 9
#define Lfor 8
#define Lrev 7
#define Rspeed 5
#define Lspeed 6
#define RF 2
#define UStrig A3
#define USecho A2

int potValue = 0; 
unsigned long remoteValue = 0;


unsigned long USduration;
int USdistance;

bool running_flag = 0;



U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

RCSwitch mySwitch = RCSwitch();



void FOR(void) {
  digitalWrite(Lrev, LOW);
  digitalWrite(Rrev, LOW);
  digitalWrite(Lfor, HIGH);
  digitalWrite(Rfor, HIGH);
}

void REV(void) {
  digitalWrite(Lfor, LOW);
  digitalWrite(Rfor, LOW);
  digitalWrite(Lrev, HIGH);
  digitalWrite(Rrev, HIGH);  
}

void LEFT(void) {
  digitalWrite(Lfor, LOW);
  digitalWrite(Lrev, LOW);
  digitalWrite(Rrev, LOW);
  digitalWrite(Rfor, HIGH);
}

void RIGHT(void) {
  digitalWrite(Rfor, LOW);
  digitalWrite(Rrev, LOW);
  digitalWrite(Lrev, LOW);
  digitalWrite(Lfor, HIGH);
}

int US(void) {
  long duration;
  int distance;
  digitalWrite(UStrig, LOW);
delayMicroseconds(2);
digitalWrite(UStrig, HIGH);
delayMicroseconds(10);
digitalWrite(UStrig, LOW);
duration = pulseIn(USecho, HIGH);
distance = (duration*0.034)/(2);
return distance;
}

void STOPM(void) {
  digitalWrite(Lrev, LOW);
  digitalWrite(Rrev, LOW);
  digitalWrite(Lfor, LOW);
  digitalWrite(Rfor, LOW);  
  }

void setup() {
  Serial.begin(9600);

 u8x8_SetI2CAddress(u8x8.getU8x8(), 0x3C*2);
 u8x8.begin();
 u8x8.setPowerSave(0);


 mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2

 pinMode(LED1, OUTPUT);
 pinMode(S1, INPUT_PULLUP);
 pinMode(SUP, INPUT_PULLUP);
 pinMode(SDN, INPUT_PULLUP);
 pinMode(pot, INPUT_PULLUP);
 pinMode(Rfor, OUTPUT);
 pinMode(Rrev, OUTPUT);
 pinMode(Lfor, OUTPUT);
 pinMode(Lrev, OUTPUT);
 pinMode(Rspeed, OUTPUT);
 pinMode(Lspeed, OUTPUT);
 pinMode(UStrig, OUTPUT);
 pinMode(USecho, INPUT);



 
}

void loop() {

potValue = map(analogRead(pot), 0, 1023, 0, 255);

analogWrite(Rspeed, potValue);
analogWrite(Lspeed, (potValue*0.96));



if (mySwitch.available()) {
    remoteValue = mySwitch.getReceivedValue();
    mySwitch.resetAvailable();
  }

if (remoteValue == 5592323 && running_flag == 0) {
  FOR();
  running_flag = 1;
  remoteValue = 0;
}
else if (remoteValue == 5592332 && running_flag == 0) {
  REV();
    running_flag = 1;
  remoteValue = 0;

}
else if (remoteValue == 5592368 && running_flag == 0) {
  LEFT();
    running_flag = 1;
  remoteValue = 0;

}
else if (remoteValue == 5592512 && running_flag == 0) {
  RIGHT();
  running_flag = 1;
  remoteValue = 0;

}

if (remoteValue == 5592323 && running_flag == 1) {
  STOPM();
  running_flag = 0;
  remoteValue = 0;
}
 else if (remoteValue == 5592332 && running_flag == 1) {
  STOPM();
  running_flag = 0;
  remoteValue = 0;
 } 
  else if (remoteValue == 5592368 && running_flag == 1) {
  STOPM();
  running_flag = 0;
  remoteValue = 0;
  }
  else if (remoteValue == 5592512 && running_flag == 1) {
  STOPM();
  running_flag = 0;
  remoteValue = 0;
}
 


/*
if (US() > 9) {
 FOR();  
}
else if (US() < 9) {
RIGHT();  
}

*/
  }

Source: Windows Questions C++

LEAVE A COMMENT