#include <AccelStepper.h>
#include <Servo.h>
#include <Toggle.h>
const byte limitSwitchPin = A3;
Toggle limitSwitch;
AccelStepper stepper(AccelStepper::DRIVER, 12, 13);
void homing () {
Serial.println("Homing - press the button to simulate limitswitch hit");
stepper.setSpeed(200);
while (true) { // run CW at constant speed until we hit the button
stepper.runSpeed();
limitSwitch.poll();
if (limitSwitch.onPress()) break;
}
Serial.println("Limit switch touched. backing up until release");
stepper.setSpeed(-100);
while (limitSwitch.isPressed()) { // run CCW at constant speed until we leave the button
stepper.runSpeed();
limitSwitch.poll();
}
Serial.println("Limit switch free. Setting position to 0");
stepper.setCurrentPosition(0);
}
void setup() {
Serial.begin(115200);
limitSwitch.begin(limitSwitchPin);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(200);
homing();
}
void loop() {}