//rotate stepper motor... on button pressed
//precise number of steps
//as defined by potentiometer
//by elaine & rico fa2024 apr7

#include <AccelStepper.h>
#include <Servo.h>

// Stepper motor pins
#define dirPin 5
#define stepPin 2
#define swPin 4

int buttonState = 0;

Servo cutServo; //create instance object of servo class - called cutServo

// Define the stepper motor and the pins that are connected to
AccelStepper stepper1(1, stepPin, dirPin); // (Typeof driver: with 2 pins, STEP, DIR)
unsigned int stepStart = 0; //value will increase for every woire length cut
int stepAdd = 0; //adjust value to get desired wire length

void setup() {
  Serial.begin(9600);
  
  // Stepper Motor Parameter
  stepper1.setMaxSpeed(1000); // Set maximum speed value for the stepper
  stepper1.setAcceleration(500); // Set acceleration value for the stepper
  stepper1.setCurrentPosition(0); // Set the current position to 0 steps

  cutServo.attach(9);

  pinMode (swPin, INPUT);
}

void loop() {
  //Grab value from potentiometer
  stepAdd = 150; 
  //stepAdd = length*17; //length should be in cm... 1 cm = 17 steps
  Serial.println(stepAdd);

//check if button is pressed
  buttonState = digitalRead (swPin);

   // Move stepper motor the required number of steps in clock-wise direction when button pushed
    //for (int i=0; i==qty; i++) { //... comment out next line and use this line when quantity value is received

  if (buttonState == HIGH) {
    advanceWire();
  }
}

//steppermotor function
void advanceWire () {
    stepper1.moveTo(stepStart);
    stepper1.runToPosition ();
    stepStart = stepStart + stepAdd ;
    delay (1000);
    cutWire();
    delay(3000);
}

void cutWire () {
  cutServo.write(0);
  delay(500);
  cutServo.write(90);
  delay(500);
  cutServo.write(0);
}
A4988