Hi all, I am super new to Arduino UNO and I have been playing around with the code for the clock. I am trying to have it read the real time from the computer reference but it tends to change to various times when I come back to work on it again. It sometimes will work and sometimes it doesn't.
This is my code:
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
// timing
#define LOG_INTERVAL 1000
#define SYNC_INTERVAL 1000
uint32_t syncTime = 0;
#define ECHO_TO_SERIAL 1
void setup(void) {
Serial.begin(9600);
Wire.begin();
#ifndef ESP8266
while (!Serial);
#endif
// RTC setup
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
DateTime now = rtc.now();
// ---------- THERMISTOR TEMP ----------
int analogValue = analogRead(thermistorPin);
float R = R_fixed * (1023.0 / analogValue - 1.0);
float invT = (1.0 / T25) + (1.0 / B) * log(R / R25);
float T = 1.0 / invT;
float Tc = T - 273.15;
// ---------- SENSOR + VOLTAGE ----------
sensorValue = analogRead(analogInPin);
readVolt = sensorValue * (5.0 / 1023.0);
// ---------- DATETIME STRING ----------
String dateTime = String(now.year()) + "/" +
String(now.month()) + "/" +
String(now.day()) + " " +
String(now.hour()) + ":" +
String(now.minute()) + ":" +
String(now.second());
// ---------- CSV ROW ----------
String dataString = dateTime + "," +
String(Tc) + "," +
String(sensorValue) + "," +
String(readVolt);
// ---------- WRITE TO SD ----------
logfile.println(dataString);
logfile.flush();
// ---------- SERIAL DEBUG ----------
Serial.println(dataString);
"" This is all my code related to time. Thank you.