Switch Headaches

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Switch Headaches

Post by siliconchip »

hi all,
using a switch icon and masking i want to press my input button and go to the selected leg this isnt a problem but once in this leg i want the program to halt and wait for the same switch to be pressed again before it continues on to the main loop the one with the 5 second delay, ive managed this in the past but i tend to look at old designs if i get stuck but the hard drive i keep everything on has died and ive lost everything :oops: :oops: anyways i feel a plonker for asking and im sure this is easy for the more experienced among you but ive got brain freeze and the more i try the more im struggling any help would be appreciated

cheers bob
Attachments
Flowcode1.fcfx
(8.21 KiB) Downloaded 103 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Switch Headaches

Post by chipfryer27 »

Hi

I think it would be easier to use the Switch Component / Component Macro as it has three options, Read State, Wait Until High, and Wait Until Low. I've changed your chart to use it but please note that it is just an example and there are other ways to do what you wish.

Remember to debounce the switch as this could lead to false triggering of your 5-second delay loop. The Switch Component has under it's Properties a debounce value which can help to eliminate it. This waits x-microseconds to allow the switch to stabilise.

Hope this helps.
Regards
Attachments
Example.fcfx
(8.12 KiB) Downloaded 100 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Switch Headaches

Post by siliconchip »

hi chipfryer27
thanks for the reply, i had done something similar to this in the past but found if you kept your finger on the button too long it would go straight to the 5 second delay and not wait for another press, as the variable select is initially high from the first press, it needs to wait after the initial button press in case the button is depressed to long then once let go waits to see it pressed a second time before continuing

bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Switch Headaches

Post by chipfryer27 »

Hi

Once you are sure the debounce has settled you could insert a Wait Until Low command before the Wait Until High. This would ensure the button was first released. Of course this would need to be debounced too.

Other ways to do as you want could include IOC looking for rising edges (again you need to address bounce), or the use of Timers / Delays. Typically bounce lasts mS but really does depend on the quality of the switch and aging. I have an old button that I love as if you look at it on a scope you get peaks and troughs the Himilaya's would be jealous of that last almost 90mS even if you just look at it never mind press it :) It caused me a lot of "fun" and I keep it to remind me that cheaper isn't necessarily better. I bought a dozen from somewhere and it's almost impossible to solder wires to them as the body melts away as soon as you touch the terminal with the iron, but I suppose they'd make a great randomiser as you could never tell or guarantee which state it would be in or even when it would change :)

Regards

Edit....
Forgot to attach example
Attachments
Example.fcfx
(8.27 KiB) Downloaded 98 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Switch Headaches

Post by siliconchip »

Hi chipfryer

thanks for the reply ive edited your program and alls well BUT ......
when using the switch icon ive developed the below program it works in simulation but my question is

1 - have i gone round the houses in my design could it be done simpler.....

ALSO

2 - what is better to use in a program for this kind of switch operation is it the use of decision branches or the switch branch icon, id value any comments or critisisms

cheers bob
Attachments
SWITCH ICON.fcfx
(9.85 KiB) Downloaded 99 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Switch Headaches

Post by chipfryer27 »

Hi

In my opinion there is no right and wrong as long as you get the result you want when you want.

Essentially you have created the same scenarios that the Component Macro does for you but with one possibly very important omission. When you added the two push buttons to your chart and set their Properties (e.g. connection = A1 etc) you will see underneath the Polarity setting a Debounce value (default = 10mS). This feature is a software debounce value that used if and only if you use the associated Component Macro to read the status of the switch.

Without using the Component Macro(s) you will need to debounce your switch some other way. When you press a button the physical mechanics of it changing state is not a clean changeover. It literally bounces on/off many times before settling in it's final state. This bouncing causes the microcontroller to receive a chain of pulses (high / low / high / low etc). Unless you are addressing this externally I think your chart would work in simulation but not in reality. There are many ways to eliminate bounce. Some use software as described, other may create a R/C solution whilst some may use a dedicated chip to eliminate, but irrespective of what works best for you, you do need to eliminate it. A search of the forum will give much advice regarding this.

As I mention above I don't believe in a right and wrong way if it's working, but it could always be a bit better, well mine probably a lot better :) Once mine is working I try to see if I can streamline any part(s) of it.

Going from the chart your scenario is that you have two inputs that will potentially do two separate things so the Switch would seem better than Decisions if only to reduce the number of icons.

I would definitely use the Component Macros to help with debounce (as mentioned above) and rather than using two separate push buttons I'd probably use the Button Array set for two buttons as this makes it a bit simpler (I can read both buttons from a single Component Macro rather than one at a time).

In the attached example I used the Button Array but I made it latching to help illustrate.

Hope this helps,

Regards
Attachments
Switch Icon Button Array.fcfx
(10.46 KiB) Downloaded 106 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Switch Headaches

Post by siliconchip »

hi chipfryer27

switch array ah never thought of this plus i did a flowchart similar to yours before i noticed yours lol, but these defo seem the way to go, think the switch situation is just about covered in all scenarios, but thanks for pointing me in the right way and thanks for the help

bob

Post Reply