我有一个Arduino Pro Mini 5v,16 mhz,它连接到引脚2上的数字开关。此开关用于通过外部数字中断将Arduino从睡眠中唤醒。我还有一个连接到引脚9的DHT11温度传感器。我想要达到的是当Arduino唤醒5秒钟并且引脚2上的开关为高电平时,我想读取温度传感器并返回温度。我正在使用Tillart的DHT11库,当我这样做时,它会返回一个TIME_OUT错误。我唯一可能的解释是,当DHT11和引脚2上的开关一起读取时,电压会发生改变?任何指向解决方案的指针都将不胜感激。谢谢。外部数字中断和dht11
编辑1:添加的代码
#include <LowPower.h>
#include <dht.h>
int pin2 = 2;
dht DHT;
#define DHT11_PIN 9
void pin2interrupt(void)
{
// Function called when awoken from sleep
// Detach interrupt to stop it from continuosly firing when in normal mode
}
void enterSleep(void)
{
attachInterrupt(0, pin2interrupt, HIGH);
Serial.println("Sleeping");
delay(100);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
Serial.println("Awake!");
}
void setup()
{
Serial.begin(115200);
pinMode(pin2, INPUT);
pinMode(DHT11_PIN, INPUT);
}
int seconds = 0;
void loop()
{
delay(1000);
seconds++;
Serial.println("Awake in the loop!");
Serial.println(seconds);
if (digitalRead(pin2) == LOW && seconds == 5)
{
seconds = 0;
Serial.println("No child detected, so going to sleep!");
delay(200);
enterSleep();
}
else if (seconds == 5)
{
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.println(DHT.temperature, 1);
delay(2000);
seconds = 0;
}
}
编辑2:我也忘了提,我使用的是低功耗库通过RocketScream把Arduino的睡觉。这个库可以在这里找到:https://github.com/rocketscream/Low-Power
@PatrickTrentin对不起。添加了代码。希望它的格式正确。 – Jigmeister
你会尝试改变attachInterrupt(0,pin2interrupt,HIGH);'attachInterrupt(0,pin2interrupt,RISING);'看看会发生什么?请将'seconds'重命名为'loop_iterations'。并用'seconds> = 5'替换'seconds == 5'。 –
@PatrickTrentin感谢您花时间给我提出您的建议。我会尝试并报告结果。 – Jigmeister