// https://forum.arduino.cc/t/trying-to-use-a-limit-switch-to-change-the-direction-of-rotation-of-stepper-motor/1250150
#include <Servo.h>  //Servo library
// Pin Definitions
#define LIMIT_SWITCH_PIN 7
#define dirPin 2   // Direction
#define stepPin 3  // Step Pulse
#define enaPin 4   // Enable Motor
// Variables
int stepDelay = 1250;  // Delay between steps in microseconds
int stepsToMove = 800;
bool run = true;
//-------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
  // // Set pin modes
  pinMode(enaPin, OUTPUT);
  digitalWrite(enaPin, LOW);  // Enable motor
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
}
//-------------------------------------------------------------
void switchHit() {
  digitalWrite(dirPin, HIGH);  // Set direction to clockwise
  for (int i = 0; i < stepsToMove; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(20);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay - 20);
  }
  run = false;
}
//-------------------------------------------------------------
void loop() {
  if ( run == true) {
    digitalWrite(dirPin, LOW);  // Set direction to clockwise
    for (int i = 0; i < stepsToMove; i++) {
      if (digitalRead(LIMIT_SWITCH_PIN) == LOW) {
        Serial.println("Activated!");
        switchHit();
        break;
      }
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(20);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(stepDelay - 20);
    }
    delay(3000);  // Delay for 1 second
  }
}
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
stepper2:A-
stepper2:A+
stepper2:B+
stepper2:B-
A4988
drv3:ENABLE
drv3:MS1
drv3:MS2
drv3:MS3
drv3:RESET
drv3:SLEEP
drv3:STEP
drv3:DIR
drv3:GND.1
drv3:VDD
drv3:1B
drv3:1A
drv3:2A
drv3:2B
drv3:GND.2
drv3:VMOT
uno1:A5.2
uno1:A4.2
uno1:AREF
uno1:GND.1
uno1:13
uno1:12
uno1:11
uno1:10
uno1:9
uno1:8
uno1:7
uno1:6
uno1:5
uno1:4
uno1:3
uno1:2
uno1:1
uno1:0
uno1:IOREF
uno1:RESET
uno1:3.3V
uno1:5V
uno1:GND.2
uno1:GND.3
uno1:VIN
uno1:A0
uno1:A1
uno1:A2
uno1:A3
uno1:A4
uno1:A5