Trouble with sensor and motors

Hello everyone so i have some trouble by creating the program for a vacuum cleaner because i actually dont know how to stop getting forward the wheels with my sensors

here the program i have so far :

#include <SharpIR.h>

#define ir A0
#define model 1080
int pwm_a = 3;
int pwm_b = 9;
int dir_a = 2;
int dir_b = 8;

SharpIR SharpIR(ir, model);

void setup() {
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
Serial.begin(9600);
}

void loop() {
delay(2000);

unsigned long pepe1=millis(); // takes the time before the loop on the library begins

int dis=SharpIR.getDistance(0); // this returns the distance to the object you’re measuring

Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);

unsigned long pepe2=millis()-pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);

}

Btw im using arduino UNO, velleman Motor Shield and 3 SharpIR sensors GP2Y0A21YK0F and 2 DC motor

I recommend you take a look at some robots codes using the Sharp sensor and a similar motor shield.

something like ... don't know what units distance is measured in

  int dis=SharpIR.getDistance(0);
  if (StopDis > dis)  {
    analogWrite(pwm_a, 0);
    analogWrite(pwm_b, 0);
  }

gcjr:
something like ... don't know what units distance is measured in

  int dis=SharpIR.getDistance(0);

if (StopDis > dis)  {
    analogWrite(pwm_a, 0);
    analogWrite(pwm_b, 0);
  }

hello sorry for late reply i try it but it didnt work but thank you for help
its mesuring in cm

i fund this one just to make the motor works and now i want to include the sensor to avoid obstacles
so i added this one :

 #include <SharpIR.h>
#define ir0 A0
#define ir1 A1
#define ir2 A2
#define model 1080

but didnt work…

here the complete code:

 #include <SharpIR.h>
#define ir0 A0
#define ir1 A1
#define ir2 A2
#define model 1080


int pwm_a = 3;         //PWM control for motor outputs 1 and 2 
int pwm_b = 9;        //PWM control for motor outputs 3 and 4 
int dir_a = 2;       //direction control for motor outputs 1 and 2 
int dir_b = 8;      //direction control for motor outputs 3 and 4 


void setup() {


Serial.begin(9600);
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  analogWrite(pwm_a, 200);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, 200);
  
}

void loop(){
  if (SharpIR.getDistance < 0){
    digitalWrite(dir_a, HIGH);
    digitalWrite(dir_b, LOW); 
  }
  digitalWrite(dir_a, HIGH); 
  digitalWrite(dir_b,HIGH);  
  
  
  analogWrite(pwm_a, 255);  
  analogWrite(pwm_b, 255);
}

ViniciusPolo:
I recommend you take a look at some robots codes using the Sharp sensor and a similar motor shield.

i look many similar codes but didnt find anything that coud actually work…

Rather than just saying “it didn’t work”, tell us what it did do, and how that fell short of your expectations.

TheMemberFormerlyKnownAsAWOL:
Rather than just saying "it didn't work", tell us what it did do, and how that fell short of your expectations.

sorry the function that i will who didnt work is that the motor have to turns in opposite direction when the sensor see an obstacle so the robot can turn around

So, what does it do?

TheMemberFormerlyKnownAsAWOL:
So, what does it do?

it just going foward and ignoring the sensor (really sorry about my comprehension)

And what is it printing?

TheMemberFormerlyKnownAsAWOL:
And what is it printing?

"Mean distance: 1 "
Time taken (ms): 0

the values dont change

Are you still using this code?

if (SharpIR.getDistance < 0){

If not, please post the code you are using.
If so, please fix the call to the getDistance function.

i finaly found a fixe thank you all for cooperating

int pwm_a = 3;  //PWM control for motor outputs 1 and 2 
int dir_a = 2;  //direction control for motor outputs 1 and 2 
int dir_b = 8;
int pwm_b = 9;
int irSenRead1 =13;
int irSenRead2 =10;
int irSenRead3 =7;
int LED1=12;
int LED2=9;
int LED3=6;
int isObstacle1 = HIGH;
int isObstacle2 = HIGH;
int isObstacle3 = HIGH;
int delayRead =100;

void setup() {
  
  pinMode(irSenRead3 ,INPUT);
   pinMode(LED3 ,OUTPUT);
      pinMode(LED2 ,OUTPUT);
         pinMode(LED1 ,OUTPUT);
    pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
      pinMode(pwm_b, OUTPUT);
    pinMode(dir_a, OUTPUT);
        pinMode(dir_b, OUTPUT);
   Serial.begin(9600);
   analogWrite(pwm_a, 255);
     analogWrite(pwm_b, 255);

}

void GAUCHE() {

  isObstacle1 = digitalRead(irSenRead3);
   Serial.println(digitalRead(irSenRead3));

   // isObstacle ==low there is obstacle infront of sensor
  if (isObstacle1 == LOW) {
    digitalWrite(LED3, HIGH);
  }
  else
  {
 digitalWrite(dir_a, HIGH );
 digitalWrite(dir_b, LOW);
  }
     // isObstacle ==low there is obstacle infront of sensor
  if (isObstacle1 == HIGH) {
    digitalWrite(LED3, HIGH);
  }
  else
  {
 digitalWrite(dir_a, LOW );
 digitalWrite(dir_b, LOW );
  }
}
  // test capteur centre

void droite() {

  isObstacle2= digitalRead(irSenRead2);
    Serial.println(digitalRead(irSenRead2));
    if(isObstacle2 == LOW) {
      digitalWrite(LED2, HIGH);
    }
    else
    {
      digitalWrite(dir_a, LOW);
      digitalWrite(dir_b, HIGH);
    }
    if(isObstacle2 == HIGH) {
      digitalWrite(LED2, HIGH);
    }
    else
    {
      digitalWrite(dir_a, LOW);
      digitalWrite(dir_b, LOW);
    }
}
void Arriere() {

  isObstacle1= digitalRead(irSenRead1);
    Serial.println(digitalRead(irSenRead1));
    if(isObstacle1 == LOW) {
      digitalWrite(LED1, HIGH);
    }
    else
    {
      digitalWrite(dir_a, HIGH);
      digitalWrite(dir_b, HIGH);
    }
    if(isObstacle1 == HIGH) {
      digitalWrite(LED1, HIGH);
    }
    else
    {
      digitalWrite(dir_a, HIGH);
      digitalWrite(dir_b, HIGH);
    }

void loop() {
  
}
  delay(delayRead);
}