I am trying to implement end switches. I haven't got enough inputs, so I used two switches on each end of the axis connected to just one input. I am using two buttons to move the motor around the axis, the motor is moved while the button is pushed, so it is momentary. I would like the motor to stop on the end and also to not move when i press the button and it is in the end position of that direction. I hope I was able to explain reasonably.
This works just partially, the motor wont stop at the end while holding the button, it will crash to the end switch but when I release the button and press it again it wont start to move again, which is correct, but I also need it to stop when it reaches the end switch while the button is still held.
The code is very very long, it is a controller for record cutting lathe. For this part, there are three inputs set as INPUT_PULLUP, so there is LOW when buttons are pressed or end switch reached.
The outputs are DIR_PIN for setting direction, MS_SWITCH for microstepping settings and EN_PIN for enabling the motor so it starts to move
Many have tried this. Work through the logic, it will work. Add in little details like mechanical switch bounce, and all heck breaks loose. MUCH better, and simpler, to use two inputs.
Good luck. If this is, for example, an Uno, have you used up all the analog pins as well? Because they can also be digital inputs. Or, you can use an analog input with two different voltage levels for the end positions, though that means you'll be doing analog reads to check limits, which is slower.
I know the cons of this solution, the undefined position doring powering up can be a problem, but I have to work with what I have, it is already wired up, lot of cables, I cannot change it now. I will be happy if it will work just with those buttons
when there are separate pins at each endpoint, travel is stopped when traveling in one direction and one end-pt is reached. And when the direction is changed, that condition doesn't prevent leaving that position
when the same pin is used for each end-pt, the end-pt switches need to temporarily be ignored when direction is reversed and re-enabled once the switch opens so that the travel will stop when the opposite end is reached
Maybe my logic is wrong, but when the "go left" button is pressed down, the motor should go left until the limit switch is activated so END_IN pin is LOW, the motor should stop even if the button is still pressed, also, when the last direction was left and the limit switch is activated, the motor won't move when I press the "go left" button again, but it can move in the other direction if I press the "go right" button. That's what I am trying to do...
I described what I want it to do, but the mentioned code wont stop the movement when the motor reaches the limit switch, but when I release the button and press the same button again it wont move, so it is somehow half working, now I need to figure out how to stop it when the button is pressed and the motor reaches the limit switch. Sorry, I am not sure if i can explain it any better.
I think most of us understand it, maybe better than you do.
The key as @gcjr points out is figuring out how to get off the limit switch.
To do that, you should be looking not at the state of the limit switch input pin, but transitions that appear there.
When the limit input goes form HIGH to LOW, that is to say when either switch becomes closed, you have reached the limit.
Switch directions and only allow going the other way.
You don't need to care about whether the limit is open or closed, only that it went from open to closed.
This is called state change detection, and in combination with contact or switch debouncing, is all and what you need to accomplish.
The detection will see the other transition, and then it is ready to see the transition you are interested in at the other end.
You will need to have a loop that comes around often enough checking for the transition you are interested in so the motor can drive too far past either limit switch.
You'll need to keep track of which direction means you'll not be banging against the end of the rail.
If the left and right limit switches are on the same pin, and it's parked at the left end, how does the going-right motion know it hasn't reached the right limit switch?
I think you might be able to solve this with edge-detecting the changes on the limit switch pin, plus a state machine for handling the IDLE, GOING_LEFT, STOPPED_LEFT, GOING_RIGHT, STOPPED_RIGHT logic. If you are STOPPED_LEFT with the limit switch triggered, you can transition to GOING_RIGHT or IDLE but not GOING_LEFT until the limit switch un-triggers & vice versa.