What do i need to change to get the 5 second wait to restart after button 6 goes back high and do nothing is button 5 is pressed?
Im not very good at coding and have just been copy and pasting might be why its not working ?
any help would be great thanx
#include <AccelStepper.h>
// DRV8825 control pins
#define STEP_PIN 8
#define DIR_PIN 9
// Create stepper object (DRIVER = 1 for step/dir driver)
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// Button pins
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;
const int BUTTON5 = 6;
const int BUTTON6 = 7;
// Speeds in steps per second
const float FAST_SPEED = 1000.0;
const float MEDIUM_SPEED = 500.0;
const float CREEP_SPEED = 100.0;
// State machine
enum State {
IDLE,
CW_FAST,
CW_CREEP,
WAITING,
CCW_MEDIUM,
CCW_CREEP
};
State currentState = IDLE;
unsigned long waitStart = 0;
bool button1Handled = false; // track fresh press for Button 1
void setup() {
Serial.begin(9600);
// Initialize stepper
stepper.setMaxSpeed(2000); // Adjust max speed as needed
stepper.setAcceleration(1000); // Optional for smoother starts/stops
// Button inputs
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(BUTTON3, INPUT_PULLUP);
pinMode(BUTTON4, INPUT_PULLUP);
pinMode(BUTTON5, INPUT_PULLUP);
pinMode(BUTTON6, INPUT_PULLUP);
Serial.println("Stepper Ready.");
}
void loop() {
// Allow stepper to move
stepper.runSpeed();
// Handle Button 1 – Restart only on fresh press
if (digitalRead(BUTTON1) == LOW && !button1Handled) {
Serial.println("Button 1: Restart CW fast");
currentState = CW_FAST;
stepper.setSpeed(FAST_SPEED); // CW direction
button1Handled = true;
} else if (digitalRead(BUTTON1) == HIGH) {
button1Handled = false; // Reset flag when button is released
}
switch (currentState) {
case CW_FAST:
if (digitalRead(BUTTON2) == LOW) {
Serial.println("Button 2: Switch to CW creep");
currentState = CW_CREEP;
stepper.setSpeed(CREEP_SPEED); // Still CW
delay(200);
}
break;
case CW_CREEP:
if (digitalRead(BUTTON3) == LOW) {
Serial.println("Button 3: Stop, wait 5s, then CCW medium");
stepper.setSpeed(0); // Stop
currentState = WAITING;
waitStart = millis(); // Start 5-second timer
delay(200);
}
break;
case WAITING:
if (millis() - waitStart >= 5000) {
Serial.println("5 seconds done. Starting CCW medium");
currentState = CCW_MEDIUM;
stepper.setSpeed(-MEDIUM_SPEED); // CCW
}
break;
case CCW_MEDIUM:
if (digitalRead(BUTTON4) == LOW) {
Serial.println("Button 4: Switch to CCW creep");
currentState = CCW_CREEP;
stepper.setSpeed(-CREEP_SPEED);
delay(200);
}
if (digitalRead(BUTTON6) == LOW) {
Serial.println("Button 6: Interrupt and restart");
stepper.setSpeed(0);
currentState = IDLE;
delay(200);
}
break;
case CCW_CREEP:
if (digitalRead(BUTTON5) == LOW) {
Serial.println("Button 5: Stop CCW creep");
stepper.setSpeed(0);
currentState = IDLE;
delay(200);
}
if (digitalRead(BUTTON6) == LOW) {
Serial.println("Button 6: Interrupt and restart");
stepper.setSpeed(0);
currentState = IDLE;
delay(200);
}
break;
case IDLE:
default:
// Do nothing
break;
}
}
