#include <AccelStepper.h>
// Define pin connections
const int dirPin = 2;
const int stepPin = 3;
// Define motor interface type
#define motorInterfaceType 1
#define btnV 4
#define btnR 5
#define btnA 6
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
// Variable to track the pause state
bool isPaused = false;
void setup() {
// Configure pins for buttons
pinMode(btnV, INPUT_PULLUP);
pinMode(btnR, INPUT_PULLUP);
pinMode(btnA, INPUT_PULLUP);
// Set motor parameters
myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(50);
myStepper.setSpeed(0); // Start with no movement
}
void loop() {
// Check if pause button is pressed
if (digitalRead(btnR) == LOW) {
isPaused = true;
myStepper.setSpeed(0); // Stop the motor
}
// If the motor is paused, check for directional commands
if (isPaused) {
if (digitalRead(btnV) == LOW) { // Move to the left
isPaused = false;
myStepper.setSpeed(-200); // Negative speed for left
} else if (digitalRead(btnA) == LOW) { // Move to the right
isPaused = false;
myStepper.setSpeed(200); // Positive speed for right
}
}
// Run the motor
myStepper.runSpeed();
}