Motor control using two ultrasonic distance sensors

Hi everyone.
Let me go straight into the problem: I have been writing a code to control a dc motor using two ultrasonic sensors 1 & 2 measuring distance1 and distrance2 respectively.

  1. As the motor is moving forward the distance1 is increasing from 95 cm until it is zero (out or range).
  2. Whilst the motor is moving forward, the distance2 is also increasing from 28 cm up 41 cm. The distance2 defines my upper limit and lower limits for the operation of the motor.
  3. i want the motor to immediately reverse as distance1 is out of range. The following code i have been using does not reverse.
    House please help out with fixing my codes below so the motor reverse back when the distance1 is out of range and stops when the upper limit is 39 cm:

#include <NewPing.h>

#define TRIG_PIN1 4
#define ECHO_PIN1 5
#define TRIG_PIN2 9
#define ECHO_PIN2 10
#define MAX_DISTANCE 200

NewPing sonar1(TRIG_PIN1, ECHO_PIN1, MAX_DISTANCE);
NewPing sonar2(TRIG_PIN2, ECHO_PIN2, MAX_DISTANCE);

int CRD_REFERENCE = 107; // Define CRD_REFERENCE
int CRD_HEIGHT = 0; // Initialize CRD_HEIGHT with a default value

const float US_upper_limit = 41.0;
const float US_lower_limit = 29.0;

#define RPWM 11
#define LPWM 12

void setup() {
Serial.begin(9600); // Start serial communication
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);

// Stop motors
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
}

void loop() {
// Trigger the first sensor
unsigned int distance1 = sonar1.ping_cm();
Serial.print("Distance1: ");
Serial.print(distance1);
Serial.println(" cm");

// Trigger the second sensor
unsigned int distance2 = sonar2.ping_cm();
Serial.print("Distance2: ");
Serial.print(distance2);
Serial.println(" cm");

delay(1000); // Wait for 1 second before taking another reading

// Check conditions for DEFLECTOR_UP
if (distance1 >= 95 ) {
// if (distance1 >= 95 && distance2 < US_upper_limit) {
// Accelerate forward
digitalWrite(RPWM, LOW);
for (int i = 0; i < 255; i++) {
analogWrite(LPWM, i);
delay(20);
}

// Decelerate forward
for (int i = 255; i >= 0; i--) {
  analogWrite(LPWM, i);
  delay(20);
}

CRD_HEIGHT = distance1; // Assuming CRD_HEIGHT is a global variable

} else { // Reverse when distance2 exceeds the upper limit
//else if (distance2 > US_upper_limit) { // Reverse when distance2 exceeds the upper limit
// Accelerate reverse
digitalWrite(LPWM, LOW);
for (int i = 0; i < 255; i++) {
analogWrite(RPWM, i);
}

// Decelerate reverse
for (int i = 255; i >= 0; i--) {
  analogWrite(RPWM, i);
}

delay(500);

} if (CRD_REFERENCE == (CRD_HEIGHT - 10)) { // Note the use of '==' instead of '='
}
else {
// Stop motors
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
delay(2000);
}

// Finding Trailer Height
//if (CRD_REFERENCE == (CRD_HEIGHT - 10)) { // Note the use of '==' instead of '='
CRD_REFERENCE = CRD_HEIGHT;
}

I look forward to receiving any help.
Regards,

Baba

Please take a minute and read How to get the best out of this forum, then edit your post to follow the guidelines. Thank you!

Cross posted at ASE programming - Arduino motor control with US sensors - Arduino Stack Exchange

And it seems like it is getting the appropriate response there.

2 Likes

You'll probably get more and better help if instead of peppering your same poorly posted question all over multiple sites with the same readers you spent just a few minutes to take in the posting guidelines of just one of those sites and made a proper post out of your question.

No, you've got delay all over this code. Nothing will ever be immediate with this code.

2 Likes

Check against the following diagram (Fig-1) if you are making any mistake in the definitions of PWM Pins?
pwm328xy
Figure-1:

1 Like

Hi @Bamusa ,
Welcome to the forum..

Looks to me like you're only using distance1..
And yes, blocking code is stopping you..

And yes, Uno ping 12 not a pwm pin, look for the ~ by the pin number..
I switched to pin 6..

