DS1307 date saved problem

Hi,

I have a func that set the time to the DS1307 component.

void FunctionalTasks::setRealTimeClock() {

    if ( _Tracer!=NULL )
    {
        _Tracer->Print("Heures du systeme embarque mise a jour.");
    }
    // MODULE HORLOGE TEMPS REEL EMBARQUEE
    RTC.stop();
    // REGLAGE HEURE
    RTC.set(DS1307_HR   , 18);     // set the hours
    RTC.set(DS1307_MIN  , 53);     // set the minutes
    RTC.set(DS1307_SEC  , 00);     // set the seconds
    // REGLAGE DATE     
    RTC.set(DS1307_DATE ,  3);     // set the date
    RTC.set(DS1307_MTH  , 12);     // set the month
    RTC.set(DS1307_YR   , 10);     // set the year
    RTC.set(DS1307_DOW  , 4 );     // set the day of the week
    RTC.start();

    
}

I build one time sketch with a call to this func in order to update time and date.

Then it is ok, on my lcd screen display time and date updated.

Then I comment the call of that func because CMOS should save time and date.

I upload the same sketch without update func line.

Then time is updated and not the date. It seems that buffer of date is not saved.

Has anyone got an idea ? Thank you

We know nothing about what the rest of your code looks like. If the time is correct, and continues to be correct after the Arduino is powered for a while, it is unlikely that the RTC has any problems, or is forgetting the date.

The only other explanation is that the problem is code related.

Ok so this is the code :

Sketch

/*
   CoreSystem

   Manage Main Loop to control some electrical feature
   */

#include <inttypes.h>

#include <Time.h>
#include <TimeAlarms.h>

// niva add features
#include <Utils.h>
#include <Traces.h>
// #include <PushButton.h>
#include <PinManager.h>
#include <FunctionalTasks.h>
#include <FunctionalWrapper.h>
#include <QueueList.h>

#include <Wire.h>
#include <LiquidCrystal.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and modified by D. Sjunnesson




Traces            Tracer                   ;

/* Arduino          Lcd 2x16
 *
 * VSS              Pin 1
 * VDD              Pin 2
 * VSS              Pin 3
 * Pin 7            Pin 4
 * VSS              Pin 5
 * Pin 8            Pin 6
 * NA                  Pin 7
 * NA                  Pin 8
 * NA                  Pin 9
 * NA                  Pin 10
 * Pin 9            Pin 11
 * Pin 10           Pin 12
 * Pin 11           Pin 13
 * Pin 12           Pin 14
 * VDD              Pin 15
 * VSS              Pin 16
 *
 *
 */
LiquidCrystal     lcd(7, 8, 9, 10, 11, 12) ;
FunctionalTasks   FuncTask                 ;
PinManager        PinMng                   ;
 
// ENTREES 

//
//  SETUP
//

void setup()  { 

      // SERIAL
      Serial.begin(9600); 

      // LCD
        lcd.begin(16, 2);
        lcd.setCursor(0, 0);
        lcd.clear();
        delay(2000);

      // pass lcd to tracer and tracer to all others objects
      Tracer.setLcd(&lcd);
      FuncTask.setTracer(&Tracer);
      PinMng.setTracer(&Tracer);
      FuncTask.setPinManager(&PinMng);

      FuncTask.setRealTimeClock();   //!!! only if date is out of date

      // Bind the wrapper to FunctionalTasks p and call procedure
      FunctionalWrapper::setObj(FuncTask);

      //
    Tracer.LcdDelayPrintTopLineFromLeft("Ecran........OK");


      // CONFIGURATION
      FuncTask.ConfigPinsRelays();

    Tracer.LcdDelayPrintBottomLineFromLeft("Config Pins..OK");
        delay(600);
      FuncTask.displayRelayConfig();

      // AUTOTEST RELAIS
      FuncTask.Autotests();
      // 
      // GESTION DES ALLUMAGES/EXTINCTION DES LIGNES
      //
      // LAMPADAIRE
      //
      // LE MATIN
      /*Alarm.alarmRepeat(15, 07, 00, FunctionalWrapper::AlimenterLampadaireLnk);*/

} 

//
//  LOOP
//
void loop()  {

      FuncTask.AfficheDateEtHeure();

}

