#include <ezButton.h>
#include <AccelStepper.h>
int button1 = 7;
int button2 = 8;
int state1, state2;
ezButton limitSwitch1(button1); // create ezButton object that attach to pin 7;
ezButton limitSwitch2(button2); // create ezButton object that attach to pin 8;
AccelStepper stepperX(1, 3, 5); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 1,3,5
int stepperSpeed = 1000;
void setup() {
Serial.begin(9600);
stepperX.setMaxSpeed(stepperSpeed); // <<<<=== THIS makes all the difference
stepperX.setSpeed(0);
}
void loop() {
limitSwitch1.loop(); // MUST call the loop() function first
limitSwitch2.loop();
if (state2) {
stepperX.setSpeed(-stepperSpeed);
}
if (state1) {
stepperX.setSpeed(stepperSpeed);
}
if (limitSwitch1.isReleased()) {
Serial.println("The limit switch ONE: TOUCHED -> UNTOUCHED");
state1 = 1;
state2 = 0;
}
if (limitSwitch2.isReleased()) {
Serial.println("The limit switch TWO: TOUCHED -> UNTOUCHED");
state2 = 1;
state1 = 0;
}
//unconditional call that makes the stepper run all the time
stepperX.runSpeed();
//delay(10); no delay needed. AccelStepper does set the speed with .setSpeed()
}