Programming help please

I am trying to control a stepper motor using Arduino and the Pushbutton library but am struggling with the sketch.
What I want, is to press one button and the motor turn cw until the button is released, or press another button and the motor turn ccw.
I also need a third button, this makes the motor turn a set number of steps but, only once for each press. If the button is held down the motor shouldn’t turn.
I have attached 2 pictures of my sketch.
One has no code for the jog buttons and works as expected with just the index button. Press the button and the motor turns the set number of steps.
The other has extra lines of code for the other 2 buttons but they don’t work and I don’t know why.
If I press the index button, the motor turns 1 step and then another 400.
If I press either of the other buttons, nothing happens.
Can anyone please help me with this sketch?
Thanks in advance.


When you run your CRC-machine-NEW3 sketch, did you press the index button first before pressing the other buttons? The sketch starts off with waiting for the index button to be pressed and released before checking the states of your other buttons, which probably happens fast before starting at the top of the loop() function again. If you do not already know, the waitForPress() method in the Pushbutton library for Arduino is a blocking function that continually loops until the button is pressed. You should probably use getSingleDebouncedPress() instead, which is more appropriate for what you’re trying to do and is a non-blocking function.

- Amanda

Hi Amanda,
Thank you for the pointer.
The sketch is now working. Sort of.
The index button works perfectly.
The jog buttons don’t always respond and I have to keep pressing them until the motor moves. This is not ideal but I can work with it.
Below is the latest incarnation of the sketch.
In the original sketch I was using ‘isPressed’ for the jog buttons but if either was pressed the motor would just move randomly in any direction it felt like going. I am now using ‘getSingleDebouncedPress’ and they sort of work as explained above.
Any suggestions to get the jog buttons working correctly would be most appreciated.

#include <Pushbutton.h>
#include <Stepper.h>
const int stepsPerRevolution = 400; //set number of steps for motor
Pushbutton buttonIndex (2); //set cycle start button input on pin 2
Pushbutton buttonJogcw (4); //set jog clockwise button on pin 4
Pushbutton buttonJogccw (7); //set jog anticlockwise button on pin 7
Stepper myStepper(stepsPerRevolution,8,9,10,11); //set stepper motor on pins 8,9,10,11
void setup(){
  myStepper.setSpeed(200); //set motor speed
void loop(){
if(buttonIndex.getSingleDebouncedPress()){myStepper.step(-400);}//set number of steps to move
if(buttonJogcw.getSingleDebouncedPress()){myStepper.step(-1);} //set to jog 1 step clockwise

if(buttonJogccw.getSingleDebouncedPress()){myStepper.step(1);} //set to jog 1 step anticlockwise

Many thanks.

The original code you had for checking buttonJogcw and buttonJogccw was fine. You should go back to using your old code for buttonJogcw and buttonJogccw.

- Amanda