All of the code is inside a while loop that runs only when pin 2 is LOW, including the code that checks to see whether pin 2 is HIGH. Can you see a problem ?
What is the resetFunc() function meant to achieve ?
What I try to achieve:
I am having a machine that opens and closes then I will put a proximity sensor at the end.
If the machine should get stuck the sensor should detect that the normal time it takes for a cycle is exceeded and gives a warning light.
The machine will take like 160 seconds for a cycle and sometimes 165 second. It very with within some few seconds. But a cycle can also be 300 seconds it depends on the setting.
But it will never jump from 160 seconds to 300 unless something is wrong.
If it has calculated that a cycle normally takes 160 seconds but the cycle suddenly exceedes 160 seconds a warning light should go off that there is a problem.
The general programming paradigm mentioned exploits an general concept and codes FSMs or finitestatemachines. Sometime people omit the finite part, so you might see "state machine".
You will or see already that the switch case statement is a good language feature to use when coding a finite state machine.
google
finite state machine arduion
or leave off the arduino term. Poke around, there are as many ways of teaching this as there are people convinced they have written or filmed the ultimate guide. Find a few sites that don't annoy you, maybe google
arduino traffic lights state machine
and see some fun stuff.
No time spent thinking about this will be wasted. Promise.
There are two states for your button: (1) Starting a timer when pressed and (2) Resetting when not pressed. That does not let much else happen. Maybe only look for button LOW to start and stop and reset. Let button HIGH to do nothing.
I can't tell from your #1 list of steps where this is going to repeat itself.
It seems like you have a training run to get a normal elapsed time, then you want to react if the time is exceeded on following runs.
Is the training a manual thing? Does the training get adjusted, like if the run was taking a bit longer each time would the allowed period be increased? Or similarly decreased?
How much longer than the trained period should a period be of it is to raise the alarm or whatever you call brining your attention to the fact? Are shorter runs always OK?
When and how does the alarm condition get reset?
The code in this case is going to be easier than unambiguously stating the desired functionality.
Okay I am looking into Arduino as a state machine, and I begins to see that this might be my solution.
So I have made a layout of what is going to happen.
Does it seem possible?
Remember that the Arduino is going to be an attachment for the big machine.
Basically you have a piston,
You push a button,
The Arduino detects the piston moves away from position A,
Starts a timer,
The piston comes back to position A,
The Arduino stops the timer and saves the result,
Next time the piston gets stuck
The Arduino detects that it takes longer for the piston to get back to position A and gives a Warning light.
Say, I bet a clever person could figure it out, but I would be helped if your diagram arcs had arrows on them showing which way the flow goes.
It also isn't yet clear to me when and how the training run is handled. Does the training time gleaned need to be stored across resets or power downs and ups?
Now this
Next time the piston gets stuck
Do you mean that subsequent to a training run (pushbutton lets Arduino time A to A again piston time) the Arduino observes leaving A and hits the panic condition if the process doesn't get back to A?
Is there some slightly longer period that would be OK?
Rigging it up first for a fixed time might be easier. Then add to that code a way to say, and do, a training run.
I got some ideas to solve this. I don't know if it is better.
But let's say instead having a stop-watch measuring the time and compare two cycles with each other, then put the measured value into a countdown that trigger the red led if it doesn't get a new measurement before reaching 0.
The other idea was using a speedometer sketch to measure speed. If the speed becomes too slow it will trigger red led.
THX. I used an additional state just for the measurement, otherwise the code got cluttered with mode stuff. When you have modal states, it's really just news that you need more states. That can be decided on a case by case basis.
I have the paths out of IDLE operating on the rising edge. The pushbutton held down at that time initiates a measurement cycle. Usually it's the real deal and we are checking the time and maybe going to the FAULT state. I have FAULT exit to IDKE when the button is seen.
The Xs with up and down arrows indicate transitions that are taken on the leading and trialing edges of the input signal.
Just out of scientific interest can you post a version without statemachine ?
My estimation is the non-statemachine-solution will be "overkill" with many flags and extra if-conditions