#include <AccelStepper.h>

#define STEP_PIN_1    D1
#define DIR_PIN_1     D0
#define ENABLE_PIN_1  D3
#define STEP_PIN_2    D8
#define DIR_PIN_2     D7
#define ENABLE_PIN_2  D6

#define FULL_STEPS_PER_REV 200
#define MICROSTEP          1

const long TEST_STEPS = FULL_STEPS_PER_REV * MICROSTEP;
const float MAX_SPEED  = 1000.0;
const float ACCEL      = 2000.0;
const float CONT_SPEED = 750.0;

AccelStepper stepper1(AccelStepper::DRIVER, STEP_PIN_1, DIR_PIN_1);
AccelStepper stepper2(AccelStepper::DRIVER, STEP_PIN_2, DIR_PIN_2);

bool continuousMode = false;

void enableDrivers(bool enable) {
  digitalWrite(ENABLE_PIN_1, enable ? LOW : HIGH);
  digitalWrite(ENABLE_PIN_2, enable ? LOW : HIGH);
}

void setup() {
  Serial.begin(115200);
  pinMode(ENABLE_PIN_1, OUTPUT);
  pinMode(ENABLE_PIN_2, OUTPUT);
  enableDrivers(true);
  stepper1.setMaxSpeed(MAX_SPEED);
  stepper1.setAcceleration(ACCEL);
  stepper2.setMaxSpeed(MAX_SPEED);
  stepper2.setAcceleration(ACCEL);
}

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();
    switch (cmd) {
      case '1': continuousMode = false; stepper1.moveTo(stepper1.currentPosition() + TEST_STEPS); stepper2.moveTo(stepper2.currentPosition() + TEST_STEPS); break;
      case '0': continuousMode = false; stepper1.stop(); stepper2.stop(); break;
    }
  }
  if (continuousMode) { stepper1.runSpeed(); stepper2.runSpeed(); }
  else { stepper1.run(); stepper2.run(); }
}