Enter variable value by key presses

GeorgeC47

Senior Member
I would like to set a value of a variable, say b1, by tapping an input key.
Example: to enter '5' I tap the key 5 times.
If there are no more key presses in the next 3 seconds, the program accepts the number as 5 and moves on.
I looked up the variable 'timer' and the instruction 'settimer', but these do not seem to be available for the 18X.
Can someone suggest a solution?
 

Andrew Cowan

Senior Member
How about using the count command? You'd need a debounce (say a 555 acting in monostable mode), but that should work for your need.

You could debounce using a pause (in the PIC), but that would mean writing your own version of the COUNT command rather than using the inbuilt command.

Not using the count command:
Code:
main:
if pin3=1 then
b1=1
goto triggered
endif
goto main

triggered:
for w1=0 to 4000        'approx 5 seconds
if pin3=1 then
   inc b1
   pause 200      'debounce
endif
pause 1
next w1
A
 
Last edited:

eclectic

Moderator
Rough, and needs refining,
but it works in simulation and for real.

Code:
#picaxe 18X

symbol timecount = b0
Symbol flag = b1
Symbol pressed = b2

Sertxd ("Start countdown ",cr,lf)

counter:
flag = pressed

for timecount = 0 to 30
pause 100
if pin1 = 1 then 
pause 500 ' debounce
pressed = pressed + 1
sertxd (#pressed, "  presses",cr,lf) ;or Serout
endif

next
If pressed > flag then goto counter

CarryOn:
sertxd ("Total = ",#pressed,cr,lf)

stop
e
 

GeorgeC47

Senior Member
Code:
main:
if pin3=1 then
b1=1
goto triggered
endif
goto main

triggered:
for w1=0 to 4000        'approx 5 seconds
if pin3=1 then
   inc b1
   pause 200      'debounce
endif
pause 1
next w1
A
Thanks. I am using input pin2 and output pin4 as a trial.
This works OK with a few mistakes when debouncing doesn't work properly.
Code:
main:
  if pin2=1 then goto triggered
goto main

triggered:
    b1=1
    pause 200                                 'debounce

  for w2=0 to 2000                            'approx 5 seconds
      if pin2=1 then
       inc b1
       pause 200                              'debounce
      endif
  next w2


  for b2 = 1 to b1                           'play back flashes
     high 4
     pause 100
     low 4
     pause 400
  next b2

goto main

I will have a try with Eclectic's code as well.
 

inglewoodpete

Senior Member
The debounce period that I use mostly is 40mS. The periods suggested above are much longer (500mS and 200mS) - are such long periods necessary for modern switches?

Just thought I'd throw that into the cooking pot.
 

eclectic

Moderator
The debounce period that I use mostly is 40mS. The periods suggested above are much longer (500mS and 200mS) - are such long periods necessary for modern switches?

Just thought I'd throw that into the cooking pot.
Actually, the pause 500 is for "human debounce" :)

(Or, it reduces the repeat-rate).

I tried a "pause 50 " but had problems with "overcounting"

e
 

eclectic

Moderator
Aha! Not really debounce at all, is it? Perhaps the comment should say 'Autorepeat' or similar.
inglewoodpete.

I must apologise for my behaviour,
and bow to your greater knowledge.

Perhaps, before the next time I try and help somone,
AND include the term

"Rough, and needs refining,
but it works in simulation and for real."

that I should check with you first?
 
Top