Ultrasonic sensor and a steppenmotor

Hi everyone,

(I posted this a few minutes ago but it was without the code)

I’m working on a project with Arduino and I am pretty new with programming and the arduino itself. I am trying to make a steppenmotor that stops spinning when something is too close to the ultrasonic sensor. Now my problem is that when I place something in front of the ultrasonic sensor, the motor stops, but when I take away the thing in front, it does not start spinning again.

Im using a hc-sr04 as ultrasonic sensor and a 28BJY-48 as steppenmotor. Between the arduino UNO and the steppenmotor, i placed a ULN2003.

This is the code I’m using

#include <Stepper.h>
const int stepsPerRevolution = 2048;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

#define trigPin 12
#define echoPin 13
int duration, distance;

void setup() {
  myStepper.setSpeed(15);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin (9600);
}

void loop() {
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;

  myStepper.step(stepsPerRevolution);

  if (distance >= 400){
    Serial.print("Distance = ");
    Serial.println("Out of range");
  }
  if (distance <= 10){
    Serial.print("Distance = ");
    Serial.println("STOP");
    myStepper.setSpeed(0);
  }
  else{
    Serial.print("Distance = ");
    Serial.print(distance);
    Serial.println(" cm");
    myStepper.setSpeed(15);
    myStepper.step(stepsPerRevolution);
  }

}

Try changing this

if (distance <= 10){

to

else if (distance <= 10){

I assume you do not want the motor to move if the sensor detects a range greater than 400.

The standard Stepper library is probably not suitable for what you are trying to do because this

myStepper.step(stepsPerRevolution);

will not stop until all 2048 steps have been done even if the ultrasonic sensor detects something.

One way is to move the motor one step at a time and manage the step timing yourself.

Or you could use the AccelStepper library which has a lo more capability and can do non-blocking moves.

…R

I want the motor to always move, except if the distance is less dan 10 centimeters. I also saw the Accelstepper library, but how can i move my motor step by step with the same speed as this code?

Disregard.

Lessiy:
I want the motor to always move, except if the distance is less dan 10 centimeters.

In that case ignore my suggestion about ELSE.

I also saw the Accelstepper library, but how can i move my motor step by step with the same speed as this code?

I think you are mixing up things here. My "step by step" suggestion is only relevant if you want to use the standard stepper library. You could manage the timing using millis() - see the demo Several Things at a Time. And have a look at Using millis() for timing. A beginners guide if you need more explanation.

You can use the AccelStepper library in non-blocking mode (with the functions run() or runSpeed() ) .

...R