Page 1 of 1

Latching Momentary Switches - Example

Posted: Sat Oct 29, 2011 6:49 pm
by Jay Dee
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...

Re: Latching Momentary Switches - Example

Posted: Sun Oct 30, 2011 3:19 pm
by Spanish_dude
You should post this in the v4 section of the forum ;).

Re: Latching Momentary Switches - Example

Posted: Mon Oct 31, 2011 9:17 am
by Steve
It has now been moved. And thanks to Jay Dee for the article.