Using an ultrasonic sensor to control motor to move in the forward and reverse direction

Ok. so i want to control a motor to move in a forward direction for 10 seconds and stop when triggered by an ultrasonic sensor. but the motor keeps moving after the delay and won't stop. like i want it to move in a forward direction then stop but it keeps moving even after the delay you get?
int trig = 9;
int echo = 8;
int in1 = 7;
int in2 = 6;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
Serial.begin(9600);

}

void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration*0.034/2;

Serial.print("Distance: ");
Serial.println(distance);
delay(1000);

if ( distance <=10 ) {
Serial.print(distance);

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
Serial.println("cm MOTOR MOVES FORWARD");
delay(1000);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);

}
else {
Serial.print(distance);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Serial.println("cm MOTOR MOVES BACKWARDS");
delay(1000);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
}
}

Gonna be hard to debug your code without seeing your code. Please post your sketch.

int trig = 9;
int echo = 8;
int in1 = 7;
int in2 = 6;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
Serial.begin(9600);

}

void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration*0.034/2;

Serial.print("Distance: ");
Serial.println(distance);
delay(1000);

if ( distance <=10 ) {
Serial.print(distance);

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
Serial.println("cm MOTOR MOVES FORWARD");
delay(1000);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);

}
else {
Serial.print(distance);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Serial.println("cm MOTOR MOVES BACKWARDS");
delay(1000);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
}
}

delay(1000);

does not pause for 10 seconds, that is 1 second

What does your serial output show?

The motor rotates in the forward direction then pauses for a sec and keeps on rotating and vice versa when i bring a wall near the ultrasonic sensor you get?
What i want is for it to rotate for a sec then stop and vice versa when i bring a wall near the ultrasonic sensor

i really hope someone understands me

The way you have it programmed is if the distance is less than 10cm it will move forward for 1 second and if it is more than 10cm it will move in reverse for 1 second. It will do that over and over again. Is that what you want?

Hi @g4boi your program is working like this:

Read from a distance:
wait 1 second:
If the distance <= 10:
turn forward:
Rotate for a second:
Stop:
Read from a distance............
If the distance is greater than 10
turn back:
Rotate for a second:
Stop:
Read from a distance............
and it stays like this always...

RV mineirin

Not my program. :grinning:

No it's not what want . i want it to stop rotating after the forward movement until it is triggered to reverse.

OK. That is not hard but realize if you move forward and distance remains <=10 then the robot will be stuck. Also vice versa if you move in reverse and distance remains >10 then robot will be stuck.

This should work but I can't test right now. It will alternate between forward and reverse based on distance and stop flags. Like I said it leaves conditions where it can get stuck.

int trig = 9;
int echo = 8;
int in1 = 7;
int in2 = 6;
int duration = 0;
int distance = 0;
bool fwdStop = false;
bool revStop = false;

void setup()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);
  Serial.begin(9600);

}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);
  delay(1000);

  if ( distance <= 10 && !fwdStop) {
    Serial.print(distance);

    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    Serial.println("cm MOTOR MOVES FORWARD");
    delay(1000);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    fwdStop = true;
    revStop = false;

  }
  else ( distance > 10 && !revStop){
    Serial.print(distance);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    Serial.println("cm MOTOR MOVES BACKWARDS");
    delay(1000);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    fwdStop = false;
    revStop = true;
  }
}

yes that's what it does. but here's what i want

Read from a distance:
wait 1 second:
If the distance <= 10:
turn forward:
Rotate for a second:
Stop:
Read from a distance............
If the distance is still <=10
hold till distance is greater than 10
then
Read from a distance, if distance is greater than 10
move backwards
stop
hold till distance greater than 10
then move forward and so on

yes that's what i want
to be STUCK!

okay! will try and give you a feedback. i really appreciate the feedback.

Code correction:

int trig = 9;
int echo = 8;
int in1 = 7;
int in2 = 6;
int duration = 0;
int distance = 0;
bool fwdStop = false;
bool revStop = false;

void setup()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);
  Serial.begin(9600);

}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);
  delay(1000);

  if ( distance <= 10 && !fwdStop) {
    Serial.print(distance);

    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    Serial.println("cm MOTOR MOVES FORWARD");
    delay(1000);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    fwdStop = true;
    revStop = false;

  }
  else if ( distance > 10 && !revStop){
    Serial.print(distance);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    Serial.println("cm MOTOR MOVES BACKWARDS");
    delay(1000);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    fwdStop = false;
    revStop = true;
  }
}

Not the most elegant solution but I don't have much spare time today!

1 Like

Sorry, finger check. Now is correct.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.