Arduino ultrasonic sensor and steppemotor

Hi everyone,

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);
  }

}

I would try to help if I could see your code. Unfortunately I cannot open ino files on my device even if I were disposed to download them.

Read how to post code so that everyone can see it in the how to use this forum stickies at the top of each topic section.

I added the code to my original message