【发布时间】: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 分钟的睡眠?