Here's some untested code with all delays removed..

#include <NewPing.h>

#define TRIG_PIN1 4
#define ECHO_PIN1 5
#define TRIG_PIN2 9
#define ECHO_PIN2 10
#define MAX_DISTANCE 200

NewPing sonar1(TRIG_PIN1, ECHO_PIN1, MAX_DISTANCE);
NewPing sonar2(TRIG_PIN2, ECHO_PIN2, MAX_DISTANCE);

int CRD_REFERENCE = 107; // Define CRD_REFERENCE
int CRD_HEIGHT = 0; // Initialize CRD_HEIGHT with a default value

const float US_upper_limit = 41.0;
const float US_lower_limit = 29.0;

#define RPWM 11
#define LPWM 6

byte direction = 0;
int stepSpeed = 0;
byte stepMode = 0;
unsigned long lastStep = 0;
int intervalStep = 20;

unsigned int distance1, distance2;



void setup() {
  Serial.begin(9600); // Start serial communication
  pinMode(RPWM, OUTPUT);
  pinMode(LPWM, OUTPUT);

  // Stop motors
  analogWrite(RPWM, 0);
  analogWrite(LPWM, 0);
}

unsigned long lastPing = 0;
int intervalPing = 1000;

void loop() {

  //limit pings to once per second..
  if (millis() - lastPing >= intervalPing) {
    lastPing = millis();
    // Trigger the first sensor
    distance1 = sonar1.ping_cm();
    Serial.print("Distance1: ");
    Serial.print(distance1);
    Serial.println(" cm");

    // Trigger the second sensor
    distance2 = sonar2.ping_cm();
    Serial.print("Distance2: ");
    Serial.print(distance2);
    Serial.println(" cm");
  }


  // Check conditions for DEFLECTOR_UP
  if (distance1 >= 95 ) {
    // if (distance1 >= 95 && distance2 < US_upper_limit) {
    // Accelerate forward
    if (direction != 0) {
      direction = 0;
      stepSpeed = 0;
      stepMode = 0;
    }
    digitalWrite(RPWM, LOW);
    if (AccelMotor(LPWM) == 2) {
      CRD_HEIGHT = distance1; // Assuming CRD_HEIGHT is a global variable
    }

  } else { // Reverse when distance2 exceeds the upper limit
    //else if (distance2 > US_upper_limit) { // Reverse when distance2 exceeds the upper limit
    // Accelerate reverse
    if (direction != 1) {
      direction = 1;
      stepSpeed = 0;
      stepMode = 0;
    }
    digitalWrite(LPWM, LOW);
    AccelMotor(RPWM);

  }

  if (CRD_REFERENCE == (CRD_HEIGHT - 10)) { // Note the use of '==' instead of '='
  }
  else {
    // Stop motors
    StopMotors();
  }

  // Finding Trailer Height
  //if (CRD_REFERENCE == (CRD_HEIGHT - 10)) { // Note the use of '==' instead of '='
  CRD_REFERENCE = CRD_HEIGHT;
}


void StopMotors() {
  // Stop motors
  analogWrite(RPWM, 0);
  analogWrite(LPWM, 0);
}


byte AccelMotor(byte motor) {
  // Accelerate forward
  if (millis() - lastStep >= intervalStep && stepMode < 2) {
    lastStep = millis();
    analogWrite(motor, stepSpeed);
    if (stepSpeed < 255 && stepMode == 0) {
      stepSpeed++;
      if (stepSpeed == 255) stepMode = 1;
    } else if (stepSpeed > 0 && stepMode == 1) {
      stepSpeed--;
      if (stepSpeed == 0) stepMode = 2;
    }
  }
  return stepMode;
}

good luck.. ~q

Hello qubits-us

Thank you for your kind welcome and insightful comments! You are entirely correct; I have been concentrating primarily on distance1. I appreciate your proposal for resolving the blocked code problem.
Regarding the PWM pin, thank you for pointing out to me that Uno pin 12 is not a PWM pin. I've swapped to pin 6 as you advised.
I'll test your code .
Your assistance is much appreciated!
Best wishes,
Bamusa

1 Like

It was also pointed out to you first in post #5.

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