DC motors and ultrasonic for a buggy

Hello everyone,

I am trying to write a code that would enable a robotic buggy to move straight from a specific starting point towards a wall, which could be up to 3.5 meters away, then rotate 180 degrees and return to the starting position as swiftly and accurately as possible.

Below is the circuit that I have created in tinkercad:

I have started to write the code which is shown below:

const int trigPin = 13; // Trigger pin of ultrasonic sensor
const int echoPin = 12; // Echo pin of ultrasonic sensor
const int motor1Pin1 = 9; // Motor 1 control pin 1
const int motor1Pin2 = 10; // Motor 1 control pin 2
const int motor2Pin1 = 6; // Motor 2 control pin 1
const int motor2Pin2 = 5; // Motor 2 control pin 2
const int enablePin1 = 11; // Motor 1 enable pin
const int enablePin2 = 3; // Motor 2 enable pin
const int TURN_DURATION = 2600; // quickness of turning (note this is in milliseconds)

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);
  
  digitalWrite(enablePin1, HIGH); // Enable Motor 1
  digitalWrite(enablePin2, HIGH); // Enable Motor 2
}
   void driveForward() {
  analogWrite(enablePin1, 127);
  analogWrite(enablePin2, 127);
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
   }

void stopCar() {
 digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

void turnAround() {
 analogWrite(enablePin1, 127);
  analogWrite(enablePin2, 0);
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(TURN_DURATION);
}

//void recordDistance(){
 //digitalWrite(trigPin, LOW);
  //delayMicroseconds(2);
  //digitalWrite(trigPin, HIGH);
  //delayMicroseconds(10);
  //digitalWrite(trigPin, LOW);
//pencil = pulseIn(echoPin, HIGH);
  //}

  
void loop() {
  // Send ultrasonic pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Receive ultrasonic echo
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate distance in cm
  distance = duration/2 * 0.034;


if (distance <= 32.5) { // If the car is very close to the wall (adjust threshold as needed)
    // Stop the car
    stopCar();
    delay(1000);
    
    // Turn 180 degrees
    turnAround();
    
    // Drive back to start position
   driveForward();
   delay(1000); //this is the problem section I believe
    stopCar();
return;
 }else
    // Drive forward
    driveForward();
  return;
}

Currently, the above code allows the buggy to come within 32.5cm to a wall, turns around 180 degrees without a problem. However, it then drives forward and does not seem to stop. What am I missing.

I have also tried the above code using a switch case but again I cant get it working.

Any help with be greatly appreciated.

The return statements are not necessary and may cause problems later on, remove them.
How does the buggy know it has got back to the starting position?

I have now removed the return statements.
In terms of the buggy known its got back to the start position, I was hoping to do it based on the duration it took the buggy to reach the wall.
Essentially, the ultrasonic sensor and the code would calculate the duration it took the buggy to get to the wall, then when it turns around the buggy would carry on moving until the travel duration matches.

I had the same idea.
So, use millis() and save the time you started to move forward and the time when you reached the wall. The difference will be the time it took you to reach the wall.
After you turn around, save the time you started on the return trip.
Check millis() to see if the difference is equal to or greater than the time it took you to get to the wall.

Give a try. If you still need help just let me know.

I think I have tried to include your suggestion in my code:

const int trigPin = 13; // Trigger pin of ultrasonic sensor
const int echoPin = 12; // Echo pin of ultrasonic sensor
const int motor1Pin1 = 9; // Motor 1 control pin 1
const int motor1Pin2 = 10; // Motor 1 control pin 2
const int motor2Pin1 = 6; // Motor 2 control pin 1
const int motor2Pin2 = 5; // Motor 2 control pin 2
const int enablePin1 = 11; // Motor 1 enable pin
const int enablePin2 = 3; // Motor 2 enable pin
const int TURN_DURATION = 1900; // quickness of turning (note this is in milliseconds)

long duration;
int distance;
long startTime = millis(); // start time when the buggy starts moving
long wallTime; // time taken to reach the wall

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);
  
  digitalWrite(enablePin1, HIGH); // Enable Motor 1
  digitalWrite(enablePin2, HIGH); // Enable Motor 2
}
   void driveForward() {
  analogWrite(enablePin1, 255);
  analogWrite(enablePin2, 255);
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
   }

void stopCar() {
 digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

void turnAround() {
 analogWrite(enablePin1, 255);
  analogWrite(enablePin2, 0);
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(TURN_DURATION);
}
  
void loop() {
  // Send ultrasonic pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Receive ultrasonic echo
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate distance in cm
  distance = duration/2 * 0.034;


if (distance <= 32.5) { // If the car is very close to the wall (adjust threshold as needed)
   wallTime = millis()-startTime; // calculate the time taken to get to the wall
  // Stop the car
    stopCar();
    delay(1000);
    
    // Turn 180 degrees
    turnAround();
    
    // Drive back to start position
   driveForward();
 if (millis()- wallTime <=0){
  // delay(1000);
    stopCar();
 }
 }else
    // Drive forward
    driveForward();
 
}

unfortunately, it is still not stopping the buggy. However, I am pretty sure it is due to my lack of coding experience. Could you have a look and see where I am going wrong?

Very close
Try this:

//
const int trigPin = 13; // Trigger pin of ultrasonic sensor
const int echoPin = 12; // Echo pin of ultrasonic sensor
const int motor1Pin1 = 9; // Motor 1 control pin 1
const int motor1Pin2 = 10; // Motor 1 control pin 2
const int motor2Pin1 = 6; // Motor 2 control pin 1
const int motor2Pin2 = 5; // Motor 2 control pin 2
const int enablePin1 = 11; // Motor 1 enable pin
const int enablePin2 = 3; // Motor 2 enable pin
const int TURN_DURATION = 1900; // quickness of turning (note this is in milliseconds)

long duration;
int distance;
unsigned long startTime;
unsigned long wallTime; // time taken to reach the wall

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);

  digitalWrite(enablePin1, HIGH); // Enable Motor 1
  digitalWrite(enablePin2, HIGH); // Enable Motor 2
  driveForward(); // Move forward
  startTime = millis(); // Start time when the buggy starts moving

}
void driveForward() {
  analogWrite(enablePin1, 255);
  analogWrite(enablePin2, 255);
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
}

void stopCar() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

void turnAround() {
  analogWrite(enablePin1, 255);
  analogWrite(enablePin2, 0);
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(TURN_DURATION);
}

void loop() {
  // Send ultrasonic pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Receive ultrasonic echo
  duration = pulseIn(echoPin, HIGH);

  // Calculate distance in cm
  distance = duration / 2 * 0.034;


  if (distance <= 32.5) { // If the car is very close to the wall (adjust threshold as needed)
    wallTime = millis() - startTime; // calculate the time taken to get to the wall
    // Stop the car
    stopCar();
    delay(1000);

    // Turn 180 degrees
    turnAround();

    // Drive back to start position
    driveForward();
    startTime = millis(); // Start time for the return trip
    
    if ((millis()-startTime) >= wallTime) {
      stopCar();
      while (1) delay(1); // Do nothing forever!!
    }
  }
}

Have fun!

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