How to detect how long a button is pressed...

Mikler

Member
Newbie help needed to get me started.....

I need to see how long a pushbutton is pressed and carry out a different action depending upon the timing.

If a pushbutton is held down for under a second, I want to goto address1

If the pushbutton is held down for more than two seconds, to goto address 2

If the pushbutton is held down for more than five seconds, to goto address 3

I would be most grateful for help/code snippet etc

Thanks

Mik
 

slimplynth

Senior Member
Evenin' Mikler - shine your shoes guvvna? ;) - oh, maybe not then :D this (seemed to) worked in the simulator though.
Code:
symbol buttton = pin1

symbol time = b1

main:

pause 250
if buttton = 1 then goto timer 
if buttton = 0 then goto assess

timer:
time = time + 1
goto main

assess:

if time <1 then goto main
if time <5 then goto act1
if time <10 then goto act2
if time <15 then goto act3

act1:
high 1
pause 4000
low 1
time = 0
goto main

act2:
high 2
pause 4000
low 2
time = 0
goto main

act3:
high 3
pause 4000
low 3
time = 0
goto main
 
Last edited:

Andrew Cowan

Senior Member
I would change that to:
Code:
symbol buttton = pin1

symbol time = b1

checkbutton:
if button=1 then main
goto checkbutton

main:

pause 250
if buttton = 1 then goto timer 
if buttton = 0 then goto assess

timer:
time = time + 1
goto main

assess:

if time <1 then goto main
if time <5 then goto act1
if time <10 then goto act2
if time <15 then goto act3

act1:
high 1
pause 4000
low 1
time = 0
goto checkbutton

act2:
high 2
pause 4000
low 2
time = 0
goto checkbutton

act3:
high 3
pause 4000
low 3
time = 0
goto checkbutton
That way it constantly, not justchecks for button presses four times per second.

A
 

Mikler

Member
Hovis......

slimplynth>> Evenin' Mikler - shine your shoes guvvna? ;)

Hi Guv

Can I resist the temptation to crack 'hovis' and 'black pudding' jokes - instead can I apologise - I hadn't realised that checking a pin was so easy - to be honest I was overcomplicating things... (I wanted to do it in the background - using your method, I think I can do it within an interrupt routine - which is great)

Thanks very much (also to Andrew) for your help. It really saved me hours!

Mikler
 
Top