0

I can't use this program on my Arduino Uno since the max is 16 bits. I need my water pump to run for 5 min, then wait for 60 min.

int motorPin = A0; 
int blinkPin = 13;

int watertime = 300000; // how long to water in miliseconds
int waittime = 3600000; // how long to wait between watering

void setup() {
    pinMode(motorPin, OUTPUT);
    pinMode(blinkPin, OUTPUT);
}

void loop() {
    digitalWrite(motorPin, HIGH);
    digitalWrite(blinkPin, HIGH);
    delay(watertime);
    digitalWrite(motorPin, LOW);
    digitalWrite(blinkPin, LOW);
    delay(waittime);
}

Changed int to:

#define watertime 300000
#define waittime 3600000

But it does not work.

5

Use the long type instead

long watertime = 300000; // how long to water in miliseconds
long waittime = 3600000; // how long to wait between watering
1
  • 4
    You might want to use unsigned long instead of long and qualify your literals with UL on the end (eg 3600000UL).
    – Majenko
    May 13 '15 at 23:14
2

For any delays longer than a few seconds you really need to use a Real-Time Clock chip, like the popular DS1307 from Maxim. The Arduino's delay() function is only very roughly accurate, and even less so when used as you are using it.

If you can't get a DS1307 or something similar (there's plenty to choose from) or you Arduino has too much connected already to allow anything else to be added, you could use the Time library to simulate one. The Time library is also useful if you do have an RTC attached.

2
  • Eh. The Arduino libraries do actually compensate for the inaccuracies in delay() et alia, but a RTC is a better call regardless. May 13 '15 at 22:57
  • @IgnacioVazquez-Abrams They try to compensate for the inaccuracies, but when you're running from a ceramic oscillator there's only so much you can do - and any interrupts you have occurring will break that compensation anyway.
    – Majenko
    May 13 '15 at 22:58
0

I do not know if you plan on doing anything else on your arduino, but holding a delay inside the loop function is bad idea if you do, things need to happen outside of your code (any serial I/O for sure and others, right?).

That said, I like @zgrknr answer the best, although the others will also work. That answer allows using smaller numbers, that are easier to understand. Just make sure to keep your variables global, and not within loop or else they will get re-initialized every time.

As far as accuracy, do some real by the clock testing, and adjust your numbers, who cares if it is accurately counting as long as you can identify what numbers gives you the time period you want.

response to comment:

My firmware code looks like this, is yours different?

int main(void)
{
  init();
  initVariant();

#if defined(USBCON)
  USBDevice.attach();
#endif

  setup();

  for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
  }        
  return 0;
}
1
  • Serial communication with Arduino is interrupt-driven, and delay() does not block interrupts. May 13 '15 at 23:42
0

I believe it's better to put the Arduino to sleep instead of using the delay function.

You can use a library called Sleep_n0m1 for the purpose.

Modify your code like following.

#include <Sleep_n0m1.h>

int motorPin = A0; 
int blinkPin = 13;

unsigned long watertime = 300000UL; // how long to water in miliseconds
unsigned long waittime = 3600000UL; // how long to wait between watering
Sleep sleep;

void setup()
{
    pinMode(motorPin, OUTPUT);
    pinMode(blinkPin, OUTPUT);
}

void loop()
{
    digitalWrite(motorPin, HIGH);
    digitalWrite(blinkPin, HIGH);
    sleep.pwrDownMode();
    sleep.sleepDelay(watertime);
    digitalWrite(motorPin, LOW);
    digitalWrite(blinkPin, LOW);
    sleep.pwrDownMode();
    sleep.sleepDelay(waittime);
}

Hope this would solve the problem.

0

If did not misunderstand, you want a longer delay. Why don't you use a for loop in order to obtain such a delay.

for(int i = 0; i < 50; i++) {
    delay(1000);
}

This produces 50 seconds instead of 1 second. You can replace 1000 and 50 according to your needs. This is just an example.

1
  • Maybe because delay(50000) is simpler and does the same thing. Jan 9 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.