I’m building an automated gardening system. If you put three plant pots (attached with moisture sensors) in front of it, it will determine which one needs water and rotate the water pipe to that direction using a servo; then it will turn the pump on.
So I have two functions: pumpOn() and turnLeft(). Here’s what I want to do:
turnLeft();
delay(1000);
pumpOn();
How can I do it without using delay()? I can’t implement the SimpleTimer library here: it doesn’t have the function.
It’d be better to just write a function like void customDelay(int time, void *functionToBeDelayed) or something so that I can use it everywhere. How do I write it?
I’ve updated the question. Do you know how I can implement it here? I don’t want to run pumpOn() and turnLeft() separately. I want a delay before running pumpOn().
But after ‘f has been called, the interval is deleted, therefore the value timerId is no longer valid’.
This means, I won’t be able to call pumpOn() again. That’s not what I want.
I want it to always run when the loop runs. I just want a delay in between turnLeft() and pumpOn().
It doesn’t mean you won’t be able to call pumpOn() again. pumpOn() is a function which you have defined and written, so you can call it whenever you like. timerId is just a variable you can use to refer to a specific timer, so you can do things like check whether a certain timer is still running, reset it, or delete it. You don’t even need to use timerIds in the simplest case; if you write this line
setTimeout(1000, pumpOn);
That will cause pumpOn() to be called 1000 ms later.
Again, read the SimpleTimer documentation. It provides all the functionality you’re asking for.
I think you need to have a play iwth SimpleTimer untill you realised it actually a very AdvancedTimer (see what I did there?)
You only need to set up a pumpOn() function with a timer. Very easy to do.
void pumpOn(){
// do you pump on functions here
}
Then you can just call timer.setTimeout(1000, pumpOn); when ever you need to turn the pump on 1sec later.
Want to turn it off after XX seconds? Easy:
void pumpOn(){
// do you pump on functions here
timer.setTimeout(3600, pumpOff); // turn off after 1min
}
void pumpOff(){
// do you pump off functions here
}
And if you’re a bit OCD about keeping things tidy then just add this function and call doPump() where ever you like.
I was putting the timer.setTimeout() function inside the void setup() function instead of void loop() like it is usually done with timer.setInterval(). My problem is solved. The code runs flawlessly now. Thanks a lot, everyone.