Changing stepper speed with push buttons

Not positive, have no steppers here to try it with. But this may do what you are looking for...

#include <mechButton.h>
#include <idlers.h>
#include <AccelStepper.h>

#define BUTTON_PIN1  2     // Pin we'll hook a button to. The other side hooks to ground.
#define BUTTON_PIN2  3     // Pin we'll hook the other button to.
#define motorInterfaceType 1

const int stepPin = 9; // Set the stepping pin to pin 9
const int dirPin = 8; // Set the direct pin to pin 8
int currentSpeed = 10; // Create a variable for speed


mechButton button1(BUTTON_PIN1);  // Set button one to pin 2.
mechButton button2(BUTTON_PIN2);  // Set button two to pin 3.

AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

// Your standard sketch setup()
void setup() {
   
   button1.setCallback(myCallback1);    // Set up #1's callback. (Don't use Analog for buttons.)
   button2.setCallback(myCallback2);    // Set up #2's callback.
   myStepper.setMaxSpeed(1000);
   myStepper.setSpeed(currentSpeed);
}


// This is the guy that's called when the button1 changes state.
void myCallback1(void) {

   if (!button1.trueFalse()) {
      currentSpeed += 1;
      myStepper.setSpeed(currentSpeed);
   }
}


// This is the guy that's called when the button2 changes state.
void myCallback2(void) {

   if (!button2.trueFalse()) {
      currentSpeed -= 1;
      myStepper.setSpeed(currentSpeed);
   }
}


// Your standard sketch loop()
void loop() {
  
   idle();                 // Let all the idlers have time to do their thing.
   myStepper.runSpeed();   // Make the stepper go.
}

You'll need to grab LC_baseTools from the library manager to compile this.

Good luck!

-jim lee

1 Like