This is the class :

/*
   FunctionalTasks.cpp manage some utility own tasks to provide 
   in my domotik managment

   Copyright (c) Nicolas VALENTIN 2010
   12 Oct 2010 - First Version
   */

#include "WProgram.h"
#include <Wire.h>
#include <Traces.h>
#include <FunctionalTasks.h>
// #include <PinManager.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and modified by D. Sjunnesson
#include <Utils.h>
#include <Time.h>
#include <TimeAlarms.h>



// MANAGER
//=============================================================================
/* Constructor & Destructor {{{1 */
FunctionalTasks::FunctionalTasks()
{
    _idxTemperature=0;
    _average=0;
    _lastAverageTemperature=0;
    _isDateDisplayed=false;

    startTimeDisplayDateEtHeure=millis();
    intervalDisplayDateEtHeure=900;
}
/* }}}1 */
//=============================================================================
/* Accessors {{{1 */
void FunctionalTasks::setTracer(Traces *TracerPtr) {

    if (  TracerPtr != NULL )
    {
        _Tracer = TracerPtr;
    }
}
void FunctionalTasks::setPinManager(PinManager *PinMngPtr) {

    if (  PinMngPtr != NULL )
    {
        _PinMng = PinMngPtr;
    }
}
/* }}}1 */
//=============================================================================
/* {{{1 CLOCK MANAGMENT */
void FunctionalTasks::setRealTimeClock() {

    if ( _Tracer!=NULL )
    {
        _Tracer->Print("Heures du systeme embarque mise a jour.");
    }
    // MODULE HORLOGE TEMPS REEL EMBARQUEE
    RTC.stop();
    // REGLAGE HEURE
    RTC.set(DS1307_HR   , 18);     // set the hours
    RTC.set(DS1307_MIN  , 53);     // set the minutes
    RTC.set(DS1307_SEC  , 00);     // set the seconds
    // REGLAGE DATE     
    RTC.set(DS1307_DATE ,  3);     // set the date
    RTC.set(DS1307_MTH  , 12);     // set the month
    RTC.set(DS1307_YR   , 10);     // set the year
    RTC.set(DS1307_DOW  , 4 );     // set the day of the week
    RTC.start();

    //
    // setTime(horodatage::getHours(),horodatage::getMinutes(),horodatage::getSeconds(),horodatage::getDay(),horodatage::getMonth(),horodatage::getYear()); // set time to 8:29:40am Jan 1 2010 
}

void FunctionalTasks::setRealDateClock() {

    if ( _Tracer!=NULL )
    {
        _Tracer->Print("Heures du systeme embarque mise a jour.");
    }
    // MODULE HORLOGE TEMPS REEL EMBARQUEE
    RTC.stop();
    // REGLAGE DATE     
    RTC.set(DS1307_DATE , 29);     // set the date
    RTC.set(DS1307_MTH  , 11);     // set the month
    RTC.set(DS1307_YR   , 10);     // set the year
    RTC.set(DS1307_DOW  , 0 );     // set the day of the week
    RTC.start();
}

void FunctionalTasks::getRealTimeClock() {

    if ( _Tracer!=NULL )
    {
        _Tracer->BeginPrint(RTC.get(DS1307_HR  , false )); //read the hour and also update all the values by pushing in true
    }
    Serial.print(":");        
    Serial.print(RTC.get(DS1307_MIN , false)); //read  minutes without update (false)
    Serial.print(":");        
    Serial.print(RTC.get(DS1307_SEC , false)); //read  seconds
    Serial.print("      ");              // some space for a more happy life
    Serial.print(RTC.get(DS1307_DATE, false)); //read  date
    Serial.print("/");        
    Serial.print(RTC.get(DS1307_MTH , false)); //read  month
    Serial.print("/");        
    Serial.print(RTC.get(DS1307_YR  , false)); //rea d year
    Serial.println();
}

