Looking for a neat way of handling a momentary switch

For general Flowcode discussion that does not belong in the other sections.
Post Reply
nheather
Posts: 6
http://meble-kuchenne.info.pl
Joined: Sat Oct 01, 2022 6:15 pm
Been thanked: 1 time

Looking for a neat way of handling a momentary switch

Post by nheather »

Hi,

Looking for a neat way of handling a momentary switch in FlowCode.

For example I have a push button that is used to increment a counter - if I have this in a fast loop then a single press gets read multiple times - I appreciate that one way is to add a delay but this is inelegant, make the delay too small and you will still get occasional double reads but make it too high and you can miss pushes.

When designing discrete electronics you would tend to use rising or falling edge transitions - is there an equivalent in FlowCode?

chipfryer27
Valued Contributor
Posts: 1170
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 287 times
Been thanked: 416 times

Re: Looking for a neat way of handling a momentary switch

Post by chipfryer27 »

Hi

Many ways to achieve this and some are dependent on your microcontroller. You may also wish to take "Debounce" into consideration too whilst you are at it. Again many ways to do this.

If using the Switch / Button component(s) then it has a debounce facility in properties which you can set to your preference (typically a few tens of mS). This obviously is a software debounce based on time.

Instead of looping you could, for example, use an Interrupt. Depending on chip this could be pin or port specific and can trigger on rising/falling/both edges. Your MCU can be busy doing whatever in Main and upon the switch/button triggering the interrupt, it will branch to do whatever you wish (e.g. increment a counter) before returning to Main. Many examples of using such in the WiKi.

Hope this helps
Regards

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Looking for a neat way of handling a momentary switch

Post by WingNut »

I always put a 200-400 ms delay before the switch. Works every time

canary_wharfe
Posts: 78
Joined: Thu Dec 10, 2020 3:54 pm
Has thanked: 6 times
Been thanked: 11 times

Re: Looking for a neat way of handling a momentary switch

Post by canary_wharfe »

The best way I have ever seen to combat contact bounce on any switch is to use a combination of a short delay and a super schmitt trigger circuit. It's like using a sledge hammer to crack a walnut but it works every time when simpler circuits fail. It adds an extra component to the input of a uC but one of the best super schmitt triggers can be made using a CD4007 CMOS logic chip. Do a Google search on using CD4007 and schmitt trigger.

FYI a super schmitt trigger is like a normal trigger except the hysteresis low high thresholds are much wider than can be obtained with a standard trigger. When you add this on to a standard short timed delay logic circuit you get zero contact bounce on any switch.

The attached file shows a simulation of this technique that I built in Multisim using ordinary MOSFETS to replace the CD4007 because the available CD4007 sim models are useless.
100 mS Debounced Switch with Super Schmitt Trigger using CD4007.jpg
100 mS Debounced Switch with Super Schmitt Trigger using CD4007.jpg (100 KiB) Viewed 1099 times

jgu1
Posts: 615
Joined: Thu Dec 03, 2020 8:25 pm
Location: Denmark
Has thanked: 507 times
Been thanked: 132 times

Re: Looking for a neat way of handling a momentary switch

Post by jgu1 »

Hi Nheather!

I am not sure, but maybe this examble is usefull. Anyway the counter will only count one time after pres the switch :D

Br jorgen
Attachments
Switch1.fcfx
(12.83 KiB) Downloaded 58 times

Post Reply