【问题标题】:Trying to get function to run (and stay running) when triggered by an RTC module尝试在由 RTC 模块触发时运行(并保持运行)功能
【发布时间】: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


【解决方案1】:

感谢@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); 
}

【讨论】:

  • 您可以阅读更多关于 here 的信息,了解为什么会发生这种情况以及它的好处。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 2016-01-21
  • 2022-06-13
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多