Sketch help please

Hi,
I am trying to program an Arduino Uno to control a stepper motor.
I need 3 buttons.

  1. Press it to turn the motor 1 step clockwise.
  2. Press it to turn the motor 1 step anticlockwise.
  3. Press it to turn the motor a set number of steps. This should only happen once per press of the button.

I have 2 sketches. One doesn’t have any code for the first 2 buttons and works as expected. It indexes as it should do.
The other sketch has extra lines of code for the jog buttons but they don’t work. Also, with this sketch, if the index button is pressed, the motor turns 1 step and then the other 400. If either of the jog buttons is pressed, nothing happens.

I used to have something very similar to this working years ago using the Button.h library but it doesn’t work anymore. That’s why I am now trying to get it working using the Pushbutton.h library instead.

Can anyone please offer advice to get the jog buttons working?
Thanks in advance.

You seem to have posted pictures of your programs. Please just post the code as text. See General Guidance and How to use the forum

...R

Also please post a wiring diagram. I could be a wiring issue.

Hi,
I managed to get the sketch working. Sort of.
The index button works as it should do but the jog buttons do not always respond. I have to keep pressing them until the motor decides to move, whilst annoying that they don’t work properly, I can work with it.
Can anybody offer any suggestions how to get the jog buttons to perform as they should?
Sketch is below.

#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
}

Thanks in advance.
Mark.

See how a little whitespace makes the program much easier to read

#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
}

I suspect the reason why your JOG buttons don’t respond is because they have to wait until after the motor has moved its full 400 steps from the first IF statement. What happens if you comment out the line myStepper.step(-400);

I am not familiar with the Pushbutton library. Maybe a simple digitalRead() for the jog button would be better with code that says

if (jogCW == LOW) { // assumes LOW when pressed
   myStepper.step(-1);
}

…R