Like a toggle.
an example:
*clicks button
motor spins forward
*clicks button
motor spins backward
*clicks button
motor turns off
would it even be worth it?
Like a toggle.
an example:
*clicks button
motor spins forward
*clicks button
motor spins backward
*clicks button
motor turns off
would it even be worth it?
Yes, it is possible, depending on the situation, it is worth it. Could you like to explain more about the situation you are currently in so we can offer advice more targeted to your situation?
-Blaziumm
Use an enum to track what state the button is in:
enum ButtonState {
Stop,
Forward,
Reverse
}
ButtonState state = ButonState::Stop;
// This should be an event, so it should be outside of your drivercontrol "while" loop and only registered once!
controller.ButtonA.pressed([]() {
if (state == ButtonState::Stop) {
state = ButtonState::Forward;
motor.spin(vex::forward, 100, vex::percent);
} else if (state == ButtonState::Forward) {
state = ButtonState::Reverse;
motor.spin(vex::reverse, 100, vex::percent);
} else {
state = ButtonState::Stop;
motor.stop();
}
});
In this example, pressing the button once will run the motor forwards. Pressing a second time will run it reverse. Pressing a third will stop it. Pressing a fourth time will run it forwards again, etc…
If you just want a simple toggle (2 states, e.g. spin either forward or stopped) then you can use a boolean:
bool toggled = false;
controller.ButtonA.pressed([]() {
toggled = !toggled;
if (toggled) {
motor.spin(vex::forward);
} else {
motor.stop();
}
});
Pressing once will run the motor forwards, pressing it again will stop it.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.