我们一直在研究 SD 卡模块和 RTC,这样做的主要目的只是显示时间和日期,然后将数据存储到 SD 卡中,它读取得很好,但是当我们读取 txt 文件时,它就变成了空的。我的问题是我们如何在 txt 文件中显示数据日志?这是代码:
#include <SD.h>
#include "RTClib.h"
const int chipSelect = 4;
int i=0;
int t;
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
RTC_Millis rtc;
void setup(void)
{
Serial.begin(9600);
Serial.println("MICRO"); //Test the serial monitor
//RTC Module Initialization
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
rtc.begin(DateTime(_DATE_,__TIME__));
delay(2000);
//SD Card Module Initialization
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
}
Serial.println("card initialized.");
delay(500);
}
void loop() {
//DATE&TIME
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//delay(3000);
delay(2000);
//SD.begin(chipSelect);
//DATALOGGING w DATE&TIME STAMP
i++;
int erick=i%5;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
if (erick==0){
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println();
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(' ');
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
i=0;
}
}
任何帮助将非常感激