每隔一天使用DS3231 RTC模块和Arduino启动功能

时间:2017-09-14 09:41:59

标签: arduino

我试图创建一个计时器,提醒我有一天做某事,但似乎我使用DS3231模块的选项允许我设置一周中的一小时或一天。不幸的是,这两种选择都不够好(如果一周只有8天),而且我不知道如何告诉它每隔一天发射一次。

我考虑过设置一个¬今天'可能会起作用的变量但是依赖于arduino而不是RTC的缺点,这意味着如果它失去了电源,那么该变量将重置。

看起来DS3231的报警功能确实不具备此选项,任何基于代码的解决方案都会出现上述功耗问题。

是否有RTC模块可以做我需要做的事情?

1 个答案:

答案 0 :(得分:0)

用户@RamblinRose向我指出了EEPROM写入和读取的方向,我不知道这是可能的。这解决了我的问题。以下是来到这里并希望得到更全面答案的其他人的完整代码:

#include <DS3231.h>
#include <EEPROM.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

Time t;

bool active = false;
void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // 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(14, 9, 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(500);              // wait for a second
      digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
      delay(500);  
      buttonCheck();
    }
  }
}

int buttonCheck() {
  if(digitalRead(11) == 0) {
    active = false;
    blinker(0);
  }
}


void loop()
{
  t = rtc.getTime();
  int hour = t.hour;
  int min = t.min;
  int sec = t.sec;

//  //  Send time to serial monitor (for debugging)
//    Serial.print(hour);
//    Serial.print(":");
//    Serial.print(min);
//    Serial.println(" ");

  // Put there different timers in here for demo purposes
  // Set activation time
  if(hour == 13 && min == 31 && sec == 00) {
    if(EEPROM.read(0) == 0) {
      active = true;  
      EEPROM.write(0, 1);
      Serial.println("Not run recently, activating");
    } else {
      active = false;  
      EEPROM.write(0, 0);
      Serial.println("Run recently, skipping this one");
    }
  }

  if(hour == 13 && min == 35 && sec == 00) {
    if(EEPROM.read(0) == 0) {
      active = true;  
      EEPROM.write(0, 1);
      Serial.println("Not run recently, activating");
    } else {
      active = false;
      EEPROM.write(0, 0);
      Serial.println("Run recently, skipping this one");
    }
  }

  if(hour == 13 && min == 45  && sec == 00) {
    if(EEPROM.read(0) == 0) {
      active = true;  
      EEPROM.write(0, 1);
      Serial.println("Not run recently, activating");
    } else {
      active = false;
      EEPROM.write(0, 0);
      Serial.println("Run recently, skipping this one");
    }
  }

  blinker();
  delay(1000);  
}