如何让 Arduino 每天在规定的时间执行一项任务? [英] How to make Arduino perform a task daily at the required time?

查看:32
本文介绍了如何让 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天全站免登陆