Data Logging Shield: wrong RTC in the logged Data

Hi,

I’m ussing the Data Logging Shield for Arduino. In my code, the data will be logged. This works, but I have a problem with the RTC. In my logged Data on the SD Card, I don’t get the real time. I get only the real time in ste serial monitor.
The is the code:

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include “RTClib.h”

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif

RTC_Millis rtc;
File dataFile;

const int audioPin = A0;
const int sensitivity = 450;

const int chipSelect = 10;

void setup() {
Serial.begin(115200);

rtc.begin(DateTime(F(DATE), F(TIME)));

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(SS, 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:
while (1) ;
}
Serial.println(“card initialized.”);

// Open up the file we’re going to log to!
dataFile = SD.open(“datalog.txt”, FILE_WRITE);
if (! dataFile) {
Serial.println(“error opening datalog.txt”);
// Wait forever since we cant write data
while (1) ;
}
}

void loop() {

// make a string for assembling the data to log:
String dataString = “”;

DateTime now = rtc.now();

Serial.print(now.year(), DEC);
dataString += (now.year(), DEC);
Serial.print(’/’);
dataString = (’/’);
Serial.print(now.month(), DEC);
dataString += (now.month(), DEC);
Serial.print(’/’);
dataString += (’/’);
Serial.print(now.day(), DEC);
dataString += (now.day(), DEC);
Serial.print(’ ‘);
dataString += (’ ‘);
Serial.print(now.hour(), DEC);
dataString +=(now.hour(), DEC);
Serial.print(’:’);
dataString += (’:’);
Serial.print(now.minute(), DEC);
dataString += (now.minute(), DEC);
Serial.print(’:’);
dataString += (’:’);
Serial.print(now.second(), DEC);
dataString += (now.second(), DEC);
Serial.println();
dataString += (’ ');
int soundWave = analogRead(audioPin);
Serial.println(soundWave);
dataString += String(soundWave);
if (soundWave>sensitivity) {
Serial.println(“Achtung!! Lärm!!”);
dataString += (“Achtung!! Lärm!!”);

delay(2000);
}

delay(1000);

dataFile.println(dataString);
Serial.println(dataString);
dataFile.flush();

}

Maybe some can help me?

Thanks

code.txt (2.31 KB)

 dataString += (now.year(), DEC);

What, EXACTLY, do you think is being appended to the String instance, and why?

Just because (now.year(), DEC) following Serial.print accomplished something does NOT mean that that snippet of text in some other place will accomplish the same thing.

Something like

dataString += (now.day(), DEC);

is nonsense.

I don't use String with capital S and you should also forget that it exists (it can result in undefined behaviour at run time), but I think the following would be a step in the right direction if you want to stick to String.

dataString += String(now.day());