![]() |
C++ Toggle Button
I am trying to make a toggle for switching between Arcade Drive and Tank drive. I wrote and tested this code but the only result I get is a switch between Tank to Arcade, not the other way back. I have such a complicated system because I believe that if I simply set a variable to be True or False, it won't compensate for if the driver holds on to the button. That's why I have a clause in there that checks if the button has been released.
If you find any issues, please reply! Code:
void OperatorControl(void) |
Re: C++ Toggle Button
One way to increase the readability of your code would be to use an enum to keep track of the drive mode. This also lets you simplify the logic for tracking the button press state:
Code:
void OperatorControl(void) |
Re: C++ Toggle Button
Very nice answer. And to bring out a couple more subtle points displayed in the answer that will help understandability.
- Don't be afraid to use a bit more whitespace. Parentheses, brackets, and keywords all deserve attention and the whitespace gives the user visual focus. The answer example has a good balance of spacing. - The code for managing the toggling is now grouped very tight and easily understood without paging around the drive code packed into it. This can be maintained by either putting the drive code into their own sub-functions, or simply making a new section immediately after the toggling code which re-checks the arcade/tank enum solely for the purpose of doing just the drive functions. There is no penalty for re-checking a value such as this. |
Re: C++ Toggle Button
This is one of the programming topics I teach my students: how to make something happen when a button is pressed or released. The first challenge is to detect button presses and releases as an edge event. The second challenge is to toggle a mode when a button is pressed. For the first challenge, detecting an edge event means to detect the transition between two states which means you need to remember the previous state and compare it with the current state. This could be done with two Boolean variables.
Code:
bool prevState = false;Code:
bool driveTankMode = false;Code:
bool prevState = false;Our team even went further to define a joystick button class where the code will be called periodically to check for all button changes. When any button has changed state, it will do an event callback with the parameters specifying which joystick, which button and whether it was a pressed or released event. This greatly simplify our code. For example, our joystick class is TrcJoystick. Code:
class MyRobot |
Re: C++ Toggle Button
Consider this:
Code:
class Posedge_toggle{ |
Re: C++ Toggle Button
Sure, how about this? It is basically the same as your Posedge_toggle class except that it allows the update method to also return the switch state so you don't have to make the get call.
Code:
class ToggleSwitch |
Re: C++ Toggle Button
Quote:
It seems like you've added a lot of unnecessary stuff though. You're made the class's code go from 13 lines to 33 while you could have made it do that by just adding one line: Code:
class Posedge_toggle{ |
Re: C++ Toggle Button
Interesting...
I ran a tool that counts line of code on the ToggleSwitch class code and it gave me: Code:
Commented Comment Comment LOC/semiCode:
Commented Comment Comment LOC/semi |
Re: C++ Toggle Button
Quote:
I think there is value in having a count of physical lines too though. For example, it controls how much you can see in an editor. |
Re: C++ Toggle Button
Quote:
|
Re: C++ Toggle Button
Quote:
|
Re: C++ Toggle Button
Quote:
|
Re: C++ Toggle Button
Quote:
|
Re: C++ Toggle Button
Quote:
|
Re: C++ Toggle Button
Quote:
|
Re: C++ Toggle Button
Quote:
Apologies, I don't remember if there are contact bounces when a mechanical switch disconnects. When I can get to our control system I'll test this out and post an update. Remember, again, we don't need to worry about this for our joystick buttons, they've been debounced for us. |
Re: C++ Toggle Button
Quote:
|
| All times are GMT -5. The time now is 14:31. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi