Delay Program Freaking Out

I’m trying to set up a program for my Arduino UNO to keep my plant watered. I have a servo that turns an arm to push a button that activated the water pump.

I’m very very new to coding. This is the code I’m using right now:

// Include the Servo library
#include <Servo.h>
// Declare the Servo pin`
int servoPin = 3;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 90 degrees 2hr 45 min
Servo1.write(90);
delay(2000);
Servo1.write(0);
delay(9900000);
Servo1.write(90);
delay(2000);
Servo1.write(0);
delay(9900000);

}

I tested the sketch with the delay set for 5 minutes to make sure it ran smoothly. I timed it for 2 cycles (4 times watering), and it worked fine. So I replaced the delay values with 9,900,000 milliseconds (2hr45min) and uploaded it. The servo activated the button successfully one time. Then, about 15 minutes later, for some reason the servo was activated again, pushing the button, and not releasing it, which kept the pump on and got water everywhere.

What’s happening with the program? Is this because I’m using delay()??

I’ve read it’s better to use the millis() function and I had a look at the BWOD thread, but I really can’t grasp how the program is working, and thus don’t know how to apply it to my need.

I want the servo to turn 90 degrees for 2 seconds every 2 hours and 45 minutes. How can I achieve this?

Thank you

I would avoid the magic numbers

#define ONE_SECOND 1000UL
#define ONE_MINUTE (ONE_SECOND * 60)
#define ONE_HOUR (ONE_MINUTE * 60)

and build up your code from that.

Also add some debug prints, to allow your sketch to tell you what it is doing.

Magic numbers? You’re saying I should define those values and then use them in the delay function? like 2HOUR + 45MINUTE?

And what are debug lines? How would I go about this?

Blink without delay

Read the first four pages: of this tutorial. Then use this example.

Jacques

larmagost:
Magic numbers? You're saying I should define those values and then use them in the delay function? like 2HOUR + 45MINUTE?

Yes. It is much easier to read and modify.

And what are debug lines? How would I go about this?

Using Serial.print at strategic points around your code.
Like Serial.println (F("I am about to do something significant"));

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

Serial.println (F("I am about to do something significant"));

I usually spell that last word "stupid" in MY code.

PaulS:

Serial.println (F("I am about to do something significant"));

I usually spell that last word "stupid" in MY code.

Me too. LOL

...R