Chasing my tail - Toggle Switch

ZOR

Senior Member
I have a momentary switch that makes pinC.3 = 0 (normally held high)

I am trying to make the action of a toggle/flip flop when the button gets pressed. When the button is pressed it is to make C.2 high or low depending on it's current state. However I cannot find a way round to make it a start/stop event on each time button pressed.

Code:
Start1:
	
	if pinC.3 = 0 and PinC.2 = 0 then
	PinC.2 = 1
	
	else
	
	if pinC.3 = 0 and PinC.2 = 1 then      
	PinC.2 = 0      
	
	      
	End if:End if
	      
	Goto Start1
 

rossko57

Senior Member
A common method is to wait for the button to be released, before looking again to see if it has been pressed.

Be wary of switch contact mechanical bounce, which can fool software into handling multilpe presses. There are software and hardware ways around that.
 

ZOR

Senior Member
Many thanks rossko57, got there.

Code:
Start1:
	
	if PinC.3 = 0 and PinC.2 = 0 then
	High UVON
	Do while PinC.3 = 0:loop
	
	Else
	
	if PinC.3 = 0 and PinC.2 = 1 then
	Low UVON
	Do while PinC.3 = 0:loop
		
	end if:end if
		      
	goto Start1
 

BESQUEUT

Senior Member
Many thanks rossko57, got there.

Code:
Start1:
	
	if PinC.3 = 0 and PinC.2 = 0 then
	High UVON
	Do while PinC.3 = 0:loop
	
	Else
	
	if PinC.3 = 0 and PinC.2 = 1 then
	Low UVON
	Do while PinC.3 = 0:loop
		
	end if:end if
		      
	goto Start1
I wish I had a crystal ball to help you, but you did not publish whole code... (and schematic...)
1) How is defined UVON ?
2) If I well understand #1, you want to toggle C.2 each time C.3 is pressed. May C.2 be driven externally ?
3 Why not using the toggle command ?
 

ZOR

Senior Member
Many Thanks.

Symbol UVON = C.2

When made high, relay closes putting power on LED'S

So the code shown splits itself into 2 legs, when pushbutton is pressed then it checks if UVON is high or low. If low then make high, if high make low.

I would like to know how I can write "If PinC.2 = 1" as "If UVON = 1" as using both UVON and PinC.2 is not so clear when reading code back.

I could use the toggle command but for such a small bit of code thought it did'nt warrant it.
 

AllyCat

Senior Member
Hi,

I must admit that the two ENDIFs on one line, with no indenting, got me scratching my head. So what about:

Code:
symbol UVON = c.2
symbol pinUVON = pinc.3
startx:
     if pinUVON = 0 then
          do : loop until pinUVON = 1
          toggle UVON
     endif
     goto startx
Note this doesn't appear to simulate correctly in PE5 if the label is start1: :(

Cheers, Alan.
 

ZOR

Senior Member
Thanks Alan for an alternative. Is my code wrong with 2 end if's? The code works okay, and allows me to know the state of whether the LED'S are on or not. Having the two sections of the code allow me to clearly branch off later to do things. It will respond to external multitasking.

Thanks again for your code, got some knowledge from it.
 

BESQUEUT

Senior Member
Is my code wrong with 2 end if's? The code works okay, and allows me to know the state of whether the LED'S are on or not. Having the two sections of the code allow me to clearly branch off later to do things. It will respond to external multitasking.
.
Not wrong... only unreadable...
How can we submit some code if we don't known what you want to do ?
 

ZOR

Senior Member
I am sorry, but I thought my thread was quite clear, I was trying to adopt a toggle action of a switch. My code showed clearly what I was up against, as after switching on it got followed by switching off. There was no break, however rossko57 quickly saw my problem, gave advice and now works. No need for reams of other code, especially as not written yet.
But thanks for your input.
 

erco

Senior Member
Not perfect, but simple & compact:

do
if pinC.3=0 then toggle c.2: pause 500:endif
loop


If you hold it, it will toggle on & off each half second
 

ZOR

Senior Member
Thanks erco. Yes compact, and shows how toggle can be used, but I am happy with my code as holding the button down does nothing, it only toggles after releasing button and pressing again. So far not had contact bounce or poor switching, so far so good. Thanks again.
 

AllyCat

Senior Member
Hi,

Having the two sections of the code allow me to clearly branch off later to do things.
So you plan to add more IF ... GOTO ... ENDIFs to that already rather confusing lump of code? Wil your branch(es) be before or after the DO WHILEs ? :confused:

holding the button down does nothing,
Indeed the PICaxe can do nothing, at least excluding any multitasking (which IMHO may create more issues than it solves).

For understanding and maintainability one should attempt to write "modular" code, i.e. one block (maybe erco's, maybe mine, or perhaps another version entirely, since each has its own particular advantages and disadvantages) "converts" the pushbutton into a "toggle switch". Then a completely separate software module acts on the state of the toggle, perhaps switching (on/off) the LEDs and/or performing other actions, etc..

Then, for example if you later discover it's necessary to do something whilst the button is pressed, the changes to the appropriate module can be reasonably easy and predictable.

Cheers, Alan.
 

ZOR

Senior Member
Thanks Alan.

to that already rather confusing lump of code?
The code does everything I want, it's all working okay. It's a simple procedure that starts/stops other multitasking blocks of code. I just put my code up asking how it could be modified as a simple event establishing what a toggle switch would do using a momentary one. I don't know why it looks a confusing lump of code, just a switching event.

I can add anything I want in the start or stop section. I can do other things whilst the button is pressed or released. I can put variables in there to store what mode it's currently in.

I was happy with the answer I had in #2. (rossko57) That answered my question of the stop (off) routine being activated after the start (on) routine being selected.

Thanks again for help.
 

erco

Senior Member
I could use the toggle command but for such a small bit of code thought it did'nt warrant it.
Using TOGGLE cuts your code to less than half. Neither routine debounces though. If your switch is noisy, add a small cap across it.

Original:

Start1:

if PinC.3 = 0 and PinC.2 = 0 then
High UVON
Do while PinC.3 = 0:loop

Else

if PinC.3 = 0 and PinC.2 = 1 then
Low UVON
Do while PinC.3 = 0:loop

end if:end if

goto Start1

Modified:

Start1:

if PinC.3 = 0 then toggle C.2
Do while PinC.3 = 0:loop

endif

goto Start1
 

ZOR

Senior Member
Thanks erco.

Code:
Modified:

Start1:

if PinC.3 = 0 then toggle C.2
Do while PinC.3 = 0:loop

endif

goto Start1
My code gives me a clean visible split to allow me to see both sides of my toggle and do what I want in those events. Where it's toggled to is logged.
I am happy with how my code is, and as you say will add a small capacitor if I get bounce. Have a good week, thanks all again.
 
Top