#include <time.h> // time() ctime()
time_t now; // this is the epoch
tm myTimeInfo; // the structure tm holds time information in a more convient way
#include <SafeString.h>
createSafeString(myDemo_SS, 32);
createSafeString(mySecondDemo_SS, 32);
#define MaxTimeStampLength 64
createSafeString(TimeStamp_SS, MaxTimeStampLength);
createSafeString(myTitle_SS, 64); // reserve 64 bytes for the SafeString-variable
createSafeString(myString_SS, 32); // reserve 32 bytes for the SafeString-variable
int myInteger = -1234;
float myFloat = -987.009;
unsigned long myCounter;
//useful function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 13; // Arduino-Uno Onboard-LED is IO-pin 13
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
myCounter = 0;
myDemo_SS = "Hello world!";
myString_SS = F("fixed text directly assigned");
Serial.print( F(" #") ); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println(F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myTitle_SS = F("content of an integer:");
myString_SS = myInteger;
Serial.println(myTitle_SS);
Serial.print( F(" #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myTitle_SS = F("content of a float:");
myString_SS = myFloat;
Serial.println(myTitle_SS);
Serial.print( F(" #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myTitle_SS = F("you can append more text with the +=-operator ");
myString_SS = F("text ");
myString_SS += myInteger;
myString_SS += F(" ,");
myString_SS += myFloat;
Serial.println(myTitle_SS);
Serial.print( F(" #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
}
void loop() {
myCounter++;
// loop is running very fast counting up very fast
// but only once every 1234 milliseconds print
if ( TimePeriodIsOver(MyTestTimer,5234) ) {
mySecondDemo_SS = myDemo_SS; // assigning a SafeString to another SafeString
mySecondDemo_SS += " "; // append a SPACE
mySecondDemo_SS += myCounter; // append integer-number
Serial.println(mySecondDemo_SS);
StoreTimeStampIntoSS(TimeStamp_SS);
Serial.println(TimeStamp_SS);
Serial.println(TimeStamp_SS.c_str() );
}
}
void StoreTimeStampIntoSS(SafeString& p_RefToSS) {
time(&now); // read the current time
localtime_r(&now, &myTimeInfo); // update the structure tm with the current time
//p_RefToSS = " ";
p_RefToSS = myTimeInfo.tm_year + 1900;
p_RefToSS += ".";
if (myTimeInfo.tm_mon + 1 < 10) {
p_RefToSS += "0";
}
p_RefToSS += myTimeInfo.tm_mon + 1;
p_RefToSS += ".";
if (myTimeInfo.tm_mday < 10) {
p_RefToSS += "0";
}
p_RefToSS += myTimeInfo.tm_mday;
p_RefToSS += "; ";
if (myTimeInfo.tm_hour < 10) {
p_RefToSS += "0";
}
p_RefToSS += myTimeInfo.tm_hour;
p_RefToSS += ":";
if (myTimeInfo.tm_min < 10) {
p_RefToSS += "0";
}
p_RefToSS += myTimeInfo.tm_min;
p_RefToSS += ":";
if (myTimeInfo.tm_sec < 10) {
p_RefToSS += "0";
}
p_RefToSS += myTimeInfo.tm_sec;
}