Oh dear, it's even worse than I thought. I really would like the time to be in AM/PM format, not 24 hour format. I'd normally do this with some long homemade code like this:
boolean PMflag = false;
int theHour = hour();
if(theHour == 0) { //12 AM
theHour = 12;
PMflag = false; //it's AM
}
else if (theHour == 12) { //noon
//leave theHour as it is
PMflag = true;
}
else if (theHour > 12) { //it's afternoon
theHour = theHour - 12;
PMflag = true;
}
but now to get all this together, as well as the possible extra '0' if the minute is less than 10, in one string as String currentTime that I can pass to the Blynk app?
This sounds great! I'll try it out. I don't understand the sprintf function at all (I'm arduino self taught, no schooling in actual C). So I guess I can replace the line: Serialprintln(buf) with.
Blynk.virtualWrite(V1, buf);
First of all, dump the String class. It will cause you nothing but problems down the road. While this may seem harder, it's not:
char timeString[10];
int hours;
int minutes;
hours = hour();
strcpy(timeString, itoa(hours); // Convert hours to a string and copy it to the buffer
strcat(timeString, ":");
minutes = minute();
if (minutes < 10) {
strcat(timeString, "0");
}
strcat(timeString, itoa(minutes));
if (hours > 12) { // Adjust if military time
strcat(timeString, " PM");
} else {
strcat(timeString, " AM");
}
Serial.println(timeString);
The String class is a crutch, so stick with the C string functions shown here. The sprintf() function will work, but it's an H-bomb to kill an ant. Compare code size for yourself, using sprintf() and the version shown here. My guess is that this version is at least 1K smaller.
SouthernAtHeart:
This sounds great! I'll try it out. I don't understand the sprintf function at all (I'm arduino self taught, no schooling in actual C).
That's no excuse. Google sprintf or his ugly cousin printf for more information.
econjack:
First of all, dump the String class. It will cause you nothing but problems down the road. While this may seem harder, it's not:
char timeString[10];
int hours;
int minutes;
hours = hour();
strcpy(timeString, itoa(hours); // Convert hours to a string and copy it to the buffer
strcat(timeString, ":");
minutes = minute();
if (minutes < 10) {
strcat(timeString, "0");
}
strcat(timeString, itoa(minutes));
if (hours > 12) { // Adjust if military time
strcat(timeString, " PM");
} else {
strcat(timeString, " AM");
}
Serial.println(timeString);
The String class is a crutch, so stick with the C string functions shown [here](https://www.tutorialspoint.com/c_standard_library/string_h.htm). The *sprintf()* function will work, but it's an H-bomb to kill an ant. Compare code size for yourself, using *sprintf()* and the version shown here. My guess is that this version is at least 1K smaller.
I'd like to try this, but it doesn't compile. An error "too few arguments to function 'char* itoa(int, char*, int)'"
LOL, I did. found a similar page Here, that someone else suggested and it looks just as greek to me.
I'll just stick with the 24 hour format. It'll be fine like that.
SouthernAtHeart:
I'd like to try this, but it doesn't compile. An error "too few arguments to function 'char* itoa(int, char*, int)'"
My bad: the itoa function needs three arguments:
char timeString[10];
char temp[5]; // Can be done without this, but easier to understand with it
int hours;
int minutes;
hours = hour();
itoa(hours, temp, DEC); // Use base 10 numbers
strcpy(timeString, temp); // Convert hours to a string and copy it to the buffer
strcat(timeString, ":");
minutes = minute();
if (minutes < 10) {
strcat(timeString, "0");
}
itoa(minutes, temp, DEC);
strcat(timeString, temp);
if (hours > 12) { // Adjust if military time
strcat(timeString, " PM");
} else {
strcat(timeString, " AM");
}
Serial.println(timeString);
I'm still away from my computer, but this should work...
Juraj: @econjack, what do you don't like on sprintf?
Like Delta_G said, code size. sprintf() is a very powerful and flexible function, but rarely does a program use a significant portion of that power. In the following program:
#include <TimeLib.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
char buf[12];
Serial.begin(115200);
while (!Serial);
sprintf(buf, "%02d:%02d:%02d", hour(), minute(), second());
Serial.println(buf);
/*
char timeString[10];
char temp[5]; // Can be done without this, but easier to understand with it
int hours;
int minutes;
hours = hour();
itoa(hours, temp, DEC); // Use base 10 numbers
strcpy(timeString, temp); // Convert hours to a string and copy it to the buffer
strcat(timeString, ":");
minutes = minute();
if (minutes < 10) {
strcat(timeString, "0");
}
itoa(minutes, temp, DEC);
strcat(timeString, temp);
if (hours > 12) { // Adjust if military time
strcat(timeString, " PM");
} else {
strcat(timeString, " AM");
}
Serial.println(timeString);
*/
}
void loop() {
}
the sprintf() version of the code, although only a couple of lines, uses 4038 bytes of flash and 241 bytes of SRAM. The commented-out code, while considerably longer, only uses 2848 bytes of flash and 239 bytes of SRAM. True, in this little test program it really doesn't matter, but as programs become more complex and memory starts disappearing, you just might have to put sprintf() on the back shelf.