Help With AccelStepper

Im trying to make a stepper move X number of steps when a button is activated and then wait for the button to be pressed to move another 16 steps, I already have the homing secuence in void setup ().

Hi,

Show us your complete sketch between code tags (the </> button). Tell us what it does and what it should do.

Sorry, I forgot to put the code…

Here it is, after the homing process, the steppers starts moving and stops when the button is pressed. I need to move the stepper X number of steps when the button is pressed and then stop to wait the next button press.

#include <AccelStepper.h>

AccelStepper stepper (1, 9 ,8);
#define home_switch 4
#define buttonState 7
long initial_homing=-1;
long stepstogo=16;

void setup()
{
pinMode(home_switch, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
delay(5);

stepper.setMaxSpeed(100);
stepper.setAcceleration(100);

while (digitalRead(home_switch)) {
stepper.moveTo(initial_homing);
initial_homing–;
stepper.run();
delay(5);
}

stepper.setCurrentPosition(0);

stepper.setMaxSpeed(500000);
stepper.setAcceleration(5000);
stepper.setSpeed(2800);
}

void loop()
{
if (digitalRead(buttonState)){
stepper.move(16);
}
stepper.run();

}

stepper.setMaxSpeed(500000);
stepper.setAcceleration(5000);
stepper.setSpeed(2800);

Are those reasonable values for max speed, acceleration and speed?

Have you tried to use lower values?

marioacosta556:
I need to move the stepper X number of steps when the button is pressed and then stop to wait the next button press.

What happens when you try the code you posted?

You seem to have the motor working with

stepper.setMaxSpeed(100);
stepper.setAcceleration(100);

so why are you changing to ridiculous numbers with these lines

  stepper.setMaxSpeed(500000);
   stepper.setAcceleration(5000);   
   stepper.setSpeed(2800);

...R

The speed of movement is not a problem, those are just random numbers that I put there.

Yes, I have tried with lower values.

I need to move the stepper an specific number of steps with a button press and then wait until the next button press to move again.

Have you tried this:

if (digitalRead(buttonState)){
    stepper.move(16);
    stepper.run();
}

Jacques

Yes, I already tried that, the problem is that as soon as the homing process is completed, the stepper begins to move without pressing the start button.

the problem is that as soon as the homing process is completed, the stepper begins to move without pressing the start button.

This

void loop()
{
  if (digitalRead(buttonState)){
    stepper.move(16);
  }
  stepper.run();

}

Will certainly do that

jbellavance:
Will certainly do that

Well spotted.

@marioacosta556, because you are using INPUT_PULLUP you should have

if (digitalRead(buttonState) == LOW){

...R

because you are using INPUT_PULLUP

Well spotted also :wink:

Jacques

You both, thank you very much! That solved the problem.