Hey guys, i have a loop that i want to take 5 seconds to run.
In this loop there are a couple of variable dependent delay's.
I want to add another variable delay that precisely delays to have the loop take 5 seconds.
SonofKingRagnar:
I have a bunch of things i want in total to take 5 seconds.
You will have to explain that in a lot more detail.
Do you want to slow something down so it takes a full 5 seconds?
Do you want to make sure that things all finish within 5 seconds?
Is there something that takes more than 5 seconds and you want to speed it up?
Tell us what your project is and you will get much better advice.
Some more details:
I have an experiment. in a fish tank i want to measure and control the pH and oxygen content. When the pH gets out of range i turn on a pump to pump acid or base. the delay i have for that is variable depending on how much the pH is out of range and the concentration of my stock solutions. The pH probe needs some time to get a new equilibrium. I could add a simple 2 sec delay but then my data collection (serial.print) has random time intervals. I want to have this complete loop to take 5 seconds to have the pH probe behave normally and have regular data points.
@Paul, Maybe I interpreted the problem/solution wrongly. It does not matter if the loop is long or short. My apologies to you for not having a good question. I do think your solution might work, it didnt yet but i did not have much time today to trouble shoot that.
@Robin, In summary I want to slow the loop down so it takes a full 5 seconds?
SonofKingRagnar:
I could add a simple 2 sec delay but then my data collection (serial.print) has random time intervals. I want to have this complete loop to take 5 seconds to have the pH probe behave normally and have regular data points.
Have a look at how millis() is used to manage timing in Several things at a time
It is perfectly possible to manage your PH measurements and your data collection with two completely separate timing schemes.
void loop() {
unsigned long starttime = millis();
measure_o2(); // takes random amount of time between 0 and 1s
measure_ph(); // takes random amount of time between 0 and 1s
if (ph_high) {
lower_ph(); // if ph is high, take steps to lower it. 1 to three seconds
}
// Delay until the next 5s tick...
while (millis() - starttime < 5000) {
// spin and do nothing
}
}