[SOLVED] Alternatives to delay() - SimpleTimer isn't enough

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?

Despite the name, SimpleTimer is VERY powerful.

1 Like

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().

Yes all covered with SimpleTimer.

http://playground.arduino.cc/Code/SimpleTimer

Study the doc in detail and then come back if you are still struggling.

1 Like

To be precise, the

int setTimeout(long d, timer_callback f)

function lets you set up a one-off delay which calls a function of your choosing when it expires.

SimpleTimer is great.

2 Likes

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().

Try timer library, it contains function “after”
t.after(millis(),function_callback);
though, idk if it will work with blynk.

1 Like

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.

1 Like

The interval is run everytime it is called, so even though it is deleted, every time a function runs with the interval in it, the interval is run…

1 Like

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.

void doPump(){
  timer.setTimeout(1000, pumpOn);
}
1 Like

@zadspecial - so basically, any “alternatives” to simpleTimer to do what you want are not even worth discussing…

1 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.

BLYNK_WRITE(V1) {
if ( param.asInt() ) {
  digitalWrite(18, HIGH);
  delay(10);
  servo.write(255);
  delay(1500);
  digitalWrite(18, LOW);
} else {
  digitalWrite(18, HIGH);
  delay(10);
  servo.write(10);
  delay(1500);
  digitalWrite(18, LOW);
}

}

guys i want to remove delay function in this code. can anybody help me??
ps:-i don’t know coding

Copy my example a few posts up! Please try yourself first… and only ask for help if you really cant do it…

can you explain me how the simple timer works?

Simpletimer counts time in milliseconds and does things you tell it to do when a certain time is reached…

Nah man it would take me all day… any way… there are loads of good documents out there about it

http://playground.arduino.cc/Code/SimpleTimer

1 Like

can the millis function be used instead of this but doing the same thing as in code that is acctually written by you @Jamin

No, use SimpleTimer for everything.

@Sai_Khurana it’s easier to learn how to use SimpleTimer than trying to use millis().

1 Like