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?
Looking for a neat way of handling a momentary switch
-
- Posts: 6
- http://meble-kuchenne.info.pl
- Joined: Sat Oct 01, 2022 6:15 pm
- Been thanked: 1 time
-
- Valued Contributor
- Posts: 1528
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 353 times
- Been thanked: 549 times
Re: Looking for a neat way of handling a momentary switch
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
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
-
- Posts: 86
- 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
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.
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.
-
- Posts: 777
- Joined: Thu Dec 03, 2020 8:25 pm
- Location: Denmark
- Has thanked: 670 times
- Been thanked: 175 times
Re: Looking for a neat way of handling a momentary switch
Hi Nheather!
I am not sure, but maybe this examble is usefull. Anyway the counter will only count one time after pres the switch
Br jorgen
I am not sure, but maybe this examble is usefull. Anyway the counter will only count one time after pres the switch

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