Hi all,
I have been having this problem using the stepper.h library with a limit switch.
The prepose of this code:
Stepper motor is a linear actuator that drive a load forward and back per cycle. The limit switch is installed so that when the load returns, it hit the limit switch, which will trigger another cycle and so on.
My problem:
The limit switch only stop my motor from running but refuse to move forward after hitting the switch.
Some of my own troubleshoot:
- I tried using AccelStepper library with ezButton and that worked just fine, but for this
project i really need to use stepper.h instead. - It seems like sometime it does move again after a very long pause and execute the
commands. - I wrote Serial.print('Turn') in the serial monitor and it only display 'Tu' when I press the
button and it display 'TuTu' second time I try.
Here is my code:
int speed = 0;
int position = 0;
int speedAbs = 0;
int positionAbs = 0;
#include <Stepper.h>
bool interrupt = false;
const byte interruptPin = 21;
const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 22, 24);
void setup() {
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), returnCycle, FALLING);
speed = 30;
position = 25.4;
positionAbs = position*(1600/25.4);
}
void loop() {
myStepper.setSpeed(speed);
myStepper.step(-1000);
myStepper.step(10000);
}
void returnCycle() {
myStepper.setSpeed(speed);
myStepper.step(-1000);
myStepper.step(10000);
}
Thanks in advance