【发布时间】:2017-09-14 17:02:02
【问题描述】:
所以我写了一些代码,我想在每天的时间达到某个点时开始闪烁 LED。 LED 应继续闪烁,直到按下按钮,但由于某种原因,LED 会自行停止闪烁,通常在大约 1 分钟后。
我不确定引脚 11 是否被随机激活,或者它只是在定时器条件结束后没有打开灯。你们知道我哪里出错了吗?
#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
bool active = false;
void setup()
{
// Setup Serial connection
Serial.begin(115200);
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}
// Initialize the rtc object
rtc.begin();
pinMode(12, OUTPUT);
pinMode(11, INPUT);
// The following lines can be uncommented to set the date and time
// rtc.setDOW(MONDAY); // Set Day-of-Week to SUNDAY
// rtc.setTime(15, 51, 0); // Set the time to 12:00:00 (24hr format)
// rtc.setDate(9, 11, 2017); // Set the date to January 1st, 2014
}
void loop()
{
int hour = rtc.getTime().hour;
int min = rtc.getTime().min;
// Send time
Serial.print(hour);
Serial.print(":");
Serial.print(min);
Serial.println(" ");
if(hour == 10 && min == 38) {
active = true;
}
blinker();
}
void blinker() {
while(active == true) {
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000);
buttonCheck();
}
}
void buttonCheck() {
if(digitalRead(11) == 1) {
Serial.println("Button pushed");
active = false;
}
}
【问题讨论】:
-
我的第一个问题是,您的按钮使用的电路是什么?它是否使用 PULL UP 或 PULL DOWN 电阻?
-
另外值得考虑的是使用定时器中断并让 Arduino 在其余时间休眠。
-
@sma 我对电子产品很陌生,我没有意识到使用带有微控制器的电阻器会有好处,因此电路中没有电阻器。它只是一个 arduino nano、一个 DS3231 RTC 控制器、一个白色 LED 和一个按钮。
-
@Danny_ds 不知道这是什么意思,所以我给它一个谷歌,谢谢:
-
@Danny_ds 啊!这可能行得通,但这意味着我必须找到另一种方法让灯闪烁。也许我可以用一个电容器来做到这一点。
标签: arduino