Hi i am working on a project where i need to stop stepper at home, for this i decided to add a stop switch to stop stepper when it presses, i tried but my switch is not working , please help i am posting my code below
#include <AFMotor.h>
#include <Wire.h>
#define home_switch 50
AF_Stepper motor(48, 1);
void setup() {
// Start Homing procedure of Stepper Motor at startup
while (!digitalRead(home_switch)) { // Do this until the switch is activated
motor.setSpeed(10); // 10 rpm
Serial.println("Interleave coil steps");
motor.step(2400, FORWARD, INTERLEAVE);
delay( 2000) ;
}
while (digitalRead(home_switch)) { // Do this until the switch is not activated
motor.release();
}
motor.step(0, FORWARD, INTERLEAVE); // Reset position variable to zero
}
void loop() {
}
By the reference from many examples i have written above code, please help or tell me where is the issue. my stepper is running but when i press button its not stopping the stepper.
My guess is that the line motor.step(2400, FORWARD, INTERLEAVE); will complete the full movement before it passes on to the the next line which is delay(2000); and which will also block the Arduino for a further 2 seconds before the button is tested.
You probably need to move the stepper one step at a time and check the switch between steps.
The AccelStepper library will probably be more suitable as it has non-blocking motor moves.