Hi everyone,
I am working on a datalogger device. Currently I am trying to just write a timestamp into a textfile.
HW: Arduino Nano Every, DS3231, MicroSD Card Adapter with a microSD card
Every single thing by itself works, but when I try to write year on the sd card only a square is in the file. I have no clue what could be wrong, as the year is normal when wirtten over the serial communication.
Serial monitor: 24-4-18 14:25:44
SD card: []-4-18 14:25:44 (The square brackets represent the square symbol)
I don't understand how the rest of the date can be correct, while the year is messed up.
#include <DS3231.h>
#include <Wire.h>
#include <BufferedPrint.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <RingBuf.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <sdios.h>
//------------------------------------------------------------------------------
// File system object.
SdFat sd;
// Log file.
SdFile file;
DS3231 myRTC;
//RTC setup
bool century = true;
bool h12Flag;
bool pmFlag;
//PIN setup
const byte LED_pin = 2;
const byte SD_pin = 4;
uint32_t t = 0;
void logDate() {
// Write data to file. Start with log time in micros.
// Write ADC data to CSV record.
file.write("");
file.write(myRTC.getYear());
file.print("-");
file.print(myRTC.getMonth(century));
file.print("-");
file.print(myRTC.getDate());
file.print(" ");
file.print(myRTC.getHour(h12Flag, pmFlag)); //24-hr
file.print(":");
file.print(myRTC.getMinute());
file.print(":");
file.print(myRTC.getSecond());
file.println("");
file.sync();
}
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Wire.begin();
pinMode(LED_pin,OUTPUT);
// SDcart setup?
if (!sd.begin(SD_pin)|| !file.open("DATA.CSV", O_CREAT | O_WRITE | O_APPEND)) {
// Replace this with somthing for your app.
}
}
// the loop routine runs over and over again forever:
void loop() {
// return date
digitalWrite(LED_pin,HIGH);
Serial.print(myRTC.getYear());
Serial.print("-");
Serial.print(myRTC.getMonth(century), DEC);
Serial.print("-");
Serial.print(myRTC.getDate(), DEC);
Serial.print(" ");
Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
Serial.print(":");
Serial.print(myRTC.getMinute(), DEC);
Serial.print(":");
Serial.println(myRTC.getSecond(), DEC);
if(millis()>t){
//Write date
logDate();
Serial.println("SD card operation");
t+=60000;
}
delay(1000); // delay in between reads for stability
digitalWrite(LED_pin,LOW);
delay(4000);
}
Don't mind the blinking LED, it's there as a sanity check (+learning to use a transistor).
Thanks for any suggestions.