Ok, some analysis of the logic.
First, the only place you set limitPressed to false is at the very beginning. by having
Code:
if(limit.get()) {
limitPressed = true;
}
in operatorControl, this means that once the limit switch is pressed, limitPressed will ALWAYS be true - there is nothing to set it to false later.
Also, there is an interesting case where you hit the limit switch, so you set them to 0, but if you're still holding button 4 you then immediately set them to 1. What happens is you start to quickly oscillate between setting the motors to 0 and 1 as long as both button 4 and the limit switch is pressed.
Now, a note about how limit switches work (I'm afraid I can't view the picture from where I am... it's blocked by the school's connection). There are two ways to wire them, and they act exactly opposite in code. If you wire signal and ground to the common/normally open connectors, the switch will be TRUE when not touched, and FALSE when pressed. Connect between the common/normally closed connectors, and it operates the other way.
So, here's some code that should help:
Code:
//ensure that limitPressed is true only when the limit switch is pressed
limitPressed = limit.get();
//output the value of limitPressed so we can verify proper functionality of the limit switch
System.out.println(“limitPressed=“ + limitPressed);
//if we hit the limit switch, stop all motion
if (limitPressed)
{
victor1.set(0);
victor2.set(0);
}
//otherwise, if we hit button 4 move forward
else if (xbox.getRawButton(4))
{
victor1.set(1);
victor2.set(1);
}
//otherwise, if we aren’t pressing anything don’t move.
else
{
victor1.set(0);
victor2.set(0);
}
This code only works to power the motors in a single direction, with the assumption that moving in that direction will trigger the limit switch. Without knowing the exact setup, I can't be sure about powering in both directions and stopping at both limits... I'll leave the other direction up to you.
Note a couple of things in the code:
First, I'm writing the value of the limitPressed variable to the console. This will let you look in the RIOLog to see what it is at all times - before moving anything, try manually pressing it and see how the value changes. You want to make sure this works before doing anything else!
Next, ALL of the controls for the motors are contained in a single if/else-if block. This means that only one of them will be active at a time. The default state (the final else) is to stop the motors - if you aren't touching anything, they'll stop. The limit switch control comes first to ensure that, if the limit switch is pressed, it absolutely doesn't move.
Give that a try, see if it helps, and see if you can figure out the other direction from there!