How to make Arduino perform a task daily at the re

2019-07-23 08:35发布

问题:

I am a student, and just newbie to Arduino. I am trying to make an automatic plant watering system which should water the plants twice a day.Is there anyway to make the Arduino to perform the task exactly at the required time daily, and then set itself to sleep mode?

回答1:

exactly at the required time daily

  • If your Arduino is clocked on internal RC, you won't have enough precision (1%). Your clock will derivate from about 7hours after 1 month.

  • If you need to have a (very) good precision you may use a RTC module (2ppm). Your clock will derivate from about 5 seconds after 1 month.

  • Or you may simply use the millis() function that should be precise enough on Xtal oscillator (200ppm). Your clock will derivate from about 10 minutes after 1 month.

I would start with the last solution as it requires no additional components and improve with RTC is needed.

and then set itself to sleep mode

The AVR core has different level of sleep, some will maintain the clock (idle) and should be used with the millis() solution and some will not maintain clock (power down) but are more power efficient and could be used with RTC. The solution depends on how low power you need to be. Note that maximum low power won't be achieved with an Arduino board and IDE because of the power regulator and other components. To achieve the 200nA sleep described in Atmega328 datasheet it will require some work.

millis() example

#define INTERVAL_1_DAY 86400000  // 1day => 24*60*60*1000

unsigned long nextDate = INTERVAL_1_DAY;  

void loop()
{
    unsigned long currentDate = millis(); //millis rollover (overflow) after about 50 days

    if(currentDate > nextDate  // time elapsed, do action
       && currentDate < (nextDate + INTERVAL_25_DAY)) //treatement of the overflow of millis() and *Dates ...
    {
        nextDate += INTERVAL_1_DAY;  //you have to use nextDate here and not current date like in some examples to have no sweep (some µs each day)

        // do your action here
    }

    // you may add some idle sleep here 
    // 10s sleep would give a execution date glitch e.g. [3pm to 3pm+10s]
    // but some code can fix this
}


标签: arduino