如何使Arduino在需要的时间每天执行任务? [英] How to make Arduino perform a task daily at the required time?

查看:891
本文介绍了如何使Arduino在需要的时间每天执行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学生,并且是Arduino的新手.我正在尝试制作一个自动植物浇水系统,该系统每天要给植物浇水两次.无论如何,Arduino是否可以每天在所需的时间准确地执行任务,然后使其进入睡眠状态模式?

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?

推荐答案

恰好在每天的规定时间

exactly at the required time daily

  • 如果您的Arduino是使用内部RC时钟的,则您的精度将不足(1%).您的时钟将从1个月后的大约7小时开始计算.

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

      如果需要(非常)好的精度,则可以使用RTC模块(2ppm). 您的时钟将在1个月后的大约5秒后得出.

      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.

      或者您可以简单地使用millis()函数,该函数在Xtal振荡器(200ppm)上应该足够精确. 您的时钟将在1个月后的10分钟左右得出.

      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.

      我将从最后一个解决方案开始,因为它不需要其他组件,并且需要使用RTC进行改进.

      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

      AVR内核具有不同的睡眠级别,有些将保持时钟(空闲),应与millis()解决方案一起使用,而有些将不保持时钟(掉电),但更节能,可以与RTC.解决方案取决于您需要多低的功率.请注意,由于功率调节器和其他组件的原因,Arduino板和IDE无法实现最大的低功耗.要实现Atmega328数据表中所述的200nA睡眠,将需要做一些工作.

      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()示例

      #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在需要的时间每天执行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