# Pausing a Stepper Motor

I am trying to start a stepper once a button has been pressed through a call back function.

When the button is pressed (with the Distance == 0), the stepper should rotate one direction until it has reached a predetermined distance (Distance == 16000).

At this point, as long as the sensor reads LOW, the stepper should rotate the same distance in the opposite direction.

But if the stepper Distance == 1600 and the sensor is HIGH, the stepper motor should pause until the sensor goes LOW again. Once the sensor is LOW again, it should continue until it has completed the desired distance.

My current attempt will not compile and can be seen below.

Any help would be much appreciated.

``````#include <Stepper.h>

int Distance = 0;

const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 9, 10);

const int buttonPin = A0;
const int sensorPin = A1;

int buttonState = LOW;
int sensorState = LOW;

void setup() {

pinMode(A1,INPUT);

myStepper.setSpeed(1600);

}

void loop(){

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
openDoor();
}

}

void openDoor(){

//the stepper should start turning once the button is pressed
if(Distance == 0 ){
digitalWrite(10, HIGH);
delayMicroseconds(100);
digitalWrite(10, LOW);
delayMicroseconds(100);
Distance = Distance + 1
}

//if the sensor is low, and the stepper Distance == 16000, then it should rotate the oppsosite direction
//but if the sensor is high, then the stepper should pause until the sensor goes low again
if (sensorState==LOW)
{
int i;
digitalWrite(9, HIGH);
while (Distance == 16000)
{
digitalWrite(10, LOW);
digitalWrite(10, HIGH);
delayMicroseconds(200);
}
}
}
``````

That while loop at the end is infinite. The condition is about distance, but distance doesn't change in the body of the while loop. So you can never escape it.

You're also missing a semicolon on the end of the Distance = Distance + 1 line.

I think the logic would be easier to develop and understand if you had two separate stepper functions - perhaps called openDoor() and closeDoor()

Then the code in loop() could be something like
void loop() {
openDoor();
closeDoor();
}

and the code in openDoor() and in closeDoor() can test for the necessary condition that allows them to function

Thank you for your help.

I rewrote the code with the follwoing callback functions:

checkButton()
checkSensor()

openDoor()
pauseDoor()
closeDoor()

However, I am still having trouble when the sensor is HIGH. The stepper still wants to complete a full cycle in both directions instead of pausing after openDoor. I assume the problem stems from how my “if” statement is set up but I’m not sure how to code otherwise.

``````#include <Stepper.h>

int Distance = 0;

const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 9, 10);

const int buttonPin = A0;
const int sensorPin = A1;

int buttonState = LOW;
int sensorState = LOW;

void setup() {

pinMode(A1,INPUT);

myStepper.setSpeed(1600);

}

void loop(){

checkButton();
checkSensor();

if(buttonState==HIGH && sensorState==LOW) {
openDoor();
closeDoor();
}

if (buttonState==HIGH && sensorState == HIGH ) {

pauseDoor();
}
}

void checkButton(){

}

void checkSensor(){

}

void openDoor(){

for(int i = 0; i < 5; i++)
myStepper.step(16000);
delay(2000);
}

void closeDoor () {

for(int i = 0; i < 5; i++)
myStepper.step(-16000);
delay(2000);
}

void pauseDoor(){
myStepper.step(0);
}
``````

What I have in mind is that you move the test
if(buttonState==HIGH && sensorState==LOW) {
inside the openDoor() function and have a similar test inside the closeDoor() and pauseDoor() functions. That way each function can respond to the conditions in a different way.

It may also be useful for a function to set a variable to show that it has completed and that value could be one of the conditons for another function.

I suspect there is no need for the pauseDoor() function.

...R

My apologies for the redundant questions but I don’t seem to be making any progress. I have even tried researching stepper motor control with limit switches but cannot make adequate adaptations. I currently have this code which will not compile.

``````int Distance = 0;

const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 9, 10);

const int buttonPin = A0;
const int sensorPin = A1;

int buttonState = LOW;
int sensorState = LOW;

void setup() {

pinMode(A1,INPUT);

myStepper.setSpeed(1600);

}

void loop(){

checkButton();
checkSensor();

openDoor();
closeDoor();

}

void checkButton(){

}

void checkSensor(){

}

void openDoor(){

if(buttonState==HIGH){
if(sensorState==HIGH){
myStepper.step(0);
}
if(sensorState==LOW){
for(int i = 0; i < 5; i++)
myStepper.step(16000);
delay(2000);
}
}
else(buttonState==LOW){
if(sensorState==HIGH){
myStepper.step(0);
}

}
}
void closeDoor () {

if(buttonState==HIGH)
{
if(sensorState==HIGH){
myStepper.step(0);
}

else (sensorState==LOW){
for(int i = 0; i < 5; i++)
myStepper.step(-16000);
delay(2000);}
}
else(buttonState==LOW){
myStepper.step(0);
}
}
``````

