I can’t get my code to work. I want it to spin the lift until the limit switch is pressed and when the limit switch is pressed I want it to stop and move down 90 degrees.
Need to see the full code really. How is that MyBlock called? Is it in a loop?
Also which direction is up and which is down?
I know in IQ you can name directions in the config window.
It’s not in a loop. It’s called on its own in the autonomous section.
Forward is up and reverse is down
If the LiftUp function is called on a single line then it will check the status of the limit switch once and go on to the next step. If the limit switch is not pressed then it will tell the Lift motor to spin forward and it will keep going (unless there is another command that tells the Lift motor to do something else). It will not continue to check the status of the LimitSwitchA,
If you want the programming to continuously check the status of the switch then you need to put it inside of a while loop and most likely inside of its own Autonomous header.
If we could see the rest of your code we would have a better understanding to help you.
If it’s not in a loop, this is what would happen:
// Program does whatever
// LiftUp() is called
LiftUp();
// LiftUp() executes
if (limit switch pressed) {
// This is what happens if the switch is not pressed at the instant LiftUp() is called
Lift.spin(down 90 degrees);
}
else {
// This is what happens if the limit switch is not pressed at the instant LiftUp() is called
Lift.spin(up);
}
// The program ends
// The lift either finishes spinning down or spins up forever
What you want should be done in a while
or repeat until
loop.
repeatUntil(limit switch pressed) {
// This code will run over and over until, when the loop checks, the limit switch is pressed
Lift.spin(up);
}
// This code will run after the condition (limit switch pressed) is true and the loop exits
Lift.spin(down 90 degrees);
Try putting the if else
block in Lift Up inside a forever loop and then adding a break block under the spin Lift forward for 90 degrees
. This will keep running this part of the code until it reaches the break statement.
It sounds to me like you want to preset your lift arm. If you want to do that one time then the following code should work for you. It will keep the motor spinning until the limit switch is pressed. Once the switch is pressed it will stop and then reverse for 90. Sound right?
This can also be done with a button and a little extra code if you want the ability to set the arm during driver or autonomous.
Yes, that should work. Another way to do it is how @Hudsonville_Robotics showed. That is a more understandable way of writing it, but both ways should function the same.
I have one limit switch and I want it to be able to raise and lower my lift until it is hit. I have the move down 90 degrees because after the switch is triggered, I need it to untriggered so I can then use it for the down movement. This probably sounds confusing so I can take a picture of my robot when I get home if you need.
I have students with catapults and they want it to cycle after they launch the balls.
The following code will run the motor if the controller presses the Right Up button of if the LimitSwitch is not pressed. This way it will always reset to the limit switch.
If this is not the case then I need to understand what you want to accomplish.
Here is a video of my bot explaining what I want to do. I have to compress it to a zip for some reason. Vex Limit Switch.zip (93.0 MB)
I watched the video and I have two thoughts.
-
My practical side wonders why you don’t use the built in motor encoders to tell the lift to go to a specific position.
-
My creative side appreciates that you want to incorporate a limit switch with dual purposes and so I threw together the following code as something you can test. It involves two functions - one for each direction.