This is a sequence. There are specific transitions ([all are 0] => [X1 only is 1] => [X1 and X2 only are 1] => etc.). Also I note that the sequence is symmetric (after the last button is held for 4s, the sequence reverses until all are 0).
A simple approach would be to have the buttons represent bits (summed powers of 2) in an integer, and you could model the valid combinations and the valid transitions. For example, X1, X2, X5, X3, and X4 could drive bits 1, 2, 3, 4, and 5 (values 2, 4, 8, 16, 32), and the 4s expiry of X4 being held could drive bit 0 (value 1).
So if no buttons are held, all integer bits are 0, and the integer value is 0. If X1 is held, then bit 1 value would be 1, so the integer value would be 1 (2#000010 binary = 2 decimal).
So valid values and their transitions are
- 0, transition from 3 (2#000011 i.e. X1=bit 1 only, plus expiry=bit0)
- 2 (X1 only = bit 1 = 2), transition from 0
- 6 (X1 and X2 only = bits 1 and 2 = 2 + 4), transition from 2
- 14 (X1, X2, X5 only = bits 1 and 2 and 3 = 2 + 4 + 8), transition from 6
- 30 (X1, X2, X5, X3 only - bits 1 and 2 and 3 and 4 = 2 + 4 + 8 + 16), transition from 14
- 62 (all pressed/on = bits 1-5 = 2 + 4 + 8 + 16 + 32), transition from 30
- 63 (all pressed/on + 4s expiry timer = bits 0-5 = 1 + 2 + 4 + 8 + 16 + 32), transition from 62
- Timer expiry seals itself in as long as transitions are valid (Y2 on - green LED) and X1 value is 1
- 31 (X1, X2, X5, X3 + 4s expiry timer = bits 0-4 = 1 + 2 + 4 + 8 + 16), transition from 63
- etc
The program will have three sections, each evaluated once per scan cycle:
- build a new integer value from the states of the inputs on the current scan cycle
- compare that new integer value to the saved integer value saved from the last scan cycle
- if the integer values are the same at this point, then do nothing
- i.e. the new and saved integers' values will be the same
- if the integer values are different, then check if the [saved => new] transition is valid
- if the transition is valid, then assign the new integer's value to to the saved integer value
- if the transition is not valid, then do nothing i.e. leave the new and saved integers' value different
- compare the new and saved integers' value again
- if they are the same, then
- either this scan cycle was the same as the last cycle,
- or this scan cycle executed a valid transition from from the last scan cycle
- so turn on Y2 (green LED), turn off Y4 (red LED)
- if they are not the same, then
- turn on Y4, turn off Y2
How to recover from an invalid set of inputs or an invalid transition is another matter; maybe all of the above are only executed as long as Y2 (green LED) is on, and when Y4 is on it waits for the inputs to all be 0 and a user reset to be pressed.
I suspect something like the Step patter (cf.
here) will be useful.
Caveats
- This is a notional approach based on the minimal description of the original post (which should be repeated in a new thread).
- Code to follow the transitions and keep the green LED on would be fairly straightforward and take around a dozen or two rungs,
- although some of those rungs will be complex with several branches.
- I don't know your process and/or how to safely would recover the process from a bad input combination or invalid transition.
- I am certainly and completely and absolutely ignorant of any safety issues.