Naguissa / uRTCLib

Really tiny library to basic RTC functionality on Arduino. DS1307, DS3231 and DS3232 RTCs are supported.
https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html
GNU Lesser General Public License v3.0
88 stars 24 forks source link

an example which shows a little bit more #28

Open StefanL38 opened 1 year ago

StefanL38 commented 1 year ago

Hi Naguissa,

recently I posted an example-code on the Arduino-Forum which goes a little bit beyond the most basic example with direct function-calls and direct number use.

My intention is to show how well structured coding is done and to show how to code conditions based on time I included another function called TimePeriodIsOver() which does non-blocking timing based on function millis(). IMHO this function is way more intuitive than the blink_without_understand-example of the arduino-IDE

If you like you can add the example to the GiPo

/*structure (= function-call hierachry) of the code
 * setup()
 *  - calls setRTC_Time()
 *  
 * loop() 
 *  - calls UpDateTime()
            - calls rtc.year() etc.
    - calls TimePeriodIsOver()
       - conditionally calls PrintTime() 
*/

#include "Arduino.h"
#include "uRTCLib.h"

// uRTCLib rtc;
uRTCLib rtc(0x68);

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int myYear;
int myMonth;
int myDay;
int myDayOfWeek;
int myHour;
int myMinute;
int mySecond;
int minuteOfDay;
// 23*3600 + 59*60 + 59 = 86399 
// which is a to big number for int
long secondsOfDay; 

int MinOfDay_9OClock = 9 * 60; // 9 hours * 60 = 540 minutes 

unsigned long myTimer;

void setup() {
  Serial.begin(9600);
  delay(3000); // wait for console opening

  URTCLIB_WIRE.begin();
  setRTC_Time();
}

void loop() {
  UpDateTime();
  // check if more than 1000 milliseconds have passed by
  // since last time the 1000-ms-period-was-over
  if ( TimePeriodIsOver(myTimer,1000) ) {
    // if REALLY 1000 milliseconds have passed by 
    PrintTime();
  }
}

void setRTC_Time() {
  // Comment out below line once you set the date & time.
  // Following line sets the RTC with an explicit date & time
  // for example to set January 13 2022 at 12:56 you would call:
  myYear  = 22;
  myMonth = 1;
  myDay   = 13;
  myDayOfWeek = 7;
  myHour   = 23;
  myMinute = 59;
  mySecond = 50;
  // using variables makes comments obsolete
  rtc.set(mySecond, myMinute, myHour, myDayOfWeek, myDay, myMonth, myYear) ;
  // set day of week (1=Sunday, 7=Saturday)  
}

void UpDateTime() {  
  rtc.refresh();

  myYear  = rtc.year();
  myMonth = rtc.month();
  myDay   = rtc.day();

  myDayOfWeek = rtc.dayOfWeek() - 1;

  myHour   = rtc.hour();
  myMinute = rtc.minute();
  mySecond = rtc.second();

  minuteOfDay  = myHour *   60 + myMinute;
  secondsOfDay = myHour * 3600 + myMinute * 60 + mySecond;
}

void PrintTime() {
  Serial.print("Current Date & Time: ");
  Serial.print(myYear);
  Serial.print('/');
  Serial.print(myMonth);
  Serial.print('/');
  Serial.print(myDay);

  Serial.print(" (");
  Serial.print(daysOfTheWeek[myDayOfWeek]);
  Serial.print(") ");

  Serial.print(myHour);
  Serial.print(':');
  Serial.print(myMinute);
  Serial.print(':');
  Serial.println();

  Serial.print("minuteOfDay:");
  Serial.print(minuteOfDay);

  Serial.print("  secondsOfDay:");
  Serial.println(secondsOfDay);

  if (minuteOfDay < MinOfDay_9OClock) {
    Serial.println("it is before 9 o' clock");
  }
  else {
    Serial.println("it is past 9 o' clock");    
  }
}

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}
Naguissa commented 1 year ago

Thanks, I'll add it soon!