/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-stepper-motor-and-limit-switch
*/
#include <ezButton.h>
#include <AccelStepper.h>
#define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type)
ezButton limitSwitch(A0); // create ezButton object that attach to pin A0;
ezButton limitSwitch1(A1); // create ezButton object that attach to pin A1;
AccelStepper stepper(1, 2, 3);
bool stopForward = false;
bool stopBackward=false;
void setup() {
Serial.begin(9600);
limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch1.setDebounceTime(10); // set debounce time to 50 milliseconds
stepper.setMaxSpeed(500.0); // set the maximum speed
stepper.setAcceleration(50.0); // set acceleration
stepper.setSpeed(100); // set initial speed
stepper.setCurrentPosition(0); // set position
stepper.moveTo(MAX_POSITION);
}
void loop() {
limitSwitch.loop(); // MUST call the loop() function first
limitSwitch1.loop(); // MUST call the loop() function first
if (limitSwitch.isPressed()) {
Serial.println(F("The limit switch: TOUCHED"));
stopForward = true;
} else if (limitSwitch1.isPressed()) {
Serial.println(F("The limit switch: TOUCHED AGAIN"));
stopBackward = true;
}
if (stopForward == false) {
// without this part, the move will stop after reaching maximum position
if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position
stepper.setCurrentPosition(0); // reset position to 0
stepper.moveTo(MAX_POSITION); // move the motor to maximum position again
} else
{
Serial.println(F("The stepper motor is STOPPED AGAIN"));
}
if (stopBackward == false) {
// without this part, the move will stop after reaching maximum position
if (stepper.distanceToGo() != 0) { // if motor moved to the maximum position
stepper.setCurrentPosition(0); // reset position to 0
stepper.moveTo(-MAX_POSITION); // move the motor to maximum position again
//stepper.run();
}
stepper.run(); // MUST be called in loop() function
}
else
{
Serial.println(F("The stepper motor is STOPPED AGAIN"));
}
}
}