Latching Momentary Switches - Example

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

User avatar
Jay Dee
Posts: 355
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 116 times
Been thanked: 135 times
Contact:

Latching Momentary Switches - Example

Postby Jay Dee » Sat Oct 29, 2011 6:49 pm

Hi all,
I wanted to have push button switches that would latch/unlatch in flow code, my result is below. It may be of use to others.
Note: I've just written this whilst stuck in the hotel with work and not tested it on any hardware so use at you own risk..... :-) Also it's written for ECIO 40P since I use these the most and in the free flowcode as I'm have laptop trouble ...again.

This description is a bit bodgy but might help unscramble it..

Basic Walk Through / Description.
> The Variable Mom_Mask is defined at the start to determine which of the 8 momentary push buttons will become latching. This mask can be simply changed to turn latching on/off for any of the 8 momentary switches. The value is shown in binary form to ease setting the correct bits. i.e. 0b00001111 sets the first four switches as latching and the others stay as momentary.
> Port_Old is initiated as 0 and and is used as the start state of all of the switches. i.e. nothing pressed.
> All switches are on Port B and their state is written to the variable Port_New.

Calc box1
> Apply Mom_Mask to Port_New, thus only the state of the latching switches is passed.
> Apply Mom_Mask to Port_Old, to see what the previous state of the latched switches was.(Initiated as 0)
>XOR the two masked results, this toggles (inverts) the state of any differences between the new and old latched switches but leaves alone anything that is the same.
> At this point any latching switch input that was a 1 is now a 0 and vise versa, these states are held in the variable called Latched

Calc box2
>NOT (Invert) the Mom_Mask and then apply it to the Port_New variable. So it will now mask out just the Latched states and pass the momentary switch states. The resulting states are held in the variable Momentary.

Calc box 3
> Recombine the Latched and Momentary states using a logical OR, the result is held in the variable Port_Old

>Send the Result to the Output.

I initially stopped at this point as it pretty much worked but if the key for a latched switch was held down the program would keep looping around and toggling the latched state on/off, which is a bit crappy. So put the following simple work around (Bodge) in, it may not suit all applications so you'll have to change it to suit.

The decision box checks if any of the latched keys have gone high, if yes it enters a loop and scans the port waiting for all latched input switches to go low. It then exits the loop and the program goes back to the main loop. This bodge is good, in that it only enters this loop due to changes with the latching switches (so a Mom key can be held low as long as you like) but bad as it stalls the program until all latched switch keys are released.

Anyway, this flowcode solves my problem and lets me quickly reassign the latching. Might help someone with something similar...
J.
Attachments
Latching_Momentary_Switch_V1.fcf
Flowcode - Momentary Switches can be set to latching
(10 KiB) Downloaded 224 times
These users thanked the author Jay Dee for the post:
medelec35 (Sun Oct 30, 2011 12:07 pm)
Rating: 5.26%
 

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Latching Momentary Switches - Example

Postby Spanish_dude » Sun Oct 30, 2011 3:19 pm

You should post this in the v4 section of the forum ;).

User avatar
Steve
Matrix Staff
Posts: 3073
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Re: Latching Momentary Switches - Example

Postby Steve » Mon Oct 31, 2011 9:17 am

It has now been moved. And thanks to Jay Dee for the article.