#include <AccelStepper.h>     // Library for controlling stepper motors
  #include <Servo.h>            // Library for controlling servo motors
  
  // Attribute for interrupt service routine on ESP32
  #if defined(ESP32)
    #define ISR_ATTR IRAM_ATTR
  #else
    #define ISR_ATTR
  #endif
  
  // ==== Pin definitions ====
  #define X_STEP_PIN D6         // Step pin for X-axis stepper
  #define X_DIR_PIN  D7         // Direction pin for X-axis stepper
  #define Y_STEP_PIN D8         // Step pin for Y-axis stepper
  #define Y_DIR_PIN  D9         // Direction pin for Y-axis stepper
  #define SERVO_PIN  D10        // PWM pin for servo motor (Z-axis)
  #define ENABLE_PIN D0         // Motor driver enable pin
  #define ENDSTOP_X_PIN D2      // Limit switch pin for X-axis
  #define ENDSTOP_Y_PIN D3      // Limit switch pin for Y-axis
  
  // Create stepper motor instances
  AccelStepper stepperX(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN);
  AccelStepper stepperY(AccelStepper::DRIVER, Y_STEP_PIN, Y_DIR_PIN);
  Servo zServo;  // Create servo motor object for Z-axis
  
  // ==== Position tracking variables ====
  long currentX = 0;  // Current X-axis position
  long currentY = 0;  // Current Y-axis position
  
  // ==== Command buffer ====
  const int BUFFER_SIZE = 20;        // Max number of buffered positions
  long bufferX[BUFFER_SIZE];         // Buffer to store X positions
  long bufferY[BUFFER_SIZE];         // Buffer to store Y positions
  int bufferCount = 0;               // Number of items in the buffer
  
  // ==== Movement parameters ====
  const float speed = 350;           // Base movement speed (steps/sec)
  float homingSpeed = 100;           // Speed for homing sequence
  int homingDirX = 1;                // Homing direction for X-axis
  int homingDirY = 1;                // Homing direction for Y-axis
  int retractSteps = 1750;           // Steps to retract after hitting limit switch
  
  // ==== State flags ====
  bool isExecuting = false;
  bool waitingServo = false;
  bool servoUp = true;               // True if the servo is in the 'up' position
  bool motorsEnabled = false;        // True if motors are energized
  bool homingDone = false;           // True if homing has been completed
  
  String serialLine = "";            // Buffer to store incoming serial data
  
  // ==== Limit switch flags ====
  volatile bool endstopX_triggered = false;
  volatile bool endstopY_triggered = false;
  
  // ==== Interrupt Service Routines (ISRs) for limit switches ====
  void ISR_ATTR endstopX_ISR() {
    endstopX_triggered = true;       // Mark that X limit switch was triggered
  }
  
  void ISR_ATTR endstopY_ISR() {
    endstopY_triggered = true;       // Mark that Y limit switch was triggered
  }
  
  // ==== Enable or disable stepper motors ====
  void enableMotors(bool state) {
    digitalWrite(ENABLE_PIN, state ? HIGH : LOW);  // HIGH to enable, LOW to disable
  }