
Re: Going in and out of an autonoumous program.
Having it toggle will be a bit trickier. Using the code behind the 'untilBump' Natural Language command, I was able to throw together this little doozy;
The first thing I did was declare an integer variable called 'delayTimeMS'. This variable is used to compensate for a property of switches called 'bouncing', which is when a switch (physically nearing the other 'prong' internally) rapidly bounces between an open and closed state. You can't see it normally (it happens very quickly) but when you push a switch its output is actually similar to this:
00000000000000000000
10101010101011111111111111111111111111111
01010101010000000000000000000000000
Where 0's are the switch being open or 'not pressed' and 1's are the switch being closed or 'pressed'. We'll get back to how this used in the code soon.
The first while loop puts the robot in tele-op mode while the 8D button is not pressed. Once the Cortex sees the button first being pressed (or the first '1' value if using the example above), it kicks out of the while loop and enters a 50 ms 'debounce' state, which allows it to ignore the rapid 010101 switching. When the debounce period is over it waits for the button to be released, which kicks it out of the second while loop.
At this point you have physically pressed and released the switch. The robot now enters the third while loop (which contains the line tracking code) and stays inside of it until the button is pressed again. It follows the same 'debounce period -wait for release -debounce period' when the button is pressed, and then the whole program loops back up to the start of the infinite loop, starting the process over again.
If we didn't have the debounce wait command, the Cortex would rapidly switch between the tele-op and line-tracking segments of code because the amount of open and closed states (0's and 1's) is completely random when the switch is bouncing. The debounce period is switch dependant as well, so you may have to tweak that value to match your physical robots. Ideally, you would want it as low a value as possible without having any bouncing issues, but it's much better to be safe than sorry.
I highly suggest taking the time to go through our
Cortex Video Trainer as it covers a great many of these topics. The knowledge you can gain from them is invaluable as a ROBOTC coder.
Wikipedia also has a
great segment on switch bouncing you might want to check out.
_________________Check out our
Blog! And our
Facebook page!
Need help? Take a look at our
updated help documentation and the
ROBOTC Forums.