how to record to a string ?

I’ve added an RTC to my beer logger project and it displays it nice on the LCD screen, next I would like to add the values to the Log file, how do I combine this so that I can log it as a String ?

here is the time display code when I send it to the LCD:

lcd.setCursor(0,1);
lcd.print(now.year(), DEC);
lcd.print(’/’);
lcd.print(now.month(), DEC);
lcd.print(’/’);
lcd.print(now.day(), DEC);
lcd.print(" ");
lcd.print(now.hour(), DEC);
lcd.print(’:’);
lcd.print(now.minute(), DEC);
lcd.print(’:’);
lcd.print(now.second(), DEC);

and here is how I write to do the file.txt on the SD card.

// if the file is available, write to it:
if (dataFile) {
dataFile.println(String(counter) + “,” + external_temp + “,” + internal_temp);
dataFile.close();
}

I need to add the DATE Time, it is displayed as: YYYY/MM/DD HH:MM:SS

thank you!

Look up the printf() family of functions, particularly sprintf() to do a formatted print to a string (lower case 's'). This should do what you need and avoid the pitfalls of using Strings (with upper case 'S').

when I try to print the YEAR just to see how this works, instead of 2016 I get 0010, why ?

char buffer[9]="";
int YEAR = (now.year(), DEC);

Serial.print(YEAR);

lcd.setCursor(0,1);
sprintf(buffer, "%04d", YEAR);
lcd.print(buffer);

I must be doing something wrong ?

kodie425:

int YEAR = (now.year(), DEC);

I must be doing something wrong ?

Yep

int YEAR = now.year();

great, thanks for helping out - I am new to C.

Maybe something is going wrong because you are formatting the data twice (I think).

In your case, sprintf stuffs a buffer with the ASCII formated string of "2016". Then, the string gets unnecessarily reformatted (I think) when calling the lcd.print() method. lcd.print() might be trying to convert the ASCII string "2016" into ASCII, which would produce erratic results. Sounds confusing, I know.

My suggestion would be to print the integer 2016 directly to the lcd. link

Also, try these edits:

char buffer[4];

and

sprintf(buffer, "%d", YEAR);

Declared as buffer[4], buffer can hold only 3 characters!

Don't forget to add 1 element for the zero terminator of a character array.

what BulldogLowell suggested worked, its fine now and displaying 2016.

I've made the buffer bigger to fit the whole DATE/TIME, and works correctly and even got it into the Log file.txt now, so great - this was my starting goal. I am capturing 4 data points every 60 seconds, or 4 columns.

What is the best way to format DATE/TIME if later I want to chart the data using a service like https://plot.ly/ ?

Also maybe someone knows, how hard is it to send the data to the internet and make a live graph ?

kodie425:
I’ve added an RTC to my beer logger project and it displays it nice on the LCD screen, next I would like to add the values to the Log file, how do I combine this so that I can log it as a String ?

I need to add the DATE Time, it is displayed as: YYYY/MM/DD HH:MM:SS

I believe you are better off printing date/time to SD in essentially the same way as you are sending it to LCD now, but then print a comma to separate things. I believe it is faster and more efficient that making strings, and you are sure to still need the comma anyway.

 dataFile.println(String(counter) + "," + external_temp + "," + internal_temp);

Printing that String to SD is something you will surely regret for two reasons: it’s bound to stuff up Arduino’s memory, and makes for useless data anyway. The latter applies to sprint(f) as well. If you simply send the variables out as ordinary number with comma separators, it is a lot easier and you then have a standard file in CSV format that you can use in Excel etc, without subsequent messing about. Something like:

void WriteSD()
{  
           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.println(ShrTemp);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE     
}

kodie425:
Also maybe someone knows, how hard is it to send the data to the internet and make a live graph ?

You can use the Internet of Things to do that, like Xively or Thingspeak. Live is a relative term. With Xively you can only update every ten seconds, and I believe it is better than most.

If you have Office 2003 or earlier, you can use PLXDAQ to to make live graphs in Excel directly. Another live option is Bluetooth Graphics Terminal.

kodie425:
I've added an RTC to my beer logger project and it displays it nice on the LCD screen, next I would like to add the values to the Log file, how do I combine this so that I can log it as a String ?

here is the time display code when I send it to the LCD:

lcd.setCursor(0,1);
lcd.print(now.year(), DEC);
lcd.print('/');

Write a function "printToLcdAndLog" that performs the lcd.print and also writes the chaacters to the log file.