Any suggestions on where I might find similar code to what I’m looking for or help with coding one of the call back functions?

I don’t mean to be unkind but I get the impression that you are not studying your own code carefully.

For example, in this

`````` if(sensorState==LOW){
for(int i = 0; i < 5; i++)
myStepper.step(16000);
delay(2000);
}
``````

why have you got the line for(int i = 0; i < 5; i++). I think that (and there may be more like it) is the cause of the compiler error. And it is simple to say “remove it altogether”.

But I don’t think your learning will progress unless you explain why you put it there in the first place - what do you want it to do?

Any suggestions on where I might find similar code to what I’m looking for

Don’t go looking for different code just because there is a problem with existing code. That is the scattergun approach to programming and it is rarely profitable. Just concentrate, bit by bit, on getting the existing code to work.

…R

I don’t think you are being unkind and I appreciate the feedback.

I placed “for(int i = 0; i < 5; i++)” because it was simply something I found a few months back that worked with a different sketch I was compiling.

I assumed that line of code would start a stepper at rest and allow it to rotate only a specified number of revolutions.

What error messages are you seeing? YOU should post them.

Including the Stepper header file will be necessary before you can create an instance of the Stepper class.

``````else(buttonState==LOW){
``````

What is this crap? If the buttonState is not HIGH, there are NO other choices than for it to be LOW.

Getting rid of all the claptrap like that, and adding the include statement, I get

Binary sketch size: 2,346 bytes (of a 30,720 byte maximum)

wickedhurricane:
I placed “for(int i = 0; i < 5; i++)” because it was simply something I found a few months back that worked with a different sketch I was compiling.

I assumed that line of code would start a stepper at rest and allow it to rotate only a specified number of revolutions.

Look at the line of code carefully and think about what it is trying to do …
for ( int i = 0; i < 5; i++)

• It is the start of a FOR loop
• it creates an integer variable called i
• it starts the variable at 0
• it continues until the variable gets to 4
• it increases the variable by 1 each time
Note that there is nothing inside the FOR loop because there are no {} brackets
and, for the same reason there is no end to the loop

Now, the \$64 question - how can that make a stepper motor do anything ?

Don’t add code to a project like you sprinkle salt on your chips.

…R

Note that there is nothing inside the FOR loop because there are no {} brackets

In the absence of brackets, the body of the for loop is the next statement, the one that makes the stepper step 16000 times.

PaulS:
In the absence of brackets, the body of the for loop is the next statement, the one that makes the stepper step 16000 times.

I was rather hoping you would not say that as, while it is correct, I don't think it was any part of the OP's plan.

...R

I was rather hoping you would not say that as, while it is correct, I don't think it was any part of the OP's plan.

Not likely, but I think it's better to explain exactly why the for loop is wrong. What it is doing is most likely not what OP wants, but why it isn't needs to be explained.

I decided to add another call back function called pauseDoor which should only be initiated when the sensor is HIGH as seen below.

openDoor();
while(sensorState==HIGH){pauseDoor;}
closeDoor();

However, once the sensor goes low again, the stepper remains stopped.

The code is below and any advice would be much appreciated.

``````#include <Stepper.h>

int Distance = 0;

const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 9, 10);

const int buttonPin = A0;
const int sensorPin = A1;

int buttonState = LOW;
int sensorState = LOW;

void setup() {

pinMode(A1,INPUT);

myStepper.setSpeed(1600);

}

void loop(){

checkButton();
checkSensor();

if(buttonState==HIGH)
{
openDoor();
while(sensorState==HIGH){pauseDoor();}
closeDoor();
}

}

void checkButton(){

}
void checkSensor(){
}

void openDoor(){
myStepper.step(16000);
delay(1000);
}

void pauseDoor(){
myStepper.step(0);
}

void closeDoor(){
myStepper.step(-16000);
delay(1000);
}
``````

This line of code is wrong

``````while(sensorState==HIGH){pauseDoor();}
``````

because there is nothing on that line or in the function pauseDoor() that will change the value of the variable sensorState to LOW so as to terminate the WHILE

I find it much easier to read if that piece of code is laid out like

``````while(sensorState==HIGH){
pauseDoor();
}
``````

When you are replying please describe again what the latest version of your program actually does and what you want it to do. This has been a long Thread and I have forgotten what the original problem was. (and it may no longer be relevant).

...R