Occasional extra logging event using an interval timer

I have an environmental monitoring station measuring wind, rain, soil moisture and temp, and ambient temp and RH. I am collecting data every 10 seconds and logging data to an SD card every 5 min and every hour. It’s not pretty but it mostly works :wink:

I have a question about why I am getting occasional logging events at HH:01:SS or HH:06:SS when I’m expecting every 5 minutes or HH:00:SS or HH:05:SS.

Any

**write_interval = 5

if ((rtc.getMinutes() % write_interval) == 0 & rtc.getMinutes() != write_time) { // control write to SD time, 5 minutes ==> set to 1 minute for testing
    write_time = rtc.getMinutes();
    digitalWrite(8, HIGH);
    //write to SD card and print results
    String dataRec = createDataRecord();  //  create record
    logData(dataRec);  //Save the data record to the log file
}

Below is the output being logged. I’ve hidden a lot of rows and columns to keep it brief (assume it increments every 5 minutes) and highlighted the odd logging events

Data Date Time GPS GPS VoltageData Data
AKRON 18-06-13 20:20:03 40.1491 -103.1421 4.06 587.73 670.07
AKRON 18-06-13 20:25:00 40.1491 -103.1421 4.06 587.33 670.25
AKRON 18-06-13 20:26:00 40.1491 -103.1421 4.07 587 669
AKRON 18-06-13 20:30:03 40.1491 -103.1421 4.06 587.08 670.04
AKRON 18-06-13 22:00:03 40.1491 -103.1421 4.03 586.63 670.17
AKRON 18-06-13 22:01:00 40.1491 -103.1421 4.03 586.63 670.17
AKRON 18-06-13 22:05:03 40.1491 -103.1421 4.03 586.08 670.17
AKRON 18-06-13 22:40:03 40.1491 -103.1421 4.03 583.73 670.07
AKRON 18-06-13 22:45:03 40.1491 -103.1421 4.03 583.23 669.97
AKRON 18-06-13 22:46:00 40.1491 -103.1421 4.02 583.23 669.97
AKRON 18-06-13 22:50:03 40.1491 -103.1421 4.02 582.75 670

I’m using an Adafruit Adalogger M0 and the rtc included on the MC. I use a GPS to set the RTC every 4 hours to keep accurate.

I’ve posted my entire code, and though I would of course appreciate any tips or pointers on the rest of it, I’m really looking to figure out this timing issue at this point.

Much Thanks

VarTrial_V1.41.ino (33.5 KB)

if ((rtc.getMinutes() % write_interval) == 0 & rtc.getMinutes() != write_time) { // control write to SD time, 5 minutes ==> set to 1 minute for testing

& and && are two different operators, producing two different results. & is not the correct operator for you to be using.

& thank you. I'll give it a shot.

In that short piece of code, rtc.getMinutes() is used three times and I am not going to look at the code to see what is used by createDataRecord().

There is no guarantee that all of the uses of etc.getMinutes() get the same time. rtc.getMinutes() should be used in exactly one place and its result stored in a variable for use in ALL places.

There may be other issues as well. For example, PaulS is correct about the & and the && operators.

Vaj4088 thank you for your feedback.

rtc.getMinutes() is how to access the RTC minutes using the RTCZero library. It functions as minute() in the Arduino Time library.

Perhaps I'm not using it efficiently? Or you are suggesting I should use something like:

CurrentMinute = rtc.getMinutes() 

 if ((CurrentMinute % write_interval) == 0 && CurrentMinute != write_time) { // control write to SD time, 5 minutes ==> set to 1 minute for testing
    write_time = CurrentMinute;
    digitalWrite(8, HIGH);
    //write to SD card and print results
    String dataRec = createDataRecord();  //  create record
    logData(dataRec);  //Save the data record to the log file

I have other instances where I need to access the RTC minutes and so I would not want CurrentMinute as a global variable. Only local.

Thanks

Your example is what I had in my mind. In general, local variables are to be preferred over global variables but you may have little choice in this case. You want to get the minutes at the top of the loop() function and then use the saved value everywhere.

I also like that you are now using && as PaulS mentioned.