# Limit Switches help.

I know that there is a section on limit switches in the defualt FRC code, but i have tried to use that section many times and failed so what i was wondering was if anyone had written there own limit switch code or could explain to me the process of how i could write one. I will be using digital inputs to limit speed controllers.

pseudo example

``````
if (rc_dig_in01 == 1)
{ joy_1 <= 127;}
else
{joy_1 == joy_1;}
``````

Its just a basic limit switch I really dont know why they gave me so much trouble. But i have everything happen from losign all joystick - motor interactivity to having everything work like the senosr wasnt tripped. I was just wondering if someone could give me an example or a pointer, i know its a stupid question but its been stumping me. Now i have time to ponder and experiment.

It’s important to remember that in C “==” is a logical equal to operator and “=” is an assignment operator. “<=” is also a logical operator meaning less than or equal to. So I think you would want your code to look like this:

``````
if (rc_dig_01 == 1) {
if (joy_1 <= 127) {
joy_1 = joy_1;
} else {
joy_1 = 127;
}
} else {
joy_1 = joy_1;
}

``````

A good place for ternaries!

Full code might look like this:

``````
if (RC_LIMITDOWN) //Alias to digital in
Joy_1 = ((Joy_1 < 127) ? 127 : Joy_1)
else if (RC_LIMITUP)
Joy_1 = ((Joy_1 > 127) ? 127 : Joy_1)
``````

2 things when i first wrote this post i cound recall the joystic alias its something like p1_y ( i dotn have the code in front of me, and 2nd off what doese this section do :
Joy_1 = ((Joy_1 > 127)** ? 127 : Joy_1**)

As a C beginner, I wouldn’t reccomend you use ternary operators. I’ve been through various programming courses and have wrote software for many years and I don’t use them.

What it looks like the original poster wants to do is limit the motion of a motor based on whether a limit switch is active.

It looks like you want the motor to go only in reverse (less than 127) when that switch is active. I’m not too good with C, but in pbasic, you’d use the MAX and MIN declarations, e.g.

p1_y = p1_y MAX 127 MIN 0

I’m not sure if there is an equivalent in C, or how it would be implemented, but I think that’s what you’d be looking for- to set that condition when the switch is enabled.

Be careful when using limit switches, eh?

On our bot, we had three switches on our lift system; one at the bottom, top, and middle (height for grabbing 2X ball).

Now the problem with our lift is that if we kept trying to run the motor to lift it up past its max, it would bend the shafts. So we tossed the limit switch at the top so when it tripped we couldn’t make it go any higher even if we tried. Our programmer decided to allow the lift to raise up again when we dropped it down to trip the middle limit switch (God knows WHY he did it THIS way… -_-

Anyhow, in one of our matches, our robot fell backwards quite a bit and the top limit switched tripped from hitting the PVC tubing of the stationary goal. Because it tripped (when our lift was all the way down) we couldn’t raise the lift up at all (we couldn’t trip the middle switch) and couldn’t hang.

So after that we forced our programmer to put in an override switch on the joystick.

So whatever it is you’re doing with limit switches, you might want to consider using a button or something to override the code if need be.

Or just cover them up. It’s an engineering bug, not a software bug. But good idea.

? 127 : Joy_1 is a short-hand If-Else. In the form of A ? B : C, If A is true than the ternary returns B, else it returns C. It’s also really nice for macros.

The pair of If’s is for bottom and top. And Aliases are a must for many things.

PS- When do you stop being a beginner C programmer and move up to the next level? This is my fist real use of C.

Hahaha, i thought that was a sarcastic joke before reading the second sentence. Maybe it should have been though. In programming, like everything else in life (though probably more so) people have there own ways of doing things (and obviously think thats the best or right way). Mike doesn’t like to use ternary operators while some others do. Though a ternary operator can be somewhat harder to read than an if-else statement, it is useful in many situations because the entire if-else can be in one line. In my opinion, if you understand the concept, have seen properly written and used and examples, and are comfortable with it, then go right ahead. The best part about programming, especially with small simple programs, is that it is pretty easy to try stuff out yourself, so don’t hesitate. Follow these guidelines and you should level-up in no time

Thats good to know, im working on a program for auton that needs theese switches so as soon as i get ahold of the robot again im going to try what ever i can.