I want to make it so that when certain channel reaches X value (sensor senses whatever) it will trigger a function, and then when that value reaches certain treshold again (sensor senses whatever again), it will stop doing the loop.
You can use loop() to do repeated actions and within loop, trigger which actions to perform.
You can trigger by elapsed time, sensor input, serial input, and whatever else you can get to run. XD
You can use a process state approach and use 1 or more state variable(s) that trigger one process that when it's done, it sets the state to trigger the next step in the process.
So you have state changes
- when certain channel reaches X value
- when that value reaches certain treshold again
So states:
State 10 (no rule says start anywhere) is where you watch for X and when you get X you change the process state to 20.
State 20 is where you watch for the certain threshold and if it's not then give the matching function a turn of the handle but no more, do not block or hog cycles unless it's vital, state 20 will likely run again in microseconds.
The matching function should be written like the inside of a loop, it gets called repeatedly during its duty cycle.
If there's anywhere in that that needs to wait on time or event, make sure it doesn't block or hog.
Is that general enough?