#include <AccelStepper.h>

// Pins for A4988 motor drivers
const int motor1StepPin = 2;
const int motor1DirPin = 3;
const int motor2StepPin = 4;
const int motor2DirPin = 5;
const int motor3StepPin = 6;
const int motor3DirPin = 7;

// Pin for push button
const int buttonPin = 8;

// Stepper motor objects
AccelStepper motor1(AccelStepper::DRIVER, motor1StepPin, motor1DirPin);
AccelStepper motor2(AccelStepper::DRIVER, motor2StepPin, motor2DirPin);
AccelStepper motor3(AccelStepper::DRIVER, motor3StepPin, motor3DirPin);

// Variables to track the number of times the button has been pressed
int pressCount = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);

  // Set up stepper motors
  motor1.setMaxSpeed(1000);
  motor1.setAcceleration(500);
  motor2.setMaxSpeed(1000);
  motor2.setAcceleration(500);
  motor3.setMaxSpeed(1000);
  motor3.setAcceleration(500);
}

void loop() {
  // Check if the button is pressed
  if (digitalRead(buttonPin) == LOW) {
    // Increment press count
    pressCount++;

    // Calculate the number of steps for each motor based on press count
    int steps1, steps2, steps3;
    switch (pressCount) {
      case 1:
        steps1 = 200;
        steps2 = 250;
        steps3 = 300;
        break;
      case 2:
        steps1 = 100;
        steps2 = 200;
        steps3 = 350;
        break;
      case 3:
        steps1 = 300;
        steps2 = 100;
        steps3 = 250;
        break;
      default:
        steps1 = 0;
        steps2 = 0;
        steps3 = 0;
        break;
    }

    // Move stepper motors to target positions
    motor1.move(steps1);
    while (motor1.distanceToGo() != 0) {
      motor1.run();
    }
    delay(1000);

    motor2.move(steps2);
    while (motor2.distanceToGo() != 0) {
      motor2.run();
    }
    delay(1000);

    motor3.move(steps3);
    while (motor3.distanceToGo() != 0) {
      motor3.run();
    }
    delay(1000);

    // Move stepper motors back to initial positions
    motor3.move(-steps3);
    while (motor3.distanceToGo() != 0) {
      motor3.run();
    }
    delay(1000);

    motor2.move(-steps2);
    while (motor2.distanceToGo() != 0) {
      motor2.run();
    }
    
    motor1.move(-steps1);
    while (motor1.distanceToGo() != 0) {
      motor1.run();
    }
    

 // Reset press count after 3 presses
    if (pressCount == 3) {
      pressCount = 0;
    }

    // Delay to avoid multiple detections from the same button press
    delay(1000);
  }
}
A4988
A4988
A4988