making momentry switch work as latching switch

tyrone

New Member
hi,
i have been reading through the manuals about a program i need to make and cant find the right area that suits my need. what i need is a momentry switch pressed on once and let go of to activate a circuit for an led and then when you press the momentry switch in a second time and let go it turns the circuit off, the switch has to act like a push on/ push off switch, can anyone help as i have no idea how to program this or the making of the circuit. any how to's or if you can point me in the right direction in the manual or even if you could start me off with what i need in the writing of this program and how to hook it up. thanking you in advance. i am using school experimenter 8m.
cheers
ty
 

Tom2000

Senior Member
Tyrone,

There are at least a couple ways to do this.

1. You could monitor the button state in a continuous do...loop structure. When you see the switch pressed, debounce it, then toggle the LED output.

2. Look up the setint command in Manual 2. You could use an interrupt to monitor the button. When the switch is pressed, once again debounce it, toggle the LED output pin, and set interrupts to monitor the switch once again.

The method you choose depends upon what else you might want your program to do. If switching the LED on and off is all you need to do, probably the do...loop method is best. If you want your program to do other things, try the interrupt method.

Good luck!

Tom
 

moxhamj

New Member
main:do
if pin1=1 then exit' detect the button push
loop
if b0=0 then' every time this is called then change the output
high 2
b0=1
else
low 2
b0=0
endif
pause 100' debounce
do
if pin1=0 then exit' wait till button released
loop
goto main
 
Last edited:

SD2100

New Member
I made this a while ago for something ???, It uses the interrupt on pin3 to stop/start pin4 toggling high/low.
Code:
#Picaxe 08m
symbol StopFlash  = bit0
symbol StartFlash = bit1

setint %00001000,%00001000	'set pin3 as interrupt pin

main:
	low 4
	if StartFlash = 1 then Flash
	goto main
	
Flash:
	toggle 4:pause 500 'Flash LED on pin4
	if StopFlash = 1 then main
	goto Flash
	
Interrupt:	'comes here when input pin3 goes high
	if StartFlash = 0 then
	    StartFlash = 1:StopFlash = 0
	elseif StopFlash = 0 then
	    StartFlash = 0:StopFlash = 1
	end if
	pause 150
	do while pin3 = 1:loop 'Stay here if the button held down
	setint %00001000,%00001000
	return
 

tyrone

New Member
problems still

hi guys,
thanks for your quick posts, im still having some problems, dr akula when i copy and paste the program you listed the computer gives me an error: do without loop,and i dont know how to fix this, also phil 75 i have downloaded your program but when i push the button the led only stays on for about a second then turns off, im not sure if i wrote what i am after right but i am after a momentry switch pressed and released and the led stays on then pressed and released and the led goes off, it sounds easy but i cant work this out and i am very new to this stuff, i am grateful for all your help and anymore info or programs would help, thanks again guys.
regards
ty
 

Tom2000

Senior Member
Here's something for the 08M, Tyrone. I've added lots of comments so you can see how the program operates.

Have fun!

Tom

Code:
;
;  Toggle LED each time button is pushed.  Picaxe 08M
;

  symbol LED    = 2          ; active high LED on physical leg 5
  symbol PB     = pin3       ; active low pushbutton and pullup on physical leg 4

Main:

  low LED                    ; initialize:  LED off

  do                         ; start of program's operation

    do                       ; wait for a button press
    loop until PB = 0

    toggle LED               ; if the LED is off, turn it on
                             ; or if the LED is on, turn it off

    gosub Debounce           ; debounce the pushbutton
                             ; wait for the button to be released before
                             ; continuing the program          

  loop                       ; loop forever

end


Debounce:

  do                         ; debounce button down
    pause 10
  loop until PB = 1

  do                         ; debounce button up
    pause 10
  loop while PB = 0
  
return
 

SD2100

New Member
Tyrone

To get the LED to stay ON instead of flashing you can change the line from
Code:
toggle 4:pause 500
to
Code:
high 4
hope this helps
 

Fletch

Member
I was doing this on Sunday but my solution was clunky. These code fragments will be a huge help. Thanks guys!
 

leftyretro

New Member
Does anyone use the Button command?

From the manual:

BUTTON pin,downstate,delay,rate,bytevariable,targetstate,address

It's a command that lacks examples in the manual, but on the surface would seem to be a robust command that should deal with real world contact problems. But all those variables :eek:

Anyone have snippet examples to share?

Lefty
 

tyrone

New Member
me and my big mouth,lol

ok, i got the switch working on the schools experimenter board, now i want to take that chip and set it up seperately in a circuit for the led on/off, my lack of skill and understanding this circuit has me at a loss as to how it would be hooked up. if tom or one of you guys could pop us a quick diagram of where the wires go into the circuit that would be great, again im a pain...but thanks again for all your help.
kind regards
ty

e-mail - tyronefinlay@hotmail.com
 
Last edited:

Tom2000

Senior Member
Tyrone, hook the chip up as shown in the diagram in Manual 1. Then add the LED and switch to the same pins you used for your experimenter board setup. Draw yourself a little schematic of the arrangement, then wire from that.

C'mon, guy, you've got to do at least *some* of the work on this project.

Tom
 

tyrone

New Member
hi tom,
i didnt mean to come accross as if i was asking you to do all the work, even though it sounded like it, it was just that i only bought this board to make this led circuit and i was just after as much info as i could get from people that know the system. thanks again for all your help.
kindest regards
ty
 

Fletch

Member
Just to say that I reflashed my 08 with a modified version of Phil's interupt driven code and it works like a charm. I had tried to be clever by using a very tight main loop and then using various flags to activate small subroutines. However the response time for a button push was not nearly as fast as i thought it would be, the interrupt code worked perfectly.
 
Top