Setting time problems.

Hi everyone. I have problem with my RTC ds1307. Im using <DS1307RTC.h> and <Time.h> library. I have clock connected to my arduino Mega 2560. Connections. SDA to pin 20, SCL to pin21, and ds18b20 to pin 2 on my Arduino. Everythink is connected to touch screen thats controling arduino. My problem is… On screen i have wirtul button, that when i pressed its saveing time. Time That was set by using buttons + to incrise hours and - decrise hours, same with minutes. Commend that im using is < setTime(hr,min,sec,day,month,yr); > Till this moment everythink is ok, time has changed. All Cool. But exactly after 5minutes, time go back to old date and time. :confused: Strange, coz its allways 5min. How to solve this problem? Pls My code in shor cut.

#include <DS1307RTC.h>
#include <Time.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
 
DallasTemperature sensors(&oneWire);
int godzina;         //Hour
int minuta;          //Min
int sekunda;         //Sec  
int dzien;           //day
int miesiac;         //month
int rok;             //year
 
int adjustH=0;
int adjustM=0;
int adjustS=0;
int adjustY=2016;
int adjustmonth=1;
int adjustD=1;
 
void setup()
{    
     // Serial.begin(9600);
      sensors.begin();
      setSyncProvider(RTC.get);
}
 
void settime()
{
  uint32_t tracker=0;                //Using for tracking.
   tracker = FTImpl.Read32(REG_TRACKER);
 
     godzina=hour();                        //Show time, h, m ,s etc
     minuta=minute();
     sekunda=second();
 
     tmElements_t tm;
     RTC.read(tm);
   
   
  {
 /// Bla Bla Bla drawing on screen.
 
        if(tracker==99)
          {
            while(2)
            {
            moje();
          FTImpl.Display();
          FTImpl.Cmd_Swap();
            }
          }
           if(tracker==1)
        {
          adjustH++;                        
          if (adjustH > 23)adjustH = 0;
         
        }
       
         if(tracker==2)
        {
          adjustH--;                        
          if (adjustH < 0) adjustH = 23;
         
        }
          if(tracker==3)
        {
          adjustM++;                        
          if (adjustM > 59)adjustM = 0;
         
        }
       
         if(tracker==4)
        {
          adjustM--;                         ///+ do gory dodajemy minute
          if (adjustM < 0)adjustM = 59;
         
        }
       
        if(tracker==7)
        {
          adjustD++;                         ///
          if (adjustD > 31)adjustD = 1;
         
        }
       
        if(tracker==8)
        {
          adjustD--;                         ///
          if (adjustD < 1)adjustD = 31;
         
        }
       
        if(tracker==9)
        {
          adjustmonth++;                         ///
          if (adjustmonth > 12) adjustmonth = 1;
         
        }
       
        if(tracker==10)
        {
          adjustmonth--;                         ///
          if (adjustmonth < 1) adjustmonth = 12;
         
        }
       
        if(tracker==21)
        {
          adjustY++;                         ///+ y.
          if (adjustY > 2050) adjustY = 2050;
         
        }
       
        if(tracker==22)
        {
          adjustY--;                         /// - y.
         
        }
       
        if(tracker==23) ///Tracker 23 adjustuje godzine oraz date.
        {
         setTime(adjustH,adjustM,0,adjustD,adjustmonth,adjustY);
       // adjustTime((adjustH*3600)+(adjustM*60)+(adjustD*86400)+(adjustmonth*86400+adjustY);
         
           
           delay(100);
        }
        FTImpl.DLEnd();
    FTImpl.Finish();
  }
 }

and what have you got in function loop() ?

Only this.

tmElements_t tm;
   RTC.read(tm);
   
    sensors.requestTemperatures();
     pomiar=(sensors.getTempCByIndex(0));   /// Termometr podlaczony do pinu 2
      
     godzina=hour();                        //Wyswietlamy godzine.
     minuta=minute();                       //Wyswietlamy minute.
     sekunda=second();   //Wyswietlamy sekundy.
     dzien=day();
     miesiac=month();
     rok=year();

As your code stands now, if tracker equals 99, you still do 12 unnecessary if tests on tracker even though you know it’s 99. Instead, if you use a switch block:

  switch (tracker)
  {
    case 99:
      moje();
      FTImpl.Display();
      FTImpl.Cmd_Swap();
      break;

    case 1:
      adjustH++;                        
      if (adjustH > 23)
      {
        adjustH = 0;
      }
      break;
      
    case 2:
      adjustH--;                        
      if (adjustH > 23)
      {
        adjustH = 0;
      }
      break;

    case 3:
      adjustM++;                        
      if (adjustH > 59)
      {
        adjustM = 0;
      }
      break;

    case 4:
      adjustM--;                        
      if (adjustH < 0)
      {
        adjustM = 59;
      }
      break;

  // More cases...
    default:
      Serial.print("I shouldn't be here. tracker = ");
      Serial.println(tracker);
      break;
  }

the code generates a jump table that avoids those unnecessary if tests. Also, how can a statement like while (2) ever stop. That would hang up your code every time.

Thanks For quick reply, i never thought about useing swich case function :) Right, ill try later to swap my if's for cases. Hope that will help me. So my next step be how to figure out and change while(2) loop. More reading ehh. Thanks again.

Till this moment everythink is ok, time has changed. All Cool. But exactly after 5minutes, time go back to old date and time. :confused: Strange, coz its allways 5min. How to solve this problem?

You are setting the Arduino internal millis() clock to a Time library reference time with the setTime command.

setSyncProvider(RTC.get);

You are synchronizing the internal clock with the RTC every 5 minutes(default time for setSyncProvider). The RTC knows nothing of the internal time you updated from the screen.

Without the complete code I’m not certain of what you display, and where you get the values from. In general, there iappears to be an intermixing and confusion between the RTC time and the internal time. In the snippet you post, RTC.read™ pulls the time elements from the RTC, but when you transfer the time elements to variables like godzina=hour() you are using time library variables.

I think it would help to spend some time with the SetTime and ReadTest examples provided with DS1307 library and with the two examples in the Time Library TimeRTC, and TimeRTCSet.

Took me a while but i think i got it sorted. Did what you told me, i was playing with time.h and ds1307.h librarys. And figure out somethink like this.

setTime(adjustH,adjustM,0,adjustD,adjustmonth,adjustY); RTC.set(now()); Now works perfect for me.

Thanks for not telling me straight answer;) Insted you showed me wich way i should go. Thx all.

cattledog: You are setting the Arduino internal millis() clock to a Time library reference time with the setTime command.

Erm... It's not that extreme. setTime() in the Time library doesn't change the millis() timer. It only sets its internal time variables, sysTime and prevMillis.

Yes, it was a poorly stated. I was trying to say the the Time library, using the millis() timer, is referenced to some external time, rather than the time since the code and millis()timer was started.