2017-09-14 176 views
0

我正在嘗試創建一個計時器,它提醒我在其他日子會做某件事,但是看起來我的DS3231模塊選項允許我設置一個小時或一週中的某一天。不幸的是,這兩種選擇都不夠好(如果一週只有8天),而且我不知道如何告訴它隔天開火。使用DS3231 RTC模塊和Arduino每隔一天觸發一次功能

我認爲設置'not_today'變量的想法很可能會起作用,但是依賴於arduino而不是RTC,這意味着如果它失去了電源,那麼這個變量就會重置。

它看起來像DS3231的報警功能並不真的有這個選項,任何基於代碼的解決方案將有上述的功率損失問題。

有沒有一個RTC模塊可以做我需要它做的事情?

+0

是否有一個原因,你不能使用eeprom來存儲某些狀態來跳轉限制?似乎會避開電力損失問題。 – RamblinRose

+0

我確定沒有理由。我只是對這些東西不夠了解,不知道我能做到。謝謝,這可能是完美的解決方案:) –

回答

0

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