Simple latch

fathom

New Member
Me again, more goofing around

Easy project press a button and the LED lights press it again and the LED goes out.

main:

if pin0 = 1 then high 1 ;is pin0 high, if yes switch output 1 on
pause 500 ;wait for fat finger to get off the button
endif ;end this loop and carry on if pin0 is low
if pin0 = 1 then low 1 ;pin0 is high? then switch output 1 off
pause 500 ;wait for fat finger to get off the button
endif ;end this loop and carry on

goto main ;loop back to the start
The pause is an issue is there a cleaner way of doing this?
 

kevrus

New Member
Don't know if this is any better, still has a pause in it for de-bounce. could be done in hardware with resistior/capacitor if required

Code:
do
   do
   pause 100
   loop until pin0=1 
 toggle 1
   do
   pause 100
   loop until pin0=0
 loop
 
Last edited:

Dippy

Moderator
I'm not goingto rewrite it, but many people don't bother with a long pause but after the 'press test' would DO..LOOP Until Pin0=0.
i.e. detect the press like you have done, but then sit in a loop until the button is released.

This assumes that the switching is clean and doesn't require debounce and that you have a pulldown in the right place.

There are quite a few ways to do this, setting flags, using variable, using toggle etc. - so have a practice.

Edit: Blimey - kevrus was in like Flint!
 

LarryGrad

Member
Hi Fathom. Have you taken a look at the "button" command? Maybe that is what you want. I have not used it, but it looks to provide a debounce.
 

hippy

Technical Support
Staff member
I'd suggest writing down a list of what the program will do and/or creating a flowchart on paper. The program responds to button pushes, and for two button pushes the button must have been released at some time ....

Wait for button to be pushed
Turn on LED
Wait for button to be released
Wait for button to be pushed
Turn off LED
Wait for button to be released
Repeat
 

Dippy

Moderator
That's good, but you may find the 'loop until the button is released' technique better if you progress onto things where faster keystrokes are required.
 
Top