所以我写了一些代码,当每天的时间到达某一点时,我想开始闪烁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;
}
}
答案 0 :(得分:0)
感谢@sma +一些代码调整,基本上我将引脚11从INPUT转换为INPUT_PULLUP并重新编写代码来拦截
#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_PULLUP);
// 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
}
int blinker(int state = 1) {
if(state == 1) {
if (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();
}
}
}
int buttonCheck() {
Serial.println(digitalRead(11));
if(digitalRead(11) == 0) {
Serial.println("Button pushed");
active = false;
blinker(0);
}
}
void loop()
{
int hour = rtc.getTime().hour;
int min = rtc.getTime().min;
// Send time
Serial.print(hour);
Serial.print(":");
Serial.print(min);
Serial.println(" ");
Serial.println(digitalRead(11));
if(hour == 9 && min == 59) {
active = true;
}
blinker();
delay(1000);
}