Hello. The essence of the problem is this - there is a button (without fixing) and I need the LED to turn on and off (with a single click) from this button and also when pressed, it blinks when released. In order for the LED to turn on and off, I wrote here to blink - no. As far as I understand, it is necessary to use a state machine here, but I do not know how. Please help me figure it out
state machines posted on this forum are typically very simple. i wouldn't say the code i posted is a state machine though it has 2 modes of operation: flash or don't flash.
a common sub-type of state machine is a sequencer which goes from one state to the next state due to some stimuli, possibly a timeout
a more conventional state machine has several states and several stimuli that can each occur in each state.
state machines on this forum are typically implemented as switch statements. within each case can be a switch statement for one or more stimuli (some can be ignored depending on state).
you could consider have a 2 state machine with 2 stimuli (buttons) in each state. in the flashing state, the LED flashes every period. one button moves to the non-flashing state with the LED on and the other button with the LED off.
in the non-flashing state, each button transitions to the flash state but with different periods. for example: 500 msec and 1500 msec.
there are 2 types of state machines: one where the actions depend on the state and the other where the actions depend on the transitions between state
i described that 2nd. the button presses not only changed state but also either turned the LED on/off and set the period
in the code i posted, the timer is not a state, but simply depends on state (flash)
one more comment about state machines. recognizing the stimuli: a button press or timer expiration, is done outside the state machine. doing so inside the state machine can result in a lot of redundant an unnecessarily complicated code
state can be a variable (global, static), a sub-function can be called to update the state where the stimuli is an argument
so when a button press is recognized, that state function is called with the argument corresponding to that button stimuli. same with a timer, if set and when it expires