I have yet to fully write it, what I have is a roughing out and won't make any sense because the way I program is to write in a manner that makes sense to me despite the functionality not being correct, I add in what I need when I work it out.
I have seen examples of state machines that are a list of functions, eg...
Blockquote
void loop(){
getButtonState();
checkButtonPressed();
sendSignalsToOutputs();
if( someStateReached ){ // we end it
while(1){};
}
}
When I get to if( someStateReached )
I intend on jumping out of loop() to do things to wait for the restart as I don't want the state to change at that point because I am using external modules that keep a count of whats happened, even when power is turned off, it returns, so I need to have some housekeeping done, such as store a copy of that information in the flash memory so I can check it to see if the external mods were cleared or not, of not, to reset the external mods.
THB the more I explain the more confusing it will become which is why I ask simple questions first.
I tried using OpenAI chatGPT to generate possible examples I could use but even with an AI its understanding of what I was trying to attempt didn't generate anything close to what I am building.
Sure it generated a couple of state machines but they were nothing like I need, the examples helped a little but not that much as its not complete in terms of what question I asked, almost like it was just spitting out previously stored examples and then some advice on one about the fact I used a non digital pin to generate something that would use PWM, which was helpful to know.
For example, I asked it how you would use a state machine to pulse a pin (7) when one of two buttons are pressed without using delay() and it used delay within a state machine... It got confused with the "Reset" and treated the reset as an input when its going to be an output signal to the external modules...
I'm pretty sure by now you're completely lost and confused.
To give you some idea... I shall try and give you a rundown of the states involved.
The system listens for one of 3 buttons to be pressed to start that count, when another button is pressed, that count stops and another counter starts and so on until all three buttons are pressed and held for 3 seconds, all active counters cease to count and an audible beep is made. The machine then sits and waits for three buttons to be pressed again for 3 seconds AFTER the first check on three buttons being pressed and held has had a "Release" state detected if so, beeps are output in that process, the internal reference is reset and external references are reset and the system returns back to waiting for one of three buttons to be pressed. (back in to loop() )
If power is lost, the external references will when powered back on indicate their last state automatically as they are not part of the Arduino, which is why the internal count needs to be checked from flash and if it has counts in them, to pulse the reset pin allocated to clear those counts on external modules then enter a listener state as described above.
Confused much? I know I am as the code I have does need to be completed before I can test it, I am still waiting on parts to arrive to build the external components and check if I need to build a debouncer module as I can't be doing with making it in software for reliability reasons and peace of mind and simplifying code to negate needing to illuminate an LED to confirm button pressed.