I am trying to setup my catapult to load to a certain position when I push a button. I currently have it setup to automatically load after every shot, but I want to put that function on a button. How do I go about doing this?
You can call button.pressed(), putting your function into those parentheses. This will work if your function is void function (void). You would place button.pressed() before your while(true) loop in user control, not inside of the loop.
What code are you using? Robot c VCS etc.
I am using RobotC to code.
Oh, well with RobotC that will be different. What you generally want to do is have an indicator that a button has been pressed so you can make sure the many times your single press is detected are only recognized as a single event. For example,
bool newlyPressed = false;
while(true) {
if (vexRT[Btn6D]) {
newlyPressed = true;
}
else if(newlyPressed) {
newlyPressed = false;
\\ run the function
}
}
You can see that pressing the button in this case does not actually call the function. Rather, the moment the button is released is the only time newlyPressed is true and vexRT[Btn6D] is false. So the moment the button is released the function will run exactly once from the single button press, no matter how long that button press lasted.
I tried to do this, but couldn’t get it to work. Instead I tried this.
If(vexRT[Btn6D] == 1 && SensorValue[In1] <= 1050)
{
motor[port2] = 127;
motor[port3] = 127;
}
This ended working ok. I can press and hold the button until it reaches the stopping point.
Yes, that will work. It doesn’t work off a single button press, but rather a hold, as you said. But it will certainly work.
If the other method didn’t work, I suspect it’s probably in the function you placed in there. What is the code for the function you called?
[SensorValue[In1] <= 1050)
{
motor[port2] = 127;
motor[port3] = 127;
}
Well, in both cases I don’t understand why the motors stop at all. Once you set them to 127, they should stay there until you set them to something else.
As for your function, you’ve got some odd bracket/parentheses stuff going on there, as well as probably a missing while statement or similar.