RTC DS3231 doesn't follow clock order

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 ::slight_smile:

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 :confused:

Those italics are painful to read. Please go back and put the code in code tags, not quote tags. Also, there is absolutely no reason to use the String class for this, and it is a known source of problems in the Arduino environment.

Have you tried to produce a simpler sketch that reproduces the problem?

I noticed something. You are passing three String objects to the String constructor. I'm not sure, but that seems wonky.

String xhour, xminute, xsecond;
//...
  timeString = String(xhour+":"+xminute+":"+xsecond);

The reference says:

Parameters

val: a variable to format as a String - string, char, byte, int, long, unsigned int, unsigned long, float, double

I don't see anything about String in the parameter list. Try:

  timeString = xhour + ":" + xminute + ":" + xsecond;

salanova:
timeString = String(xhour+":"+xminute+":"+xsecond);
dateString = String(date)+"/"+month+"/"+year;

I don't know much about such bullshit like "String", but as it looks, you know even less about.

The quoted lines should possibly read rather like that:

  timeString = xhour+":"+xminute+":"+xsecond;
  dateString = String(date)+"/"+String(month)+"/"+String(year);

You can even use bullshit for something useful when using the bullshit in a correct way.

Hopefully your program will not grow much bigger than that, or lack of RAM might become a problem soon, using your style of programming using "String" objects.

The problem is probably in the way the string is being assembled, thereby you have yet another reason for not using strings anyway. You can just print the individual figures, and it takes less computer time.

Just get some decent clock code., like http://bildr.org/2011/03/ds1307-arduino/ and then add the sensor code you have.

You might find it more sensible to use the date as the file name, and make a new file at midnight, thereby making it unnecessary to record the date all the time.

I think you have misconfigured the RTC's internal registers - it uses BCD, so certain values are
forbidden in the registers, ie hex A,B,C,D,E,F, which will readout as 10..15 which is why I think
you are seeing 15 increment to 16, you have 0x0F in the hour register clock to 0x10, when it should
never have had 0x0F in there, but 0x15.

If the value had been 0x09, it would clock to 0x10 too - but the behaviour for 0x0A..0x0F are
undefined as those values aren't expected.