Arduino SD卡屏蔽和RTC无法随时间在数据记录中写入

时间:2018-02-21 17:27:59

标签: android-sdcard sd-card arduino-ide real-time-clock

我们一直在研究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;
    }



}

非常感谢任何帮助

0 个答案:

没有答案