void FunctionalTasks::displayRealTimeClock() {

    if ( _Tracer!=NULL )
    {
        _Tracer->BeginPrint(RTC.get(DS1307_HR  , false )); //read the hour and also update all the values by pushing in true
        _Tracer->BeginPrint(":");        
        _Tracer->BeginPrint(RTC.get(DS1307_MIN , false)); //read  minutes without update (false)
        _Tracer->BeginPrint(":");        
        _Tracer->EndPrint(RTC.get(DS1307_SEC , false)); //read  seconds
    }
}
/* }}}1 */
//=============================================================================
/* {{{1 AFFICHAGE INFOS HORODATAGE */
void FunctionalTasks::AfficheDateEtHeure() {
    if ( _Tracer!=NULL )
    {
        if ( _isDateDisplayed==false )
        {
            _Tracer->LcdPrintBottomLineFromRight(horodatage::currentDate());
            _isDateDisplayed=true;
        }
        if ( (millis()-startTimeDisplayDateEtHeure) > intervalDisplayDateEtHeure )
        {
            _Tracer->LcdPrintTopLine(horodatage::currentTime());
            startTimeDisplayDateEtHeure=millis();
        }
    }
}

/* }}}1 */
//=============================================================================
/* {{{1 AFFICHAGE TEMPERATURE */
void FunctionalTasks::AfficheTemperature() {

    if ( _Tracer!=NULL )
    {
        FunctionalTasks::SaveTemperatureSamples();
        // PRINT
        if (  _idxTemperature==_NB_TEMP_SAMPLES )
        {
            // AVERAGE
            _average=_average/_NB_TEMP_SAMPLES;
            if (_lastAverageTemperature!=0 )
            { 
                _lastAverageTemperature=(_lastAverageTemperature+_average)/2;
            }
            else
            { 
                _lastAverageTemperature=_average;
            }
            _maxAverageTemp = _lastAverageTemperature;

            // LCD TRACE
            _Tracer->LcdPrintTopLineFromRight(convert::ToString(_lastAverageTemperature) + " C");

            // RAZ
            _idxTemperature=0;
            _average=0;
        }
    }
}

void FunctionalTasks::SaveTemperatureSamples() {

    // TEMPERATURE MANAGE FUNCTION
    //getting the voltage reading from the temperature sensor
    int reading=analogRead(0);  

    // converting that reading to voltage, for 3.3v arduino use 3.3
    float voltage=reading * 5.0 / 1024; 
    float temperatureC=(voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset

    if (  _idxTemperature < _NB_TEMP_SAMPLES )
    { 
        _tabTemperatures[_idxTemperature]=temperatureC;
        _average+=_tabTemperatures[_idxTemperature];

        if ( _lastAverageTemperature!=0 )
        { 
            if ( 0.95*_maxAverageTemp < temperatureC < 1.05*_maxAverageTemp  )
            { 
                _idxTemperature++;
            }
        }
        else
        { 
            _idxTemperature++;
        }
    }

    // print out the voltage
    _Tracer->BeginPrint(convert::ToString(voltage));
    _Tracer->ContinuePrint(" volts - " );

    // now print out the temperature
    //to degrees ((volatge - 500mV) times 100)
    _Tracer->ContinuePrint(convert::ToString(temperatureC));
    _Tracer->EndPrint(" celcius" );
}

/* }}}1 */
//=============================================================================
/* {{{1 GESTION DES LIGNES ELECTRIQUES */
//
//      SHUTDOWN / START POWER
void FunctionalTasks::ConfigPinsRelays() {
      //
      // ENTREES
      //

      // PinMng.setupAPin( 0, INPUT  , "TEMPERATURE_SENSOR");

      //
      // SORTIES
      //
      _PinMng->setupDPin(  "LAMPADAIRE"       , 2, OUTPUT ) ;
      _PinMng->setupDPin(  "PRISE TERRASSE"   , 3, OUTPUT ) ;
      _PinMng->setupDPin(  "APPENTI"          , 4, OUTPUT ) ;
      _PinMng->setupDPin(  "PROJO ALLEE"      , 5, OUTPUT ) ;

    //
      _PinMng->setupRelay( "LAMPADAIRE"       , 1 ) ;
      _PinMng->setupRelay( "PRISE TERRASSE"   , 2 ) ;
      _PinMng->setupRelay( "APPENTI"          , 3 ) ;
      _PinMng->setupRelay( "PROJO ALLEE"      , 4 ) ;
}
void FunctionalTasks::AfficheConfigPins() {

      _PinMng->