Control stepper motor with single button

I’m trying control a stepper with a single button.
I would like it to move x amount of steps when the button is pressed and move x amount of steps the opposite direction once the button is pressed again.

Can anyone help with some tips on where to start?

Start with the IDE example of how to read a switch and detect it's change from off to on and back to off.
Paul

Here is a demo code that I wrote a while back. Push the button once it will move clockwise, next press anti clock wise and so on. Speed adjustable by potentiometer. Uses the AccelStepper library and the state change detection method (see also my active low state change detection).

// rotate stepper 1 turn with push of button at speed set by potentiometer.
// by groundFungud AKA charlie goulding.

// change step, dir, button and pot pins to match your set up
// tested with an Uno with CNC shield V3,
// hence the step, dir and enable pin numbers
// stepper set to 4X microstepping
// pushbutton toggles stepper move direction

#include <AccelStepper.h>

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;  // stepper enable for CNC shield
const byte buttonPin = 9;  // wired to ground and pin 9
const byte potPin = A0;

bool moveDir = false;  // stepper direction flag
long moveSteps = 800;  // number of steps to move per button press

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

bool lastButtonState = HIGH; // button history

void setup()
{
   Serial.begin(115200);
   pinMode(enablePin, OUTPUT);
   pinMode(buttonPin, INPUT_PULLUP);
   digitalWrite(enablePin, LOW);  // enable steppers
   // Change these to suit your stepper if you want
   stepper.setMaxSpeed(100); // initial speed only
   stepper.setAcceleration(500);
}

void loop()
{
   int potValue = analogRead(potPin); // read pot   
   potValue = map(potValue, 0, 1023, 50, 4000);
   stepper.setMaxSpeed(potValue); // change the speed by pot setting.
   stepper.setAcceleration(potValue / 2);
   
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      bool buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW) // button went from not pressed to prssed
         {
            Serial.println(potValue);
            moveDir = !moveDir; // toggle direction
            if (moveDir == true)
            {
               stepper.move(moveSteps);  // move 800 steps, RELATIVE to current position
            }
            else
            {
               stepper.move(-moveSteps);
            }
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
   stepper.run();  // must be called very often
                   // ideally once every loop iteration
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.