freehabitat, on Tue Feb 12, 2008 7:12 AM, said:
can you explain me this in little words?
dim controller_p0 = c
controller_p0 = 0
:
:
dim select_debounce = d
select_debounce = 0
:
:
if select_debounce <> 0 then select_debounce = select_debounce - 1 : goto main
if switchselect then select_debounce = 12 : controller_p0 = controller_p0 ^ 1
ok,.. you dim 2 variables,.. but what is happening with them in this 2 lines??! i am really not sure with this function.
A "debounce" variable is a way to have a short delay after pressing something (a console switch, joystick button, key on a keybord, etc.) before you recognize it as being pressed again. The computer is extremely fast at doing things, and even if you slow it down a little bit by letting it check on something only once every frame (i.e., once every 1/60 of a second on an NTSC Atari, or once every 1/50 of a second on a PAL or SECAM Atari), it can still check on that something several times between the time you start pressing down on the switch/button/key/etc., and the time you finally stop pressing down on it. For example, suppose you tell the Atari to increment the color value by 2 each time you press the fire button, and it takes you 1/10 of a second to press the fire button and then finish letting go of it-- then it would work as follows:
Color = 0
Begin pressing the button -- 0/60 of a second
Frame 1 -- 1/60 of a second -- Is fire pressed? Yes -- Color = color + 2 = 0 + 2 = 2
Frame 2 -- 2/60 of a second = 1/30 -- Is fire pressed? Yes -- Color = color + 2 = 2 + 2 = 4
Frame 3 -- 3/60 of a second = 1/20 -- Is fire pressed? Yes -- Color = color + 2 = 4 + 2 = 6
Frame 4 -- 4/60 of a second = 1/15 -- Is fire pressed? Yes -- Color = color + 2 = 6 + 2 = 8
Frame 5 -- 5/60 of a second = 1/12 -- Is fire pressed? Yes -- Color = color + 2 = 8 + 2 = 10
Frame 6 -- 6/60 of a second = 1/10 -- Finish letting go of fire button
Is fire pressed? -- Not anymore
So by the time you pressed the fire button and finished letting go of it, the color got incremented several times, and went from 0 to 10, instead of from 0 to just 2 as you might have expected.
With a debounce variable, you set the debounce counter to some delay value as soon as you start pressing the fire button (or console switch, or keyboard key, etc.). Then you let the counter decrement by -1 during each frame, until it reaches 0. As long as the debounce counter is greater than 0, it means you don't want the Atari to keep reacting to the fire button, because you're waiting a little bit to make sure you've had time to finish letting go of the fire button. But if the debounce counter is at 0, and the fire button is pressed, then you want to set the debounce counter (so it can start counting down), and react to the fire button.
Actually, I really should have coded the debounce logic a little bit differently, as follows:
if !switchselect then select_debounce = 0 : goto main
if select_debounce <> 0 then select_debounce = select_debounce - 1 : goto main
select_debounce = 12 : controller_p0 = controller_p0 ^ 1
What this modified code does is check the select switch first. If it isn't pressed, then the debounce counter gets cleared, and you jump back to the beginning of the loop. Otherwise, the select switch must be pressed, but before you just react to it, you check the debounce counter. If it isn't 0, then the delay isn't over yet, so you go ahead and decrement the counter, then jump back to the beginning of the loop. Otherwise, the debounce counter must be 0-- and we've already determined that you must be pressing the select switch-- so it's okay to set the debounce counter to 12 (or whatever value you want to use for the delay time period), and then do whatever it is you want to do when the switch has been pressed.
As for the last part-- "controller_p0 = controller_p0 ^ 1"-- it will make the controller_p0 variable flip back and forth from 0 to 1 to 0 to 1 to 0, etc. (In batari Basic, "^" means "exclusive or," like the "EOR" command in assembly.)
Michael