Go Down

Topic: DS1307 Real time clock Datalogger (Read 2 times) previous topic - next topic

peko86

Hi, I am making a code to save data in a SD card from sensors of air flow and temperature (DS18B20), It´s all right, but when I save data in an excel document, It don´t saves the hour correctly in every samples.......For example: in the menu of the program, I put 5 seconds of delay between samples and I begin to save data at 19:00:00, it continues with 19:00:05    19:00:10     19:00:15     19:00:20     19:00:25    19:00:31. I can say with this that in a X number of samples (which varies) It has a sample with a delay of one second. I tried a lot of methods, but I can´t look the answer. I'm 5 days with this problem, and Its making me crazy  :(. Can anyone help me please?. I put a part of the code where its the problem (its a part of the while loop-I'm using wire and SD libraries).


Quote

                Wire.beginTransmission(DS1307_ADDRESS);
                Wire.write(zero);
                Wire.endTransmission();
              
                Wire.requestFrom(DS1307_ADDRESS, 7);
              
                int second = bcdToDec(Wire.read());
                int minute = bcdToDec(Wire.read());
                int hour = bcdToDec(Wire.read() & 0b111111); //24 horas
                int weekDay = bcdToDec(Wire.read()); //0-6 -> Domingo-sabado
                int monthDay = bcdToDec(Wire.read());
                int month = bcdToDec(Wire.read());
                int year = bcdToDec(Wire.read());




                
          unsigned long currentTime = 3600*hour+60*minute+second;
              if (currentTime >= lastReadTime + refresh_rate) {
                 
              File logFile = SD.open("LOG.csv", FILE_WRITE);
              
                       logFile.print(monthDay);
                       logFile.print("/");
                       logFile.print(month);
                       logFile.print("/");
                       logFile.print(year);
                       
                       logFile.print("  -  ");
                       
                       logFile.print(hour);
                       logFile.print(":");
                       logFile.print(minute);
                       logFile.print(":");
                       logFile.print(second);
                       
                       logFile.print(",");//Separador de columna
              

                      DATA TO SAVE


              
              logFile.close();
                                                   
              lastReadTime = 3600*hour+60*minute+second;
              }                                  






Thanks for your help ;)

pylon

Have you read the sticky not at the top of this forum?

You should post your full code, else you just waste our time guessing what the rest may be. My guess here is: you have an unnecessary delay() somewhere in your code.

peko86

At first: Thanks for your answer.
My code has about 1500 lines.......I didn´t post it because it would made harder to help me (menus, etc....). I took a look if I see any unnecesary delay, and I think that it hasn´t, other thing that makes me to think is that this delay sometimes is each 7-8 samples, sometimes is each 70 samples (its irregular). If you don´t see any error in this part of code, I´ll post the rest of it....But if I said before, it will be difficult for its length. Thanks!

PaulS

Code: [Select]
              if (currentTime >= lastReadTime + refresh_rate) {
You shouldn't be adding time values. That can cause problems if the values roll over. Only subtraction is guaranteed to work.

With 1500 lines of code, why do you expect to hit EVERY second on the mark? Opening and closing files takes time. Writing to files takes time.

Code: [Select]
          unsigned long currentTime = 3600*hour+60*minute+second;
              lastReadTime = 3600*hour+60*minute+second;

Between those two assignments, lots of time has ticked away.

peko86

Thank you too PaulS. I took your advices, I tried the substraction in the code and it runs a few better than before.....but I see trhat this part is uncorrect:
unsigned long currentTime = 3600*hour+60*minute+second;
              lastReadTime = 3600*hour+60*minute+second;


If the day changes at 00h, this would cause a problem!! This datalogger must to run for a few days. If anyone knows any example of a datalogger that can help me to save data each predefined time, please post it! 

When I finish my code completly, I would post it.....but I think that now is unnecesary, because it has a lot of menus, submenus and other parts unfinished, and it may cause problems to other users.
Thank you to everyone!!

Go Up