Unable to retrieve RTC date or write to SD card with MSTimer2

Hello,

I currently have a program to manage the engagement of pumps according to the weather conditions (temperature, humidity, brightness ....).
I use an interrupt timer using the MSTimer2 library (triggered every 5 seconds) to read the values of the various sensors and display them in the serial monitor (So far, no problem).

However, in the interrupt routine, when I want to retrieve the time and date with the "RTC.now ()" function or I want to write data to an SD card file with "sensorData = SD.open ("RaisveCapteurs.csv", FILE_WRITE); " This blocks the entire program.

On the other hand, if I test my problems in the main function "loop", everything works correctly ..

Anyone to already encounter this problem? :confused:

Best regards, Leo.

I use an interrupt timer using the MSTimer2 library (triggered every 5 seconds) to read the values of the various sensors and display them in the serial monitor

Using a timer for this seems like overkill.
Why not just use the BlinkWithoutDelay technique and test the value of millis() at intervals ?

As to your problem with using functions in the ISR, do they perhaps use interrupts, which are disabled when in an ISR.

However, in the interrupt routine, when I want to retrieve the time and date with the "RTC.now ()" function

Most RTCs use the Wire interface, which uses interrupts. Forget doing that in an ISR.

You really do not need to use the MsTimer2 library. Is it really critical that something happen at precisely n millisecond intervals?

Hello

Thank you for your reply.
Actually I trigger the actions directly in the interrupt routine and this seems to pose problems of simultaneous uses of the same library.

To be precise, it is according to the permanent measurements of the sensors (interruption) that I will decide to engage (in the loop) a pump cycle very particular.

For example → If outside temperature> 21 degrees + Brightness <40% + …
Then I go into cycle # 3 which will start the pump 2min every 2 hours.

In case of a sudden change in climatic conditions, I wish to be able to switch from the current cycle to another cycle which will be adapted to the new values ​​of the sensors (2min every 10min, 2min every hour, etc).

Only I am blocked by the delay () of activation and deactivation of the pumps which block the main program, whatever the potential change of external climatic conditions.

I am sending you the code to see more clearly. The problems are therefore in the interrupt function: “Interrupt_capteurs()”

Best regards, Leo.

Principal_program_tootem_V2.ino (23.9 KB)

lcharpen17:
Only I am blocked by the delay () of activation and deactivation of the pumps which block the main program, whatever the potential change of external climatic conditions.

yes…

you should learn to create such timers as this:

      while (tempo <= 3600 && test_cycle_jour_2() ) {
        delay(1000);
        tempo ++;
      }

without blocking while loops and delay()

then you probably needn’t use the interrupt timers to escape those blocky bits.

Climactic conditions do not change in microseconds. It is cumbersome and sometimes impossible to use interrupts for the tasks you are attempting. Of course you are blocked by delay(). That is what it is designed to do, block and wait. You need to use non-blocking code using millis() for timing, and get rid of all those delay() calls. Get rid of all the interrupts, you don't need them.