Hello everyone, I'm trying to get a stepper motor to spin until the moment the limit switch is pressed.
The goal is that once the limit switch is pressed the engine will not return to work.
Thanks to everyone for the help
#include <Stepper.h>
const int limit_s_pin = 7;
const int stepsPerRevolution = 200;
int i = 1 ;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 9, 10, 11);
void setup()
{
pinMode(limit_s_pin,INPUT);
myStepper.setSpeed(60);
}
void loop() {
while(digitalRead(limit_s_pin)== HIGH && i == 1)
{
myStepper.step(3);
}
i = 2;
}
switches are typically connected between a pin and ground, the pin configured as INPUT_PULLUP so that it is pulled HIGH and when the switch is active, it pulls the pin LOW
why not simply
void loop() {
if (HIGH == digitalRead(limit_s_pin))
myStepper.step(3);
}
but since the starting position of a stepper motor is typically unknown at startup, why not use the switch as a known calibration point at one extreme and rely on the # of steps to keep the motor within limits.
positioning the limit switch at one extreme allows the motor to always be moved in one direction at startup to locate calibrate
Then you have to use INPUT_PULLUP or use an external pull up resistor. As you have been told. Wiring to the NC contact on the switch will make the input HIGH when the limit is exceeded. If you want to run it just once, put it in setup():
Because he said, "and not run again". If it's a homing setup, that's a good idea. Subsequent code should run in the loop() function without the homing happening every time the limit switch is inactive. I'm trying to anticipate an actual use case where the stepper performs some kind of work besides just homing. Also demonstrate the basic framework of "run once in loop()".
To really drill it in, I should have called the variable "homing" instead of "running".
i try to answer the question directly. i think it's less confusing to anticipate fewer issues without explanation
i by "homing" you mean a calibration point as i suggested, wouldn't that be a process in setup() that occurs once and resets a step-count variable? for such a thing, why would there need to be a "run" variable?
My thought, you might actually wish to home the device without power cycling the system. Perhaps the question would not have arisen if the OP had presented an overview of the project right in the first post, revealing the desired operational states.
We don't really know if this is a real project, or today's lab handout or homework.