Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Error while saving data in text file

I created a function that saves the timestamp in a text file, but it saves unreadable. Full of invalid characters, example: Ÿ@Â O ƒo!\ Ÿ@Â O ƒo!\ Ÿ@Â O ƒo!\ Ÿ

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
RTC_Millis rtc;
File txt;
const char * getCurrentTimestamp()
{
 char dateBuffer[40];
 DateTime now = rtc.now();
 sprintf(dateBuffer,"%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
 return dateBuffer;
}
void saveLog()
{
 txt = SD.open("dados.txt", FILE_WRITE);
 txt.print(getCurrentTimestamp());
 txt.close();
}
void setup() {
 pinMode(53, OUTPUT);
 SD.begin(4); 
 Serial.begin(9600);
 rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
 Serial.println(getCurrentTimestamp());
 saveLog();
 delay(1000);
}

My code is very simple, do not understand where I went wrong. if I copy the following code snippet for saveData() function and save the dateBuffer variable, everything works normally.

char dateBuffer[40];
DateTime now = rtc.now();
sprintf(dateBuffer,"%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

how to save the timestamp correctly in the file through the function getCurrentTimestamp() ?

Answer*

Draft saved
Draft discarded
Cancel

AltStyle によって変換されたページ (->オリジナル) /