I'm trying to execute two types of function based on Bluetooth selection.
E.G selection 10 and 20 both integer type.
When int 10 is received it will loop through the function 1 time for 20s and produce a value .
My question is how do i return to
"selection = ESP_BT.read();" at line 65. When I insert a "return;" at the end my code just stop checking for new value.
Here are the things i've tried
1.I've tried switch case but it wont loop through the heart rate and SpO2 calculation unless a while(1) is inserted which I can't exit from.
2. changed while to "if" again it wont loop through unless a while(1) is inserted
in general, loop() needs to do things that are non-block such as monitoring for input and executing some non-blocking operation.
the input can set a mode indicating what operation(s) are in effect. and set mode to something like IDLE or NONE when the operation is completed
operations can be invoked repeatedly if they need to monitor something. just like serial input checking if available(), checking for something using an "if" instead of a "while".
if the operation is time based, it can use millis() instead of delay to check if some time has expired to perform some action
when I changed it to if (selection ==10) my code stops looping the calculating function inside is there a better way of structing?
I'm using millis() as time as well I'm having trouble resetting it back to zero .
My main goal is to measure both SpO2 and BPM at an interval of 10mins. I know if I used delay it would cause of drift in time and overflow will occur if I don't reset the time. The currentmillis act as a guild line for how long the sensor will be measuring its data from which is 20s.
But to measure elapsed time you do not need to set it to zero. Instead save the value of millis() when the period starts and compare it to the current value of millis() at any time to determine how much time has passed
then it's only going to execute when there's something available.
there needs to be a condition statement (e.g. switch) outside the if (ESP_BT.available()). that switch can execute the appropriate operation
the timestamp can be a global variable (so that it's value is not lost between iterations of loop()) and capture at the begin of the period you want to delay for or at the time of each execution to delay again
i don't see where you initially set "premillis" (why not call is msecLst) and would reset it immediately after the time expires