const int enPin = 8;
const int stepXPin = 2; //X.STEP
const int dirXPin = 5; // X.DIR
const int stepYPin = 3; //Y.STEP
const int dirYPin = 6; // Y.DIR
const int stepZPin = 4; //Z.STEP
const int dirZPin = 7; // Z.DIR

// Added pins for stop control
const int stopControlOutPin = 12; // Output pin
const int stopControlInPin = 13; // Input pin
const int testControlInPin = A0; // Input pin
const int testControlInPin2 = A1; // Input pin

bool currentDirection = HIGH; // Start with clockwise
bool previousDirection = !currentDirection; // Initialize to the opposite of currentDirection

int pulseWidthMicros = 50; // microseconds
int millisBtwnSteps = 2000; // Time between steps in microseconds

unsigned long lastStopDetectionTime = 0; // Tracks the last time a stop was detected
const unsigned long debounceDelay = 1000; // 1 second delay to debounce the stop detection

void setup() {
    Serial.begin(9600);
    pinMode(enPin, OUTPUT);
    digitalWrite(enPin, LOW);
    pinMode(stepYPin, OUTPUT);
    pinMode(dirYPin, OUTPUT);
    pinMode(stepZPin, OUTPUT);
    pinMode(dirZPin, OUTPUT);

    // Setup stop control pins
    pinMode(stopControlOutPin, OUTPUT);
    pinMode(stopControlInPin, INPUT_PULLUP); // Use internal pull-up resistor
    pinMode(testControlInPin, INPUT_PULLUP); // Use internal pull-up resistor
    pinMode(testControlInPin2, INPUT_PULLUP); // Use internal pull-up resistor

    Serial.println(F("CNC Shield Initialized"));
}

void loop() {
    if (currentDirection != previousDirection) {
        // Only update direction and log if there has been a change
        digitalWrite(dirYPin, currentDirection); // Set the current direction
        Serial.println(currentDirection == HIGH ? F("Running clockwise") : F("Running counter-clockwise"));
        previousDirection = currentDirection; // Update previous direction to current
    }

    unsigned long currentTime = millis();
    
    // Check if stop condition is met
    if (digitalRead(stopControlInPin) == LOW && (currentTime - lastStopDetectionTime > debounceDelay)) {
        Serial.println(F("Stop detected, changing direction..."));
        currentDirection = !currentDirection; // Change direction
        lastStopDetectionTime = currentTime; // Update the last stop detection time
    }
    unsigned long currentTime2 = millis();

    if (digitalRead(testControlInPin) == LOW && (currentTime2 - lastStopDetectionTime > debounceDelay)) {
      Serial.println(F("Awe Bra..."));
      digitalWrite(dirZPin, HIGH); // Starting direction for Z
      for (int i = 0; i < 5; i++) {
        digitalWrite(stepZPin, HIGH);
        delayMicroseconds(pulseWidthMicros);
        digitalWrite(stepZPin, LOW);
        delayMicroseconds(millisBtwnSteps); // Adjust for pulse width time
      }
      lastStopDetectionTime = currentTime2; // Update the last stop detection time
    }

    const int stepsPerRev = 200;
    int pulseWidthMicros = 50; // microseconds
    int millisBtwnSteps = 4000; // Time between steps in microseconds
    unsigned long currentTime3 = millis();

    if (digitalRead(testControlInPin2) == LOW && (currentTime3 - lastStopDetectionTime > debounceDelay)) {
      digitalWrite(dirZPin, LOW);
      Serial.println(F("Going Home!"));
      for (int j = 0; j < stepsPerRev * 2; j++) {
        digitalWrite(stepZPin, HIGH);
        delayMicroseconds(pulseWidthMicros);
        digitalWrite(stepZPin, LOW);
        delayMicroseconds(millisBtwnSteps); // Adjust for pulse width time
      }
      lastStopDetectionTime = currentTime2; // Update the last stop detection time
    }
    int stepPerRev = 500;
    // Send step instruction Y
    for(int i = 0; i < stepPerRev; i++) {
      digitalWrite(stepYPin, HIGH);
      delayMicroseconds(pulseWidthMicros);
      digitalWrite(stepYPin, LOW);
      delayMicroseconds(millisBtwnSteps); // Adjust for pulse width time
    }
    currentDirection = !currentDirection;
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
stepper1:A-
stepper1:A+
stepper1:B+
stepper1:B-
A4988
drv1:ENABLE
drv1:MS1
drv1:MS2
drv1:MS3
drv1:RESET
drv1:SLEEP
drv1:STEP
drv1:DIR
drv1:GND.1
drv1:VDD
drv1:1B
drv1:1A
drv1:2A
drv1:2B
drv1:GND.2
drv1:VMOT
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
stepper2:A-
stepper2:A+
stepper2:B+
stepper2:B-
A4988
drv2:ENABLE
drv2:MS1
drv2:MS2
drv2:MS3
drv2:RESET
drv2:SLEEP
drv2:STEP
drv2:DIR
drv2:GND.1
drv2:VDD
drv2:1B
drv2:1A
drv2:2A
drv2:2B
drv2:GND.2
drv2:VMOT