【问题标题】:How can i sleep my arduino for 12 minutes?我怎样才能让我的 arduino 睡 12 分钟?
【发布时间】:2021-11-30 23:20:20
【问题描述】:

所以我已经生成了这段代码,但我似乎无法在我想要的时间里睡觉我的 Arduino Uno。我需要 Arduino Uno 睡 12 分钟,醒来并在 3 分钟后读取读数(我的传感器的预热时间),然后读取 5 分钟。我的起床时间和阅读时间是正确的,只是睡眠时间似乎无法正确。

如下图所示,我已经设法使用const int time_interval = 2000 ; 将其睡眠时间提高到 12 分钟左右,但它每次睡眠时都会发生变化,似乎每次都增加一分钟..

有人可以帮忙吗?这些是控制时间的代码的sn-ps,后面是完整的代码:

const unsigned long eventInterval = (1000UL*60*3);   //Assign the time in millis for event interval (every 15 minutes) 
unsigned long startTime =  millis()+ eventInterval;
unsigned long readingInterval= (1000UL*60*5);             //Assign the time in millis for reading (every 5 minutes) 

//SD stack select
const int chipSelect = 10;
//const int pingPin = 7;


//RTC Module Global Variables
const int time_interval = 2000 ;  // Sets the wakeup interval in minutes. 

....和......

    /* This is the event */
 if ((long)(millis()-startTime)>=0) {


  delay(10);
  unsigned long i=millis();
  while ((long)((i+readingInterval)-millis())>=0){
  //Get the values of the sensors
  GetRTC_Readings();
  GetVOC_Readings();
  GetPM_Readings();
  GetBME_Readings();
  delay (5000); // to give time for PMS to take readings?

  // Store the three sensor values to the SD card.
  print2SD();
  delay(5000);
  } 
   
  digitalWrite(indicator,HIGH);
  delay(2000); 
  digitalWrite(indicator,LOW);
  Serial.println("Going to sleep for 12 minutes");
  digitalWrite(Relay_pin,LOW);
  digitalWrite(Relay_pin2,LOW);
 
  delay(100);
  for(int i = 0; i<= 6*time_interval; i++) //this is sleeping arduino for 12 minutes
  {
  LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,SPI_OFF, USART0_OFF, TWI_OFF);
  }
  delay(1000);
  startTime = millis()+ eventInterval; //this is what affects the time
  Serial.println("Arduino: Hey I just Woke up");
  Serial.println("");
  delay(1000);
  
 }
}

【问题讨论】:

  • 有很多代码要通读!你能提供一个最小的例子吗(stackoverflow.com/help/minimal-reproducible-example
  • 为什么你的评论说//this is sleeping arduino for 24 seconds,此时代码让arduino 休眠12 分钟减去24 秒?为什么是负 24 秒?为什么要循环 6 * 12 而不是循环 (60 * 12) / 8 次以睡眠(大约)12 分钟?
  • 嘿 SamBob,我编辑了代码以仅显示重要的部分。到目前为止,我对这段代码有帮助,我仍然是初学者,所以仍在努力理解它。 “睡眠 24 秒”是我忘记从测试中删除的评论,我的错。
  • 你会在哪里以及如何放置这个 (60 * 12) 的循环,以及你如何让它循环 8 次以获得(大约)12 分钟的睡眠?

标签: c++ arduino sensors


【解决方案1】:

您应该始终在更改代码时更新代码中的 cmets。

对于解决方案,您应该更改将电路板置于空闲模式的 for 循环。 代码:LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,SPI_OFF, USART0_OFF, TWI_OFF); 将使您的开发板空闲 8 秒 + 开发板需要唤醒的时间。所以你应该改变for循环time_interval。从您发布的代码中,您仅将 time_interval 用于 for 循环,因此您可以在初始化时将其设置为 12 分钟,而不是在其中计算它:const int time_interval = 12 * 60; // 12 minute wake up interval in seconds .

for 循环的开始是这样的:for(int i = 0; i &lt; time_interval / 8; i++) 除以 8 可以在初始化时完成,但更清楚的是将它放在 for 循环中,即每轮将电路板闲置 8 秒。

如果您的时间需要更精确,您需要考虑代码中的唤醒时间和延迟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2021-05-27
    相关资源
    最近更新 更多