Go Down

Topic: simple stepper task (Read 737 times) previous topic - next topic

markanthkelli

//Hi, i'm new to all this, the code here does output stepper pulses while I keep pin2 pressed , but I wish it to run to completion after just a short press.
Any further pin2 presses must be ignored until stepperun has finished.
This simple code doesn't allow a repeat event if pin2 is pressed again later either.

Please help.
Thanks
Mark K









#include <AccelStepper.h>

AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9);

void setup()


  pinMode(2, INPUT_PULLUP);
 
  stepper2.setMaxSpeed(120.0);
  stepper2.setAcceleration(200.0);
  stepper2.moveTo(400);
}

void loop()

{
  int sensorVal = digitalRead(2);
 
  {
  if (sensorVal == LOW)
  stepper2.run();
  }

}

PaulS

Quote
but I wish it to run to completion after just a short press.

You need to look at the state change detection example. Learn to determine when a switch BECOMES pressed, not IS pressed.

Quote
This simple code doesn't allow a repeat event if pin2 is pressed again later either.

Where do you define where the stepper is to go to? In setup(). Unless you define a new position, once it steps 400 times, it's done.

I don't really see a reason for you to be using AccelStepper, Stepper would do what you want, in a much easier to understand fashion. The drawback is that it is blocking, but that seems to be what you want, so it isn't really a drawback.
The art of getting good answers lies in asking good questions.

Go Up