Hi everyone!
This is my first topic in this forum. I started working on arduino few days ago (I'm a newbie yet) so I decided to make a temperature and humidity datalogger with a RTC, but there is a problem with the clock. It works pefectly but when it pass from 15pm to 16pm it writes 10pm :o and I don't know why.
The code is a mix of some tutorials and things I wrote but I think it's okay :
Components
Arduino UNO
DHT22
DS3231
MicroSD Adaptor
#include <DS3231.h>
#include <Wire.h>
const int chipSelect = 10;
//The SDlibrary needs SPI
#include <SPI.h>
#include "SD.h"
File dataFile;
#define LOGFILE "datalog.txt"
//library to read the temp/humid sensor
#include "DHT.h"
#define DHTPIN 4 // what pin the DHT is connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
DS3231 Clock;
byte year, month, date, DoW, hour, minute, second;
String xhour, xminute, xsecond;
String temperature;
String humidity;
String timeString;
String dateString;
String logEntry;
void setup() {
// Se inicial la interfaz I2c
Wire.begin();
// Se inicia la Comunicación Serial
Serial.begin(9600);
//Initialize the Sensor
Serial.println("Initializing datalogger with RTC version 1.0");
Serial.println("Starting Temp and Humidity Sensor");
dht.begin();
Serial.println("Starting SDCard reader and card");
pinMode(chipSelect, OUTPUT);
pinMode(SS, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("SD Card initialization failed!");
return;
}
Serial.println("Opening logfile for write.");
// Open up the file we're going to log to!
dataFile = SD.open(LOGFILE, FILE_WRITE);
if (! dataFile) {
Serial.println("error opening log file");
// Wait forever since we cant write data
while (1) ;
}
}
void loop() {
Clock.getTime(year, month, date, DoW, hour, minute, second);
if(hour<10)
{
xhour = "0"+String(hour);
}else
{
xhour = String(hour);
}
if(second<10)
{
xsecond = "0"+String(second);
}else
{
xsecond = String(second);
}
if(minute<10)
{
xminute = "0"+String(minute);
}else
{
xminute = String(minute);
}
timeString = String(xhour+":"+xminute+":"+xsecond);
dateString = String(date)+"/"+month+"/"+year;
getTemperature();
getHumidity();
logEntry = createLogEntry();
writeEntryToFile(logEntry);
delay(60000);
}
void getTemperature()
{
temperature = String(dht.readTemperature());
}
void getHumidity()
{
humidity = String(dht.readHumidity());
}
String createLogEntry()
{
String logEntry;
logEntry = dateString+","+timeString+","+temperature+","+humidity;
return logEntry;
}
void writeEntryToFile(String LogEntry)
{
dataFile.println(LogEntry);
dataFile.flush();
Serial.println(LogEntry);
}
What do you think it is?
This is an example of the datalogger:
27/12/15,15:55:39,18.40,79.20
27/12/15,15:56:40,18.40,79.40
27/12/15,15:57:40,18.40,79.30
27/12/15,15:58:40,18.40,79.40
27/12/15,15:59:41,18.40,79.40
27/12/15,10:00:41,18.40,79.30
27/12/15,10:01:41,18.40,79.20
27/12/15,10:02:41,18.40,79.20
Thank you and...Sorry about my english