Count how long switch down ?

1968neil

Senior Member
Hi guys

Need a little help here.
I need to use the same switch to do 3 things.
Ive done 2 of them using :

if pbswitch = down then switchdown ' is switch pressed
branch counter, (talkthruoff, talkthruon, recovery) 'branch on counter state
counter = 0 'reset counter
goto loop1

switchdown:
counter = counter + 1 'Increment counter

sdloop:
if pbswitch = down then sdloop ' wait til switch released
goto loop1

What i need to do is hold the switch for 3 or so seconds then it will goto another piece of code.

Any help appreciated.

Regards

Neil
 

premelec

Senior Member
Sample the switch in every so many milliseconds and add that into a variable then take action appropriate to the number stored - you've got the idea... However note: switches tend to not have perfect closure ['bounce'] so put a small capacitor and resistor across the input from switch so it makes the input definite during the progam's sampling of the contact.

Don't reset the counter variable until you've done all your actions -
Do
If b0 = 1 Then Act1
If b0 = 2 then Act2 :
If b0 = 3 then Act3 [Act3 includes b0=0]
b0 = b0 + 1
Pause x
Loop
Modify the timing by a pause amount... you're almost there.... I hope...
 

hippy

Ex-Staff (retired)
It's not clear where you want the test for hold to be. When dealing with interactive control like this a good thing to use is a "state diagram", one example pulled at random ...

http://www.elecfans.com/article/UploadPic/2009-4/2009430104639738.gif

You define which states the system has - "talkthruoff", "talkthruon", "recovery" - as the circles and then the arrows indicate what action must occur and when it does which state the system then moves to.
 

1968neil

Senior Member
full code

Thanks for the reply guys,
I've added the whole code for clarity.
What i need to do is hold the switch down manually for 3 seconds to make the code go to the recovery header in the code otherwise it will act as normal.
It's really doing my head in now !





symbol led = 4
symbol op1 = 1
symbol op2 = 2
symbol pbswitch = pin3
symbol down = 0
symbol up = 1

symbol counter = b0

counter = 0 'initialise counter

loop1:
if pbswitch = down then switchdown ' is switch pressed
branch counter, (talkthruoff, talkthruon) 'branch on counter state
counter = 0 'reset counter
goto loop1

switchdown:
counter = counter + 1 'Increment counter

sdloop:
if pbswitch = down then sdloop ' wait til switch released
goto loop1

talkthruoff:

low led 'Main led indicator OFF
low op1 'O/P 1 Low
high op2 'O/P 2 Low
pause 250 'wait 250ms
high 0 'output high on 0
pulsout 0,250 'send neg going pulse of 250ms on 0
pause 20 'pause 20ms
goto loop1


talkthruon:
high led 'led on
high op1 'O/P 1 High
low op2 'O/P 2 Low
pause 250 'pause 250ms
high 0 'output high on 0
pulsout 0,250 'send neg going pulse of 250ms on 0
pause 20 'pause 20ms
goto loop1

Recovery:


toggle op1
toggle op2
pause 250
goto loop1
 

BeanieBots

Moderator
Get your head around one thing at a time.
You need a loop to do the detection in.
Then integrate your other code into that loop.

Code:
counter = 0
downfor3secs=false
Do
  counter = counter + 1
  if counter = 30 then
    downfor3seconds = true
  endif
  pause 100
Loop while button = down
You can modify the pause and counter test to suite any other code you may need to place within the loop. The example above will test the button every 0.1 seconds.
 
Top