Button doesn't reset when using AccelStepper.h

This works fine, but only ‘once’ at the press of the button. I need it to re-play when the button is pressed again.

I've done some simple tests using an led without the stepper library and it works fine, ie. the button resets and works over and over.

Is there something with AccelStepper that is conflicting and needs some sort of initialization?

Arduino Code:

#include <AccelStepper.h>
AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9);
const int switchPin = 2;
int buttonState = LOW;
void setup()
 
{  
stepper2.setMaxSpeed(700.0);
stepper2.setAcceleration(430.0);
}
 
void loop()
 
{   
buttonState = digitalRead(switchPin); 
if (buttonState == HIGH)  
stepper2.moveTo(2000);
stepper2.run(); 
}

Cheers.

What is the state of pin 2 if the button is not pressed?

It's tied to ground with a resistor, therefore LOW.

Try this:

#include <AccelStepper.h>
AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9);
const int switchPin = 2;
int buttonState = LOW;
void setup()
 
{  
stepper2.setMaxSpeed(700.0);
stepper2.setAcceleration(430.0);
}
 
void loop()
 
{   
buttonState = digitalRead(switchPin); 
if (buttonState == HIGH)
{  //<-----  
stepper2.moveTo(2000);
stepper2.run(); 
}
}

Thanks very much, I'll give it a try tomorrow as it's getting late here.

Cheers.

The problem is you only ever ask the stepper to move to 2000, so it moves there and stays there.

Perhaps you meant move()? This is a relative move, whereas moveTo() is absolute (see AccelStepper.h)

jcallen: The original code structure is correct, run() needs to be called every time round the loop -
please don't answer questions wrongly, it doesn't help anyone. If you are not sure, look it up or
let someone else answer.

@MarkT

Does a "if" with multiple statements not need "{" and "}" around those statements?
Also, don't you need a final "}" at end of loop()?

BTW, I'm not familiar with AccelStepper library.
I was pointing out several curly bracket errors in the ops original code.

#include <AccelStepper.h>
AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9);
const int switchPin = 2;
int buttonState = LOW;
void setup()
 
{ 
stepper2.setMaxSpeed(700.0);
stepper2.setAcceleration(430.0);
}
 
void loop()
 
{   
buttonState = digitalRead(switchPin);
if (buttonState == HIGH) 
stepper2.moveTo(2000);
stepper2.run();
}

If my efforts to help newbies is not needed or appreciated, I will gladly cease and desist.

MarkT:
The problem is you only ever ask the stepper to move to 2000, so it moves there and stays there.

Perhaps you meant move()? This is a relative move, whereas moveTo() is absolute (see AccelStepper.h)

jcallen: The original code structure is correct, run() needs to be called every time round the loop -
please don't answer questions wrongly, it doesn't help anyone. If you are not sure, look it up or
let someone else answer.

Thanks Mark. The code is for a project my brother is involved in. He was a member on this forum but couldn't remember his login details. Therefore I posted and asked the question.

It now works as it should. As you pointed out it should have been move() not moveTo().

Many thanks.