Help with programming limitswitch for rotating turret

HELP, I’ve set up 2 limit switches on my turret and hooked them up to DiO ports on the roborio. The idea is that when one side of the turret does a 180 it will stop moving in that direction and can only go until it hits the other switch and repeat. I have this so far for the code which I thought would work (and it kind of did) but it did the opposite of what I wanted. Instead of only moving the other direction when a switch is pressed, it was able to move in both directions when a switch was pressed and only in one when it wasnt. This is my code, any thoughts would be appreciated.

A couple things -

Limit switches in DIO, if wired between S and GND will read high (true) when open, as there’s an internal pull-up resistor inside the rio.

Check out the WPIlib guide, they invert all of their examples.

But even better than this, would be wiring it straight in to the motor controller if that’s an option you have! SparkMax and Talon SRX both have limit switch ports. These can be enabled in configuration and by default are. I’m a big advocate of using these features as it runs outside your code - if the limit switch is hit, no matter how your code tells it to move, it won’t. And, it will display a designated light code for troubleshooting purposes. Downside is it’s a little more work to set up - wiring and soldering to a breakout board or crimping a small connector. But it’s well worth it. You will protect your motor from being run past the limit in almost all circumstances, and with 0 code to write.

Hope this helps!

1 Like
  1. Is there by chance any way to switch the way it reads high when open? Does that consist of wiring it the other way or is it not possible? 2. As for wiring it directly to the motor controller, thats another alternative I was thinking of and it seems like its the best option even with the extra work
  1. It depends on the limit switch. Some limit switches have 3 terminals; one will be labeled “NO” (normally open) and the other “NC” (normally connected), with the third one being “common”, but if the limit switch has only two terminals, it is what it is, and you have to deal with it in code.
1 Like

It’s always smart to wire limit switches so they are normally closed; that way a broken wire and a pulled out connector both say “limit hit”.
You can wire your switch between signal and ground to use the built in pull up. Or three wire it: common to signal, NO and NC to +5 and ground. With that you can have positive up and down.

+1 on “wire it into the motor driver”!!! I finally convinced our programming lead to do this, and it has really notched up our limits!
BTW, on breakout boards, you -really- want the ones with through hole pads. The other type are very vulnerable to ham handed soldering and thus.pad tear off.