digital on/off switch

jobarrco

New Member
is there any way to make a push button switch into an on/off switch?
as in if pinx goes high make piny go high until pinx goes high again?

does that make sense? i know a simple on/off switch would be easier, but i'm just curious...

thanks!
 

goom

Senior Member
Probably several (or many) ways to do this.
My first thought is to set up an interrupt for pinx. In the interrupt routine, toggle piny, pause to allow the push button to be released before proceeding, then reset the interrupt.
 

westaust55

Moderator
Firstly, welcome to the PICAXE forum.


within the interrupt loop that goom has mentioned you could use a command like:

LET OUTPINy = PINx

Have a look in PICAXE manual 1 around page 46 onwards
 
Last edited:

goom

Senior Member
There is a TOGGLE command (look it up in the manual). You could also program it manually with something like:
if pinz=1 then
low piny
else
high piny
endif
You would connect the output (piny) to an input pin (pinz).
You could probably do it without the extra pin connection by reading the status of all outputs (readoutput b1 for example), then checking the bit of b1 which corresponds to the output pin that you want to toggle.
Anyway, why complicate things when the toggle command exists.
 

westaust55

Moderator
digital on/off follower

If the intended code is directly within the main code, then the TOGGLE command is idea as the code cannot get out of sequence and it only takes 2 bytes.

If within an interrupt routine then from my experience occasionally an event can be missed which could result in the toggle command being out of step with the input pin state.

In this case I recommend the more positive: LET OUTPINy = PINx
This only requires 3 bytes and will always be correct.

In a smaller PICAXE where program space may become a concern use of an IF...THEN construct will require a further 6 bytes of space for the IF...THEN lines over and above the TOGGLE x or the LET OUTPINy - PINx program lines.
 

ckoehn

Member
I have been a "reader" of this forum for a little while now and throughly enjoy the challenges and answers that the members have. Below is the code that I would use in having a push button switch turn something on or off.
Code:
'this code takes 34 bytes

'change pin number for your processor type
symbol led_sw=pin1	'pin for the switch
symbol led_pin=2		'pin for LED
symbol led_bit=4		'bit for pin 2
symbol sw_on=1

main:
	if led_sw=sw_on then switch_led
	
	toggle 4
	
	'do other stuff when switch isn't pressed
	
	goto main
	
switch_led:
	readoutputs b0
	
	b0=b0 and led_bit
		
	if b0=led_bit then	'LED is ON, turn OFF
		'code when LED is OFF
		low led_pin
	else				'LED is OFF, turn ON
		'code when LED is ON
		high led_pin
	end if
	
	do while led_sw=sw_on loop	'loop until switch is released
						'if you do a pause instead of this,
						'holding down the button will
						'make the light go ON,OFF,ON,OFF, ect.	
	goto main
 

westaust55

Moderator
Clint,

Firstly welcome to the PICAXE forum.

Secondly not a bad effort by any means for a new PICAXE user for your sample of code. Well laid out and comments to go with it.



The original request/objective was:
if pinx goes high make piny go high until pinx goes high again?

here your code leaves the LED at output 2 high when the switch (input) returns to a low state.
 

ckoehn

Member
Westaust55,

I must be a slow. Isn't that what my program is doing? When pin 1 (sw) goes high the LED on pin 2 lights. When pin 1 (sw) goes high again, the LED on pin 2 goes off.

Clint
 

westaust55

Moderator
Clint,

You will find that your program does the following:

1. While the switch (input 2) is off, output 4 toggles on and off

2. when the switch is on, the output 2 is switched on and then you enter the DO LOOP

3. when the switch goes back off, output 2 is left on and control returns to main loop and While the switch (input 2) is off, output 4 toggles on and off

4 when the switch is on again the output 2 is switched off and then you enter the DO LOOP

5. when the switch goes back off, output 2 is left off and control returns to main loop and now its back to step 1.

So in effect you are toggling the output 2 through one cycle for every two cycles of the input.
 

ckoehn

Member
Westaust55,

That is what I thought it would do. I think the original question was if a push button could be made to work like an on/off switch. When you push the button switch it will toggle the LED then wait for you to release the button (do while button is pressed loop). When you press the push button again it will toggle the LED and then wait till you release the button. If you don't have the "do while button is pressed loop" and you just hold the button down, you will toggle on/off continuously. Am I right on that?

Thanks,
Clint
 

westaust55

Moderator
Westaust55,

That is what I thought it would do. I think the original question was if a push button could be made to work like an on/off switch. When you push the button switch it will toggle the LED then wait for you to release the button (do while button is pressed loop). When you press the push button again it will toggle the LED and then wait till you release the button. If you don't have the "do while button is pressed loop" and you just hold the button down, you will toggle on/off continuously. Am I right on that?

Thanks,
Clint
Okay (got it :) )
looking for a push-push type switch where the action is:
push on and push off effect - aka latching

as opposed to a simple on-off follower.

All comes down to terminology :rolleyes: http://www.kpsec.freeuk.com/components/switch.htm
 

BCJKiwi

Senior Member
@ckoen
Just for the sake of clarity and completeness;

The post #8 code should not have the "toggle4" line as this creates issues if the button is not pressed.

Suggest this form of code to work the way intended;
Code:
'Push-Push switch from momentary contact pushbutton.
'post #8
'http://www.picaxeforum.co.uk/showthread.php?t=11388#post88311
'this code takes 34 bytes
 
'change pin number for your processor type
symbol led_sw=pin1 'pin for the switch
symbol led_pin=2  'pin for LED
symbol led_bit=4  'bit for pin 2
symbol sw_on=1
 
main:
' if led_sw=sw_on then switch_led
' toggle 4
 if led_sw=sw_on then gosub switch_led
  
 'do other stuff when switch isn't pressed
 
 goto main
 
switch_led:
 readoutputs b0
 
 b0=b0 and led_bit
  
 if b0=led_bit then 'LED is ON, turn OFF
  'code when LED is OFF
  low led_pin
 else    'LED is OFF, turn ON
  'code when LED is ON
  high led_pin
 end if
 
 do while led_sw=sw_on loop 'loop until switch is released
      'if you do a pause instead of this,
      'holding down the button will
      'make the light go ON,OFF,ON,OFF, ect. 
' goto main
return
 

jobarrco

New Member
Thanks for all the replies! I appreciate it... here is my solution.
It is simple but it works... :)

Code:
setint %00000001,%00000001
'activate interrupt when pin0 only goes high

main:
	'other code/program here
	goto main
	

interrupt:
	toggle 4 ‘ switch output 4 on
	pause 500
	if pin0 = 1 then interrupt	'loop here until the
						'interrupt cleared
	setint %00000001,%00000001	're-activate interrupt
	return
 

goom

Senior Member
I think that the pause may be necessary in order to avoid multiple interrupts due to switch bounce.
 

westaust55

Moderator
When you enter an Interrupt subroutine, the interrupt function has already been turned off, That is why you must issue the SETINT command again if you wish to detect further interrupt events.

instead of the line:
Code:
if pin0 = 1 then interrupt	'loop here until the interrupt clears
try:
Code:
do : loop until pin0 = 0   ; wait until switch goes low/off
 
Last edited:
Top