# Help on programming a limit switch

Hey, I have been trying to figure out how to program a motor with 2 limit switches. I want the motor to go forward, when a trigger/button is pressed, until it hits the first limit switch and then it will go back until it hits the second limit switch and then it will stop… and repeat when i press the trigger/button again. this is for our shooting setup, we plan on having an arm that will push the disks to the shooter.

I haven’t done something like this before, so someone will have to double-check this solution, but I believe the following code will achieve your goal:

Begin.vi:

Teleop.vi:

Finish.vi:

The Teleop routine takes place in three steps (working from top to bottom). When the trigger on the joystick is pressed, the arm begins to move at half speed and releases the first limit switch. When the arm hits the second limit switch, the code will compare the previous state of the limit switch to its current one. If those states are different (the switch has been tripped), the arm will begin to move back towards the first limit switch. Finally, when the arm reaches and trips the first limit switch, the motor will be turned off. I created this off the top of my head, so I am sure Greg McKaskle will be along soon to point out the flaws (or unnecessary complexities).

Best of luck.

Sorry, I can’t see your examples to check them out. They’re on a blocked site.

Here are two examples of just the limit switches. You want the one to the right, then need to add you button presses.

I will attach the LabVIEW files to this post. After looking at your code, I think each of us is trying to solve different problems. I included an the attached VI an extremely rudimentary drawing of what I was imagining jackofallace’s robot “arm” to look like: an appendage with its degree of rotation limited by two limit switches that the arm itself hits. Perhaps some clarification is needed.

Arm and 2 limit switches.vi (33.1 KB)

Arm and 2 limit switches.vi (33.1 KB)

That attachment works better for viewing.

Your Teleop needs a little work. Your answer approximates what I posted.
The way you set it up, the three case statements will roll the dice for who wins at setting the motor speed. The last one wins, but that doesn’t mean the one on the bottom. You need a cascade of wires rather than local variables to do what you are attempting. Local variables should generally be avoided and shouldn’t be set all over the place. Local/Global variables are set once, read everywhere.

PS.
Here is your example with some changes to make it a cascade. I also simplified the button check. The picture is your code just with the Case statements replaced by Selects, only because that way every choice can be seen at a glance.

The limit switch values might be different depending on how the limit switch has been wired. There are two choices in the wiring: 1) normally open, 2) normally closed. One way will return a True until it is pressed, the other way will return a False until pressed. After you’ve wired a limit switch, test it in code to see which value you are getting. In other words, you may have to reverse your True/False cases depending on how the switch has been wired (and don’t assume without double checking).

Arm and 2 limit switches-revised.vi (33.6 KB)

Arm and 2 limit switches-revised.vi (33.6 KB)

Ah, thank you for the corrections. There is nothing in the false cases of my code so I’m not sure if it would ever be attempting to write to the local variable at more than one location (perhaps in the instant you press the button for the first time). But your way is definitely more elegant.

Mark, I had trouble with switching the axis to a button. I get broken wires between the upper and lower limit.

Which example are you referring to?
The simple limit switch example won’t run the sequence you were talking about originally. That’s what Stephen’s code does.

In that original limit switch example, if you want to use a button instead of the joystick axis, then you’ll need to use the button to choose a motor speed to put into the coercion vi.
The broken wires are because the upper and lower limit wires are carrying floating-point numbers, while a button is a boolean True or False.

@propionate I tried the modified version of the code you created (the corrected one by mark mcleod) and ran into an issue with it. When the trigger is pressed, the arm moves forward as desired, then the first limit switch is pressed, the problem arises is that the only way the arm moves in reverse is if it presses the switch and lets it go and same goes for when it presses the second limit switch to stop. Essentially those movements only work when the switch is pressed and released, but does not work if you continuously hold the either of the switches. Is there anyway to change the program so that when the switch is pressed, it starts the next set of programmed movements instead of it waiting for the switch to start those movements? Thanks for any help

Here is about the only thing that I can contribute to, if you have a jaguar motor controller, don’t forget about the built in limit switch connections on the motor controller. This is the absolute easiest thing, no programming required.