Hi everyone! I'm sorry if the title is confusing-- I wasn't really sure how to word my question in one sentence but hopefully it's understandable. I'm looking to build a state machine, implementing millis() for timing. I have 8 conditions the program is looking for in the default state. If any of those conditions occur, the same millis() code will first run for 5000ms regardless of the condition that was triggered, and immediately following that same millis() code would be a function/state dependent on the condition that was initially triggered in the default state, while simultaneously also returning to looking for any of the 8 conditions in the default state.
The flow of the states would look like this, as an example:
Default state -> IF (A0 > 2.0 volts) -> Warning ->Troublecode1 (also check for conditions in default state) -> Default state (break);
Default state -> IF (A1 < 1.0 volts) -> Warning ->Troublecode2 (also check for conditions in default state) -> Default state (break);
Default state -> IF (A2 >= 3.0 volts) -> Warning ->Troublecode3 (also check for conditions in default state) -> Default state (break);
Default state -> IF (A3 = 1.5 volts) -> Warning ->Troublecode4 (also check for conditions in default state) -> Default state (break);
Default state -> IF (A4 = 2.0 volts) -> Warning ->Troublecode5 (also check for conditions in default state) -> Default state (break);
The above only shows 5 conditions, but you get the idea I'm sure. The same "Warning" state, implementing millis(), will always be ran before going to the specific Troublecode state (specific to the condition that took place).
I suppose the best way to solve this (please correct me if I'm wrong) is to actually embed the millis() function of the "warning" code to actually be in each Troublecode state, and just call the specific Troublecode state instead of going to a separate Warning state first, so like this:
Default state -> IF (A0 > 2.0 volts) -> Troublecode1 (also check for conditions in default state) -> Default state (break);
Default state -> IF (A1 < 1.0 volts) -> Troublecode2 (also check for conditions in default state) -> Default state (break);
The problem that I can't seem to find a resolution for after searching several forums and Google, in general, is how to call what's needed for the "Warning" portion of the code using millis(), for each Troublecode state, without writing out the millis() code completely over again for each Troublecode state, since the millis() code for the Warning will always be the same.
Is there a way to simplify the calling of the same millis() code for Warning, for each of the Troublecode states, without writing the same block of code over again for each Troublecode state?? How can I also simplify the checking of the same conditions that are in the default state, while each Troublecode state is running, without writing the conditions all over for each Troublecode state? Obviously the answer to these would be to have a separate function written out for the conditions, a separate function for the Warning, a separate function for each Troublecode state, and just call the functions below, IF you were able run multiple functions at the same time:
Default state -> Conditions() -> IF (A0 > 2.0 volts) -> Warning() -> Troublecode1() & Conditions() -> Default state (break);
Default state -> Conditions() -> IF (A1 < 1.0 volts) -> Warning() -> Troublecode2() & Conditions() -> Default state (break);
Obviously, we know that multiple functions can't be called to run at the same time, so I'm not sure how to check for the same list of conditions as from the start of the loop() again while running a Troublecode function simultaneously, without re-writing the conditions to return to Warning for every instance of a Troublecode state.
Also, is there a more efficient way laying this code out if I want a specific millis() code and a specific message written on an OLED display for each Troublecode state, after the same millis() code for the Warning has been ran for the 5000 ms?
My apologies for not having some true example code of my millis() programming. I can write some up if necessary, I just haven't gotten that far yet because I'm stuck with trying to figure out how to lay out this state machine. If anyone is willing to give some advice on this I'd be very appreciative!! Thank you so much!
-Andrew