Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Soft/Hard RTC code simplified?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Soft/Hard RTC code simplified?

by fmbfla on Sat Nov 21, 2015 11:38 pm

Not sure if any one needs this but I got board tonight and did this for myself for a few projects. I kept getting mad because I had to always induce the "DateTime now = rtc.now();" every time I needed something to do with the Date or time. the footprint is not much bigger than the stock Softrtc and can be used either Hard or soft.
Make me happy.
:-)
Fmbfla
Code: Select all | TOGGLE FULL SIZE
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// With 12 hour conversion and Simplification
#include <Wire.h>
#include "RTClib.h"

char* month[] = {
  "null", "January,", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
char* weekDays[] = {
  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
// Below is so an Emic T2S mod will sound human
char* day[] = {
  "null", "First", "Second", "Third","Forth", "Fifth", "Sixth", "Seventh","Eighth", "Nineth", "Tenth", "Eleventh", "Twelfth","Thirteenth","Fourteenth","fifthteenth","Sixteenth","Seventeenth","EightTeenth","Nine Teenth","Twenty eth","Twenty first","Twenty Second","Twenty third","Twenty Forth","Twenty Fifth","Twenty Sixth","Twenty Seventh","Twenty Eighth","Twenty Ninth"," Thirty eth","Thirty First"};




int Year;
int Month;
int Day;
int Hour;
int Minute;
int Second;
int dayOfWeek;
//RTC_DS1307 rtc;  // Hardawre
RTC_Millis rtc;  // Software

void setup () {
  Serial.begin(115200);
  /*---------------HARDWARE RTC------------------------*/
  //  The Eight (8) Lines Below are for HARDWARE RTC
  /*  rtc.begin();  // Uncomment for HARDWARE RTC
      if (! rtc.isrunning()) {
      Serial.println("RTC is NOT running!");
      // following line sets the RTC to the date & time this sketch was compiled
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
      // This line sets the RTC with an explicit date & time, for example to set
      // January 21, 2014 at 3am you would call:
      // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
    }
  */
  /*---------------SOFTWARE RTC------------------------*/
  //  rtc.adjust(DateTime(__DATE__, __TIME__));  // Uncomment for HARDWARE RTC
  //  The Single Line below is for SOFTWARE DEFINED RTC
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));  // Uncomment for SOFTWARE RTC
  //rtc.begin(DateTime(2014, 1, 21, 3, 0, 0));  // Uncomment To set a Specific Date and Time using SOFTWARE RTC



}

void loop () {

  save_time();
  save_time_conversion();
  /*
  Serial.print("24 Hour Time: ");
  Serial.print(Hour);
  Serial.print(':');
  Serial.print(Minute);
  Serial.print(':');
  Serial.print(Second);
  Serial.print(' ');
  Serial.print("Date ");
  Serial.print(Year);
  Serial.print('/');
  Serial.print(Month);
  Serial.print('/');
  Serial.println(Day);
  */
  Serial.println();

  Serial.println();
  delay(3000);
}


void save_time() {
  DateTime now = rtc.now(); // never needs to be called upon again
  Year = now.year();
  Month = now.month();
  Day = now.day();
  Hour = now.hour();
  Minute = now.minute();
  Second = now.second();
  dayOfWeek = now.dayOfWeek();

}
void save_time_conversion() {
  //Serial.print("Date ");
  Serial.print(dayOfWeek[weekDays]);
  Serial.print(' ');
  Serial.print(Month[month]);
  Serial.print(' ');
  Serial.print(Day);
  Serial.print(',');
  Serial.print(' ');
  Serial.println(Year);
  // Save_time with the Twelve Hour conversion added
  //Serial.print("12 Hour Time: ");
  if (Hour < 10) {              // Zero padding if value less than 10 ie."09" instead of "9"
    Serial.print(" ");
    Serial.print((Hour > 12) ? Hour - 12 : ((Hour == 0) ? 12 : Hour), DEC); // Conversion to AM/PM
  }
  else {
    Serial.print((Hour > 12) ? Hour - 12 : ((Hour == 0) ? 12 : Hour), DEC); // Conversion to AM/PM
  }
  Serial.print(':');
  if (Minute < 10) {       // Zero padding if value less than 10 ie."09" instead of "9"
    Serial.print("0");
    Serial.print(Minute, DEC);
  }
  else {
    Serial.print(Minute, DEC);
  }
  Serial.print(':');
  if (Second < 10) {       // Zero padding if value less than 10 ie."09" instead of "9"
    Serial.print("0");
    Serial.print(Second, DEC);
  }
  else {
    Serial.print(Second, DEC);
  }
  if (Hour < 12) {                // Adding the AM/PM sufffix
    Serial.print(" AM ");
  }
  else {
    Serial.print(" PM ");
  }

}


fmbfla
 
Posts: 109
Joined: Fri Jun 08, 2012 6:48 pm

Re: Soft/Hard RTC code simplified?

by adafruit_support_bill on Mon Nov 23, 2015 9:07 am

Nice! Thanks for posting.

adafruit_support_bill
 
Posts: 77937
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.