/*
  Basic Stepper Test
  Written for DRV8825, but works with A4988
  Arduino | coding-help
  My stepper motor is rotating only in one direction. Need Coding Help
  Charlie October 18, 2025 — 1:20 AM
*/
#include <AccelStepper.h>
#define STEP_PIN 9
#define DIR_PIN 8
#define ENABLE_PIN 11
#define FAULT_PIN 2
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
long moveSteps         = 1600;
float maxSpeed         = 400.0;
float acceleration     = 200.0;
bool clockwise         = true;
void setup() {
  pinMode(ENABLE_PIN, OUTPUT);
  digitalWrite(ENABLE_PIN, LOW); // Enable driver
  pinMode(FAULT_PIN, INPUT_PULLUP);
  stepper.setMaxSpeed(maxSpeed);
  stepper.setAcceleration(acceleration);
  Serial.begin(9600);
  Serial.println("Stepper control started");
  Serial.print("Initial fault state: ");
  Serial.println(digitalRead(FAULT_PIN));
}
void loop() {
  stepper.setCurrentPosition(0);
  long target = clockwise ? moveSteps : -moveSteps;
  stepper.moveTo(target);
  Serial.print("Now rotating ");
  Serial.println(clockwise ? "CW" : "CCW");
  while (stepper.distanceToGo() != 0) {
    stepper.run();
    if (digitalRead(FAULT_PIN) == LOW) {
      Serial.println("Fault detected!");
      break;
    }
  }
  delay(1000);
  clockwise = !clockwise;
}