Quote:
Originally Posted by 414cnewq
How would I implement a command to have a toggle button (i.e. the joystick button) to set the robot in reverse (press button once, in reverse, press button again, back to moving forward)?
|
You need to toggle the direction only when the "rising edge" of the button is detected.
Here's some C pseudo_code:
Code:
button_now = get_button(); // get the button state (pressed or not pressed)
if (button_now && ! button_previous) // detect rising edge only
{forward = ! forward;} // if rising edge, toggle the direction boolean
button_previous = button_now; // save the button state for comparison in the next iteration
... then use the "forward" boolean to determine whether to command forward or reverse.