Stepper motor instant stop

Hello guys I need help. I’m having a problem with a stepper motor project. I want it to stop instantly when i low 12 pin. it spins the rest of the cycle and then stops. Can anyone share a command of it? cheers.

#include <Stepper.h>
const int A = 12;
const int B = 8;
const int C = 9;
const int D = 10;
const int E = 11;
int AState = LOW;
int Bstate = LOW;
int Cstate = LOW;
int Dstate = LOW;
int Estate = LOW;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
digitalWrite(A,HIGH);
myStepper.setSpeed(60);
}

void loop() {
AState = digitalRead(A);
if (AState == HIGH) {
myStepper.step(stepsPerRevolution);}
else
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
}

by the way, im using l298n, arduino uno, and bipolar stepper

the step() command moves that many steps before returning. You are currently asking it to do an entire rotation before moving on. look at OneStepAtATime example. Take a step, check your pin, repeat.

myStepper.step(stepsPerRevolution);

That blocks all action till it completes. If you want the code to be responsive, move one step, check for input, move one step, check for input, ... It may seem cumbersome, but repetitive stuff is what computers do the best.

When posting code please use code tags.

Your primary issue is that the step function in the Stepper library is blocking, in other words it will complete moving the stepper motor before moving on. You cannot stop the movement once it starts.

I also note a couple of other suspicious things about you code:

  1. You are performing a digitalWrite(A,HIGH) in your setup code. This enables the pullup resistor for the input. If this is what you intended I would recommend using pinMode(A, INPUT_PULLUP) instead as it makes it clear what your intentions are.

  2. In the following code section I assume you want to set all of those pins to low when pin A is LOW:

if (AState == HIGH) {
myStepper.step(stepsPerRevolution);}
else
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);

If that is a correct assumption then you need to write the code this way:

if (AState == HIGH) {
  myStepper.step(stepsPerRevolution);
  }
else {
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(E,LOW);
}

However, those pins are under control of the Stepper library and you shouldn't change them.

groundFungus:

myStepper.step(stepsPerRevolution);

That blocks all action till it completes. If you want the code to be responsive, move one step, check for input, move one step, check for input, ... It may seem cumbersome, but repetitive stuff is what computers do the best.

groundFungus indicates the simplest way to fix your issue and is probably sufficient for your application. However, if you want more flexibility than that you can use the AccelStepper library.

It is pretty much the same with the AccelStepper library except that you call the run() function and check the input every time through loop(). The run() function will take one step at a time when it is time.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.