#include <AccelStepper.h>
#include <Toggle.h>

const byte dirPin             = 2;
const byte stepPin            = 3;

const byte cwButtonPin        = 4;
const byte ccwButtonPin       = 5;
const byte cwLimitSwitchPin   = 6;
const byte ccwLimitSwitchPin  = 7;
const byte stopButtonPin      = 8;

const byte solenoidPin        = 9;


const unsigned long cwTarget = 10000000;
const unsigned long ccwTarget = -10000000;

AccelStepper stepper = AccelStepper(AccelStepper::DRIVER, stepPin, dirPin);
Toggle cwButton, ccwButton, cwLimitSwitch, ccwLimitSwitch, stopButton;

enum MotorState {STOPPED, GOING_CW, GOING_CCW} motorState = STOPPED;

void activateSolenoids() {
  digitalWrite(solenoidPin, HIGH);
}

void deactivateSolenoids() {
  digitalWrite(solenoidPin, LOW);
}

void stop() {
  deactivateSolenoids();
  stepper.setCurrentPosition(0);
  stepper.moveTo(0);
  motorState = STOPPED;
}

void motorManagement() {

  cwButton.poll();
  ccwButton.poll();
  cwLimitSwitch.poll();
  ccwLimitSwitch.poll();
  stopButton.poll();

  switch (motorState) {
    case STOPPED:
      if (cwButton.onPress()) {
        stepper.moveTo(cwTarget);
        activateSolenoids();
        motorState = GOING_CW;
      } else if (ccwButton.onPress()) {
        stepper.moveTo(ccwTarget);
        activateSolenoids();
        motorState = GOING_CCW;
      }
      break;

    case GOING_CW:
      if (stopButton.onPress() || cwLimitSwitch.onPress()) stop();
      break;

    case GOING_CCW:
      if (stopButton.onPress() || ccwLimitSwitch.onPress()) stop();
      break;

  }

  stepper.run();
}


void setup() {
  pinMode(solenoidPin, OUTPUT);

  cwButton.begin(cwButtonPin);
  ccwButton.begin(ccwButtonPin);
  cwLimitSwitch.begin(cwLimitSwitchPin);
  ccwLimitSwitch.begin(ccwLimitSwitchPin);
  stopButton.begin(stopButtonPin);

  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(20);
  stop();
}

void loop() {
  motorManagement();
}
A4988
CW limit
CCW limit
CW Button
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
Solenoids
CCW Button
STOP !!