LRAT:
Hi UKHeliBob,I'm not in front of Arduino work station but the compiler throws a fault about the strings.h library. Something in the order that Strings is not recognized.
The quotation marks are not part of the code. I just used them to differentiate between code and explanatory text.
Cheers,Luc
-
The "String" class can cause memory issues, fragmentation in particular, and is not recommended.
It's much better to use C strings, (char arrays), with the standard C library functions for manipulation. -
You don't need to include any libraries. It should compile fine without. As Bob asked, what error did you get?
I compiled that example using your variables before I posted it - it definitely works. And just did it again - see below. -
'sprintf_P()' does exactly the same as 'sprintf()', but the format string is stored in program memory rather than in RAM, conserving precious RAM. Much like using the 'F' macro for string literals. The PSTR() just casts the char array to a Pointer to STRing, as required when using the _P version of the library functions.
Nick Gammon has kindly provided some explanations and examples of F() and ..._P(), along with 'PROGMEM' here:-PROGMEM
Load, compile and run this. (Tested on UNO with IDE V1.6.5):-
Edit: You might want to change the baud rate, but it works fine for me at 115200.
char DateAndTimeString[20]; //19 digits plus the null char
int thisYear = 2016;
int thisMonth = 1;
int thisDay = 11;
int thisHour = 9;
int thisMinute = 9;
int thisSecond = 30;
void setup()
{
Serial.begin(115200);
sprintf_P(DateAndTimeString, PSTR("%4d-%02d-%02d %d:%02d:%02d"), thisYear, thisMonth, thisDay, thisHour, thisMinute, thisSecond);
Serial.println(DateAndTimeString);
}
void loop() {}
Edit2: There's a bit more info on format strings here:- sprintf_P formating questions. - #6 by system - Programming Questions - Arduino Forum