Do - Loop ?

1968neil

Senior Member
Hi Folks,
I have a small issue that im trying to solve.........

I have a Push switch that increments a counter by 10 when its pressed once.

What i want to do is..........If it's held for say 3 seconds it resets the counter to Zero.
I'm racking my brains to think of a simple way of doing it.
Any ideas ? I have it working with a Do- Loop but its unreliable even with small pauses for de-bounce etc

Any input much appreciated
Regards
Neil
 

oracacle

Senior Member
I would suggest using one of timers.
There tuner Kroger increasing while the button is pressed and resets when let go.

A code snippet I wrote a while back maybe of help


Unfortunately I won't have much time until say least the weekend to look at our in more details
 

erco

Senior Member
Tested, try this:

Code:
#picaxe 14m2
#no_data

' momentary switch connects pin b.2 to ground
' attach a 0.1-1 uF capacitor across switch for debounce
' pinb.2=1 unless button pressed, when it is 0

pullup 4 ' enable pullup resistor on pin b.2

'w1 = accumulator value
'b1 = loop counter for reset
do
b1=0 ' reset loop counter
if pinb.2=0 then gosub press
loop

press: 
w1=w1+10 ' increment accumulator by 10 each keypress
sertxd (#w1,13,10)

for b1=1 to 255   ' increment loop counter
pause 8  '   adjust reset length ~3 seconds
if pinb.2=1 then release
next

w1=0: sertxd ("RESET",13,10)
release:if pinb.2=0 then release  ' wait for button release after reset
return
 

Attachments

inglewoodpete

Senior Member
Key-press lenght discrimination can be done as either a foreground or background task. Foreground coding is the best solution for M2 PICAXEs, while (I think) using a timer regular interrupt like 50, 80 or 100mS is simpler for X2s. You don't mention which chip series you are using.
 

1968neil

Senior Member
Hi guys,
Thanks for the info.
Erco’s solution works brilliantly in my 14M2 application. Thankyou 👍
I can see this being used in future projects also, a very clever implementation, the video made it crystal clear.
Thanks for the help, much appreciated 👍🍻🇬🇧
 
Top