I've been at whits end for 3 solid days researching just this and still can't seem to crack this nut open. I've tried many different things, but what I have now produces the closest to what I am trying to accomplish. Essentially, I am trying to gather the readings collected in;
now.hour();
now.minute(); &
now.second();
and have them populate a value in a char array, but also to have the colons ( in between each element/value. The purpose for this is because I am trying to merge me sketch with Blynk so that I can send my sensor values sent downstream to an app on my smartphone. In order for me to do that, in the case of my RTC, all the data needs to be collected into a string or String (if need be). Once done, the entire string can be sent over to Blynk and the data readings in the string will easily be displayed in a widget in the app.
The code is a stripped down version of ds1307 example in the RTClib library. I created a few other variables to create a 12 hour display. As I am a novice at coding, can someone suggest the best possible means to achieve my goal? Like, should I not be using char arrays in favor of say, Strings or possibly char*? Thank you in advance!
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
byte (displayHour);
byte (MIN) = now.minute();
byte (SEC) = now.second();
//char* meridian[]{"AM", "PM"};
if (now.hour() == 0) // First we test if the hour reads "0"
{
displayHour = zeroHour;
//meridian[] = "AM";
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
displayHour = twelveHour;
//meridian[] = "PM";
}
else
{
displayHour = now.hour();
//meridian[] = "AM";
}
char timeStamp[6];
sprintf(timeStamp, "%02d:%02d:%02d", displayHour, MIN, SEC);
//char dateStamp[6] = {now.month(), '/', now.day(), '/', now.year()};
{
Serial.println(timeStamp);
//Serial.print(dateStamp);
}
delay(3000);
}
You're trying to squeeze (at least) 8 + 1 characters in an array that only has space for 5 plus 1 characters. The '1' is for the terminating '\0'. Change the size of timeStamp to 9.
The round brace variables are my attempt to "try something" to see if there was something that I overlooked. I thought maybe my variables were not setup properly to receive the values of the RTC readings, so I changed byte MIN; to byte (MIN);
same end result...
I will change the 6 to a 9, thanks for pointing that out!!!
The sprintf() function is very powerful, but most applications only use a small fraction of that power yet all of it is yanked into the program. The program above compiled to 6130 bytes for me. Making this modification:
which uses a few of the standard library str*() functions to shrink the code size to 4890 bytes. True, it's a few more lines, but if memory gets tight, it can be a help.
Perhaps it is for another unknown reason, but the output on the serial monitor produced the same result for the sprintf method and the econjack method; 153:165:85. There must be some other reason that the time is being displayed this way. Is it possibly that the RTC outputs a byte and both methods involve the use of char? I am still too new to understand the variances and am trying to learn these elements of code through study and application.
I did run the ds1307 example sketch and it did output the correct time. From there I stripped out all of the non essential elements such as, "day of week", (! rtc.begin), and days since 1970 and just focused the changes on now.hour/minute/second(); and now.year/month/day();, but I have the year/month/day commented out till I can get a printable string out of the first 3. Here is that example for reference if needed.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#if defined(ARDUINO_ARCH_SAMD) // for Zero, output on USB Serial console
#define Serial SerialUSB
#endif
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
while (!Serial); // for Leonardo/Micro/Zero
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
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));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
THANK YOU!!! rtc.begin did the trick. Now I just gotta figure out why the same method for the month/day/year produces a screwy result. That aside though, I am very greatful to have been taken this far. Thanks again!
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
byte displayHour;
byte MIN = now.minute();
byte SEC = now.second();
//char* meridian[]{"AM", "PM"};
if (now.hour() == 0) // First we test if the hour reads "0"
{
displayHour = zeroHour;
//meridian = "AM";
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
displayHour = twelveHour;
//meridian = "PM";
}
else
{
displayHour = now.hour();
//meridian = "AM";
}
char timeStamp[9];
char dateStamp[9];
sprintf(timeStamp, "%02d:%02d:%02d", displayHour, MIN, SEC);
//sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
{
Serial.println(timeStamp);
//Serial.println(dateStamp);
}
delay(3000);
}
Thank you once again cattledog for setting me straight!
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
byte displayHour;
byte MIN = now.minute();
byte SEC = now.second();
char* meridian[6];
meridian[0] = "AM";
meridian[1] = "PM";
if (now.hour() == 0) // First we test if the hour reads "0"
{
displayHour = zeroHour;
meridian[0];
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
displayHour = twelveHour;
meridian[1];
}
else
{
displayHour = now.hour();
meridian[0];
}
char timeStamp[12];
char dateStamp[11];
sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
{
Serial.println(timeStamp);
Serial.println(dateStamp);
}
delay(3000);
}