ir reciever TO be Debounced

i have to make my robot work with remote . When i press 1 on my remote i want to trigger ultrasonic sensors so that it can do obstacle avoidance .

The problem is than the ultrasonic sensor will work only when i keep on pressing the button 1 or when i leave it then the sensor gets off.

So i want to press the button only once and the sensors must work until i press next button.
:slight_smile:

You will need to provide your code to sort this out.

It is probable that your loop says receive IR, output a sonar pulse and back to start. In that case it will only output with IR pressed.

If IR received and sonarflag false. Detect first IR
Waste time in loop waiting for IR to stop
Set sonarflag

If sonarflag true output sonar. Keep pulsing until next IR

If IR received and sonarflag true. If second IR and sonar pulsing
Waste time waiting for IR to stop
turn off sonarflag

Weedpharma

This is the code .

//____________________________________ IR reciever 

#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;


#define rightMotor1 4 
#define rightMotor2 5
#define leftMotor1 12
#define leftMotor2 13

#define motorPower1 40
#define motorPower2 41




#include <NewPing.h>

#define TRIGGER_PIN1  9  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN1     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE1 200// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define TRIGGER_PIN2  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE2 200// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);
// _______________________________________________________________________________________



void setup(){
  
  irrecv.enableIRIn(); // beginig the reciever ----------
  

  
  // ----------------- motor initialisation ----------------------
  
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
  pinMode(leftMotor1, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(motorPower1, OUTPUT);
  pinMode(motorPower2, OUTPUT);
  

   
  

   
}


void loop(){
  


  
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); 

            
               if (results.value == 1 || results.value == 801 ){  // 1 pressed

                
                // run the line following code
                 LineFollowing();
              }
              else if (results.value == 2 || results.value == 802 ){ // 2 pressed

                
                // run the Obstacle avoider
                  ObstacleAvoider();
              }
             
}
}



void LineFollowing(){
// UNDER PROGRESS 
}
   

   void ObstacleAvoider(){
     unsigned int uS1 = sonar1.ping();
 unsigned int uS2 = sonar2.ping();
     
     if (uS1 / US_ROUNDTRIP_CM >= 50 && uS2 / US_ROUNDTRIP_CM >= 50 ){
    stop();
  }
  else if (uS1 / US_ROUNDTRIP_CM <= 50 && uS2 / US_ROUNDTRIP_CM >= 50 ){
    rightTurn();
  }
 
  else if (uS1 / US_ROUNDTRIP_CM >= 50 && uS2 / US_ROUNDTRIP_CM <= 50 ){
    leftTurn();
  }
  else if (uS1 / US_ROUNDTRIP_CM <= 50 && uS2 / US_ROUNDTRIP_CM <= 50 ){
    forward();
  }
   }
     
 
   
   // _________________________________________________________________________
   void stop(){
    
    digitalWrite(motorPower1, LOW);
  digitalWrite(motorPower2, LOW);
}

void forward(){
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
//  analogWrite(rightMotorPWM, 100);
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(leftMotor1, HIGH);
  digitalWrite(leftMotor2, LOW);
  //analogWrite(leftMotorPWM, 100);
}

void back(){
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(rightMotor1, LOW);
  digitalWrite(rightMotor2, HIGH);
 // analogWrite(rightMotorPWM, 100);
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(leftMotor1, LOW);
  digitalWrite(leftMotor2, HIGH);
 // analogWrite(leftMotorPWM, 100);
}

void rightTurn(){
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(rightMotor1, LOW);
  digitalWrite(rightMotor2, HIGH);
 // analogWrite(rightMotorPWM, 100);
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(leftMotor1, HIGH);
  digitalWrite(leftMotor2, LOW);
//  analogWrite(leftMotorPWM, 100);
}

void leftTurn(){
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
 // analogWrite(rightMotorPWM, 100);
  digitalWrite(motorPower1, HIGH);
  digitalWrite(motorPower2, HIGH);
  digitalWrite(leftMotor1, LOW);
  digitalWrite(leftMotor2, HIGH);
 // analogWrite(leftMotorPWM, 100);
}



//_____________________________________________________________________________________

Please Try Sorting my problem :sweat_smile: :sweat_smile: :relaxed:

neerajarduino:
Please Try Sorting my problem :sweat_smile: :sweat_smile: :relaxed:

wp gave you pseudo code to help you to sort the problem.