BrutusBert:
Hello,
I have a Nema23 stepper motor running with a tb6600. IT runs very good. I want to use the stepper motor like a tv lift. There s a leadscrew mounted. When I hit a switch the 'tv' goes up and stops at the top. The motor also stops. When I hit the switch in the other direction it goes down and stops at the bottom. My idea was that When it reaches the top or bottom a pull down takes place on a pin and the motor stops. When I hit the switch again, reverses the direction and stops at the other end, again with a pull down resistor. Does anyone has an idea on how to do this? Are the pull downs a bad idea?
It looks easy...but nog for me
This is a pretty common situation, and we deal with it with a state machine. At any time, your sketch can be in one of four states:
enum State { TOP, BOTTOM, MOVING_UP, MOVING_DOWN } state;
Inside your loop, you have a switch statement whose job it is to deal with what needs to be done when the sketch is in that state:
void loop() {
switch(state) {
case TOP:
if the 'down' switch has been pressed, {
start the moving down motor
state = MOVING_DOWN;
}
break;
case BOTTOM:
if the 'up' switch has been pressed, {
start the moving up motor
state = MOVING_UP;
}
break;
case MOVING_UP:
if the top bumper has been hit {
stop the moving up motor
state = TOP;
}
else {
continue moving up
}
break;
case MOVING_DOWN:
if the bottom bumper has been hit {
stop the moving down motor
state = BOTTOM;
}
else {
continue moving down
}
break;
}
}
As an aside, it helps if you use english language modal words (should, would, will, can, might). When you say:
When I hit a switch the 'tv' goes up and stops at the top.
This means that this is the situation that currently happens now - that something is stopping your tv already. What you probably mean is
When I hit a switch, the 'tv' should go up and stop at the top.
And what might even be better is to distinguish who is doing what - the agent. I can see here that it's you who hits the switch, but who or what is making the TV go up or down?
When I hit a switch, the sketch should make the 'tv' go up and should stop it at the top.