Save Data to SD card every hour (exactly time)

hello everyone,
I`m new in arduino,
I have a little problem with how to save data from my Arduino to SD card with every hour(exactly time), I used RTC ds3231, a couple sensor…
for example what I want is:

Date Time Temperature Pressure
5/7/2015 19:00:00 23.4 1012.6
5/7/2015 20:00:00 25.4 1010.2
5/7/2015 21:00:00 25.6 1011.3
5/7/2015 22:00:00 28.1 1011.5

until now, I just can save using delay, but it’s not like I want.
any suggestion, what should I do… thanks for your answer…

here is my full code

#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library

//For RTC
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
  return( (val/10*16) + (val%10) );
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
  return( (val/16*10) + (val%16) );

#include "Wire.h"    // imports the wire library for talking over I2C 
#include "Adafruit_BMP085.h"  // import the Pressure Sensor Library We are using Version one of Adafruit API for this sensor
Adafruit_BMP085 mySensor;  // create sensor object called mySensor

float tempC;  // Variable for holding temp in C
float tempF;  // Variable for holding temp in F
float pressure; //Variable for holding pressure reading

int chipSelect = 4; //chipSelect pin for the SD card Reader
File mySensorData; //Data object you will write your sesnor data to

void setup(){
Serial.begin(9600); //turn on serial monitor
mySensor.begin();   //initialize pressure sensor mySensor

pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
  // sets time and date data to DS3231
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec( & 0x7f);
  *minute = bcdToDec(;
  *hour = bcdToDec( & 0x3f);
  *dayOfWeek = bcdToDec(;
  *dayOfMonth = bcdToDec(;
  *month = bcdToDec(;
  *year = bcdToDec(;

void loop() {
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
tempC = mySensor.readTemperature(); //  Read Temperature from BMP180
tempF = tempC*1.8 + 32.; // Convert degrees C to F
pressure=mySensor.readPressure(); //Read Pressure

mySensorData ="PTData.txt", FILE_WRITE);
if (mySensorData) {
Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  if (minute<10)
  Serial.print(minute, DEC);
  if (second<10)
  Serial.print(second, DEC);
  Serial.print(F(" "));
  Serial.print(dayOfMonth, DEC);
  Serial.print(month, DEC);
  Serial.print(year, DEC);
  Serial.print(F(" ")); 
  Serial.print("The Temp is: "); //Print Your results
  Serial.println(" degrees F");
  Serial.print("The Pressure is: ");
  Serial.println(" Pa.");
  delay(250); //Pause between readings.

mySensorData.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  if (minute<10)
  mySensorData.print(minute, DEC);
  if (second<10)
  mySensorData.print(second, DEC);
  mySensorData.print(dayOfMonth, DEC);
  mySensorData.print(month, DEC);
  mySensorData.print(year, DEC);
  mySensorData.print(F(" "));
mySensorData.print(tempF);                             //write temperature data to card
mySensorData.print(",");                               //write a commma
mySensorData.println(pressure);                        //write pressure and end the line (println)
mySensorData.close();                                  //close the file

Please edit your Post and use the code button </> and not the quote button for your code.

I don’t see anything in your code that attempts to do anything once an hour.
If you are reading the time from your RTC all you need to do is save the data once every time the hour value changes. Something like

if (newHour != prevHour) {
    prevHour = newHour;



Another method : instead of constantly getting the time from the RTC and comparing hours as Robin2 did, you can set an alarm that would trigger an interrupt. See pages 11 to 13 of the DS3231 datasheet.