Controlling DC Motors at a Slow Speed With an Ultrasonic Sensor

I'm trying to jump start the motors with a brief high voltage then have them run at a slow speed when the ping time is above 500. When ping time is below 500 I'm trying to again jump start the motors with a brief high voltage in the opposite direction, then move backwards a bit at an angle for a few seconds. More or less how the first generation of Roombas ran.

The problem I'm running into is that when ping time is above 500 the motors only run at a very high speed. When I delete the conditional if statement, essentially taking the ultrasonic sensor out of the picture it works as I intend it to at least in 1 direction. Jump starting then running at a slow speed.

int trigPin=12;
int echoPin=11;
int pingTravelTime;

int motor1pin1=2;
int motor1pin2=4;
int motor2pin1=7;
int motor2pin2=8;

int motor1speedcontroller=6;
int motor2speedcontroller=3;

void setup() {
  // put your setup code here, to run once:
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);

  pinMode(motor1pin1,OUTPUT);
  pinMode(motor1pin2,OUTPUT);
  pinMode(motor2pin1,OUTPUT);
  pinMode(motor2pin2,OUTPUT);

  pinMode(motor1speedcontroller,OUTPUT);
  pinMode(motor2speedcontroller,OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  digitalWrite(trigPin,LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  pingTravelTime=pulseIn(echoPin,HIGH);
  delay(25);
  Serial.println(pingTravelTime);

  if (pingTravelTime > 500){
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, HIGH);
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, HIGH);
    analogWrite(motor2speedcontroller,255);
    analogWrite(motor1speedcontroller,255);
    delay(5);  
    analogWrite(motor2speedcontroller,22);
    analogWrite(motor1speedcontroller,27);   
  }
  if (pingTravelTime < 500){
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
    digitalWrite(motor2pin1, HIGH);
    digitalWrite(motor2pin2, LOW);
    analogWrite(motor2speedcontroller,255);
    analogWrite(motor1speedcontroller,255);
    delay(5);  
    analogWrite(motor2speedcontroller,22);
    analogWrite(motor1speedcontroller,35); 
    delay(2000);
  }
}

A glance at the code shows that the program is performing exactly as expected.

  if (pingTravelTime > 500){

Shouldn't the motors slow down with this bit of code though after running at a high speed? How does the conditional if statement work in this context? Because when I take this bit of code out of the if conditional statement, essentially taking the ultrasonic sensor out of the picture it works as I'm trying to get it to. At least in 1 direction.

    analogWrite(motor2speedcontroller,255);
    analogWrite(motor1speedcontroller,255);
    delay(5);  
    analogWrite(motor2speedcontroller,22);
    analogWrite(motor1speedcontroller,27);   

What is the big picture? What's that motor pulling?

Nothing so far, just have the motors free hanging on a cloth surface.

Okey but what's the setup You aim at?

Just learning I suppose.

Okey.
No offence but sometimes members are heading into a dead end. There's no point spending time helping them and then discover is all wasted. Better to get the good heading from the beginning.
Your description, turning backwards and forwards and a quick run makes no sense to me. What do I miss?
Please post the entire code.

No worries. I can explain a bit more since I understand where you are coming from now. Thank you for taking the time to be thorough. The end goal is to make an autonomous 4 wheel robot that will randomly traverse a room. Pretty simple I think. I want it to travel forward until it reaches within a few inches of an object then it will backup at a 45 degree angle for a few seconds, then continue traveling forward again. I hope to accomplish this with a motor controller, ultra sonic sensor, 2 DC motors, and an Arduino UNO.

With DC motors if you want them to run at a slow speed from what I understand, sometimes you need to jumpstart them for a brief second with a high voltage then you can set the speed low. I can accomplish this with the following

digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
analogWrite(motor2speedcontroller,255);
analogWrite(motor1speedcontroller,255);
delay(5);
analogWrite(motor2speedcontroller,25);
analogWrite(motor1speedcontroller,25); 

I can also run the DC motors starting at a high speed (no jumpstarting) based on distance using the ultra sonic sensor with the following

if (pingTravelTime > 500){
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, HIGH);
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, HIGH);
    analogWrite(motor2speedcontroller,255);
    analogWrite(motor1speedcontroller,255);   
  }

But I can not seem to combine the 2 concepts where I first jump start a motor with a high voltage for a brief second then have it run at a slow speed based on distance. The following does not work.

 if (pingTravelTime > 500){
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, HIGH);
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, HIGH);
    analogWrite(motor2speedcontroller,255);
    analogWrite(motor1speedcontroller,255);
    delay(5);
    analogWrite(motor2speedcontroller,25);
    analogWrite(motor1speedcontroller,25); 
  }

The above code only runs the motor at a high speed and does not slow down.

The code in the original post is all of it. If need be I can post the full code of what I've managed to get to work, and what doesn't work.

What driver are you using?

I'm using the driver L298N.

Could it be the difference between the post-slowdown delays in the two cases?

The response to PWM control of a motor under load is very different than the response of a motor with no load, so this effort is not teaching you much.

Built your rover and use it for experiments with motor control.

That doesn't seem to be it.

PWM works at the same voltage, whether high or low duty cycle. By starting with analogWrite(xx,255) you're holding the same high voltage on for longer than you are with analogWrite(xx,23) but it is the same voltage. I think it may function without the "kick".

I see, thank you. I think I will go with that.

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