Stepper motors controlled using Ultrasonic sensor

Good day,

I'm in the middle of a project where I want to control 2 stepper motors using Adafruit Motorshield(ver.1)- with external power(5v) & Arduino Mega. Movement direction of the steppers (28BYJ-48 5V) has to be controlled using input from an Ultrasonic sensor(HC-SR04).

When the distance is more than 20cm both the steppers have to move in Fwd direction and when the distance is between 10-20cm it should turn left(1-stepper fwd & the other reverse). Similarly when the distance is less than 10 cm it should move back wards and turn right.

Now the issue which i'm facing is that :

  • steppers are rotating at a very slow speed taking around 4 times the actual time it takes to complete one rotation. Without Ultrasonic sensor, they are almost 4 times faster.

What could be causing the steppers to slow down? Could it be an error in my code or could it be the Ultrasonic sensor slowing down the steppers?

Ultrasonic reading is intermittently giving 0 cm reading even when the obstruction is kept at same distance.

Here's the code:

#include <AFMotor.h>
#include <NewPing.h>  

int trigPin = 51;       // Transmit - Trigger
int echoPin = 53;       // Receive - Echo
 
NewPing sonar(trigPin, echoPin, 200);  // define an utrasonic sensor and name it 'sonar' wih a maximum range of 200cm

int distance;      // For use with ultrasonic sensor in the 'rangeDetect' function below

AF_Stepper motor1(4096, 1); //AF_Stepper(steps, stepper#)
AF_Stepper motor2(4096, 2); //AF_Stepper(steps, stepper#)

void setup() {

  motor1.setSpeed(60);  // 60 rpm
  motor2.setSpeed(60);  // 60 rpm
  Serial.begin(9600);//start the serial port at 9600 baud
  pinMode(trigPin, OUTPUT);             // Set socket 51 as output socket
  pinMode(echoPin, INPUT);              // Set socket 53 as an input socket
  
}


void loop() {                   // *** The contents of this function will be run repeatedly ***
 
 if (rangeDetect() > 10 && rangeDetect()< 20){   // When the distance of the nearest is onject is less than 20cm & more than 10cm, move Left
  leftStep();                                                                    
  
  }
 else{  
  forwardStep();
  
   }

 if (rangeDetect() < 10){   // When the distance of the nearest is onject is less than 10cm
        backwardStep();         
        delay(10);
        rightStep();
 }  
}




//to move the motors forward
void forwardStep() {
    // step one step each forward
 motor2.step(1, FORWARD, MICROSTEP);//step(#steps, direction, steptype)
 motor1.step(1, FORWARD, MICROSTEP); 
 delay(10);
}

// to move the motors back
void backwardStep() {
    // step one step each backward
  motor1.step(1, BACKWARD, MICROSTEP); //step(#steps, direction, steptype)
  motor2.step(1, BACKWARD, MICROSTEP);
  delay(10);
}


// to move the motors in opposite directions (left)
void leftStep() {
    // step one step each left
  motor1.step(1, FORWARD, MICROSTEP); //step(#steps, direction, steptype)
  motor2.step(1, BACKWARD, MICROSTEP);
  delay(10);
}


// to move the motors in opposite directions (right)
void rightStep() {
   // step one step each right
  motor1.step(1, BACKWARD, MICROSTEP); //step(#steps, direction, steptype)
  motor2.step(1, FORWARD, MICROSTEP);
  delay(10);
}

// to power down the motor drivers and stop the motors
void allStop() {
    // steppers stop
  motor1.release();
  motor2.release();

}

int rangeDetect() {             // This function is used for determining the distance of any obstacle
  distance = sonar.ping_cm();   // The value is placed in a variable called 'distance'
 
  Serial.print("Distance: "); //Print text with no carriage return (on my laptop)
  Serial.println(distance);   //Give me a readout then carriage return
 
  return distance;            // The distance value that was found is sent bak up to the 'rangeDetect' function that called it
 
}

Kindly assist.

Thanks in advance

Delays are best avoided, you have a lot of them in your code is that part of the problem.
I would add more print statements into the code to see what is happening.

The fact that you get 0cm readings is odd.
If you do get 0cm reading it looks like your device will do a forwardStep, then a backwardStep, then a delay then a rightStep. All the steps also include delays. Is that what was intended?

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

I was under the impression that 28BYJ stepper motors are usually paired with a ULN2003 as the driver.

...R