How can I pause a stepper motor?

Hey, I am trying to use an ultrasonic sensor to control a stepper motor. I can get the sensor to turn the motor, but I need to stop the motor in place while the sensor is detecting something in a given range, and then rotate back to it's original spot

I'm using an Arduino Uno, a Longruner 5X Geared Stepper Motor 28byj 48 Uln2003 5v Stepper Motor with a Uln2003 Driver Board.

#include <Stepper.h>

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

// defines pins numbers
const int trigPin = 7;
const int echoPin = 6;

// defines variables
long duration;
int distance;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
myStepper.setSpeed(60);
// initialize the serial port: (again??)

}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

if (distance < 10) {
myStepper.step(-stepsPerRevolution);
delay(1000);
if (distance > 10) {
myStepper.step(stepsPerRevolution);
delay(10000);
}
}
}

Stop right there and explain what you mean: "and then rotate back to it's original spot".

Do you know what the original spot is? How many steps away and which direction

Paul

If you want to be able to stop a stepper motor at any position then you need to move it one step at a time and check for the stop signal between steps.

...R
Stepper Motor Basics
Simple Stepper Code

Skrish12345:
Hey, I am trying to use an ultrasonic sensor to control a stepper motor. I can get the sensor to turn the motor, but I need to stop the motor in place while the sensor is detecting something in a given range, and then rotate back to it's original spot

What you wrote doesn't keep track of an "original spot". If moves the stepper 200 steps backward when the distance is less than 10 and moves the stepper 200 steps forward when the distance is simultaneously less than 10 and greater than 10 (in other words, never).

  if (distance < 10)
  {
    myStepper.step(-stepsPerRevolution);
    delay(1000);
    if (distance > 10)
    {
      myStepper.step(stepsPerRevolution);
      delay(10000);
    }
  }

When you say "and then rotate back to it's original spot" do you mean when the sensor no longer "detecting something in a given range"? And the 'given range' is "less than 10"?

Create a global variable for the distance moved from the 'original spot':

unsigned long DistanceSteps = 0;

Then change you code to move when in range and back when out of range.

  if (distance > 0 && distance < 10) // '0' means 'out of range'
  {
    myStepper.step(-stepsPerRevolution);
    DistanceSteps -= stepsPerRevolution;
  }
  else if (DistanceSteps != 0)
  {
    // Out Of Range: Go back to the Original Position
    myStepper.step(-DistanceSteps);
    DistanceSteps = 0;
  }
}