After recent posts in another thread, I got to thinking that after jumping through a few extra hoops, using a timer to allow for a sudo multi tasking situation could be achieved. It a little more granular than I am used to. Using the settimer instead of timer3 should be possible but i haven't has chance to look at that as yet.
It would be useful if maths could be done within an if statement but digress
The below code contains example running on a 20x2 flashing an LED at 1hz and PTM switch debounced without the use of the pause command.
One other thing to note is, I am not sure what will happen if call a goto within the debounce sequence, I can't remeber how the picaxe deal with leaving open ended if statements like that. I would advice trying to use gosubs instead if you need to call another piece of coe
you can alter the timing by changing tmr3setup prescailar or altering the debounce and flash values
I suggest reading the tmr3settup command to get a better understanding of that command, and thusly this code
tmr3setup - BASIC Commands - PICAXE
This timer can also be used to determine elapsed time privding you keep a record of when the even happend (wX = timer3) and do the obligtory maths (elapsed_time = timer3 - wX) and comparing that to the time you need.
It would be useful if maths could be done within an if statement but digress
The below code contains example running on a 20x2 flashing an LED at 1hz and PTM switch debounced without the use of the pause command.
One other thing to note is, I am not sure what will happen if call a goto within the debounce sequence, I can't remeber how the picaxe deal with leaving open ended if statements like that. I would advice trying to use gosubs instead if you need to call another piece of coe
Code:
#no_table
#terminal 9600
#picaxe 20x2
tmr3setup %10000001 'timer 3 on 20x2 enabled, no prescler
'with no prescaler timer3 will increment 32,768us or 33ms
'this mean the finest timing that can be had is 33ms plenty fast enough for debounce
symbol debounce = 2 '2 cycles of the internal timer or 66ms, this will be the debounce time
symbol flash_time = 30 'should give around 1000ms
symbol last_flash = w2 'the time of last flash
symbol last_press = w3 'the time of the last buytton press
symbol temp_timer = w4 'temp word for calculating time difference
symbol led = b0 'led pin
symbol sw3 = pinc.7 'button on this pin
main:
'flash without pause
temp_timer = timer3 - last_flash 'calculate difference between last flash and now
if temp_timer > flash_time then 'check to see if it been longer
toggle led
last_flash = timer3 'update time of last flash
end if
'debounce without pause
if sw3 = 1 then 'check for input
temp_timer = timer3 - last_press 'calculate time since last press
if temp_timer > debounce then 'check for debounce time exceeded
'do stuff
sertxd ("Button pressed",13,10)
end if
last_press = timer3 'update last press time
end if
goto main
I suggest reading the tmr3settup command to get a better understanding of that command, and thusly this code
tmr3setup - BASIC Commands - PICAXE
This timer can also be used to determine elapsed time privding you keep a record of when the even happend (wX = timer3) and do the obligtory maths (elapsed_time = timer3 - wX) and comparing that to the time you need.