433MHz receiver, interrupt and millis() issue

Hello,
I have created a sketch which listens the data transmitted by some Oregon Sciencitif sensors, based on the informations available here:
https://jeelabs.net/projects/cafe/wiki/Decoding_the_Oregon_Scientific_V2_protocol

It works pretty well.

I want to add a features which tells me when the signal is lost.
The idea is to fill a variable with millis() and raise an alert after 60 seconds with no signal.

Here is the simplified source code :

#include "oregon.cpp"

float indoorHumidity = 0.0;
float indoorTemperature = 0.0;
float previousIndoorHumidity = 0.1;
float previousIndoorTemperature = 0.1;
int piles = 0;
unsigned long timerInt;
int IntSigLost = 0;

/* Debut Oregon */
OregonDecoderV2 orscV2;

volatile word pulse;

void ext_int_1(void) {
   static word last;
   // determine the pulse length in microseconds, for either polarity
   pulse = micros() - last;
   last += pulse;
}

float OregonTemperature(const byte* data) {
   int sign = (data[6]&0x8) ? -1 : 1;
   float temp = ((data[5]&0xF0) >> 4)*10 + (data[5]&0xF) + (float)(((data[4]&0xF0) >> 4) / 10.0);
   return sign * temp;
}

byte OregonHumidity(const byte* data) {
   return (data[7]&0xF) * 10 + ((data[6]&0xF0) >> 4) ;
}

// Ne retourne qu'un apercu de l'etat de la baterie : 10 = faible
byte battery(const byte* data) {
   return (data[4] & 0x4) ? 10 : 90;
}

void reportSerial (const char* s, class DecodeOOK& decoder) {
   byte pos;
   String inTemp;
   String inHum;
   const byte* data = decoder.getData(pos);

   if (data[0] == 0x5A && data[1] == 0x6D) { // Pour BTHR918N salon: 0x5A6D (Id: 7D)
      indoorHumidity = OregonHumidity(data);
      indoorTemperature = OregonTemperature(data);
      piles = battery(data);
    
      inTemp = String(indoorTemperature,1);
      inHum = String(indoorHumidity,0);
      
      /* DATA displayed on a TFT screen and Serial here, removed for clarity */
     
      previousIndoorHumidity = indoorHumidity;
      previousIndoorTemperature = indoorTemperature;
      
      timerInt  = millis();
      IntSigLost = 0;
   }
   decoder.resetDecoder();
   getAndPrintTime();
}

void setup() {
   Serial.begin(115200);

   /* TFT initialisation here */
   attachInterrupt(4, ext_int_1, CHANGE);   // DATA sur D19
}

void loop() {
   static int j = 0;
   cli();
   word p = pulse;

   pulse = 0;
   sei();

   if (p != 0) {
      if (orscV2.nextPulse(p))
      reportSerial("OSV2", orscV2);
   }
   //checkTimers();
}

void checkTimers() {   
   if (((millis() - timerInt) > 60000) && (IntSigLost == 0)) {
      tft.setCursor(88,290);
      tft.setTextColor(RED);
      tft.setTextSize(2);
      tft.print("Pas de signal Int");
      Serial.println("Pas de signal");
      IntSigLost = 1;
   } else {
      tft.fillRect(66,288,200,20,BLACK);
   }
}

My concern is that as soon as I uncomment the checkTimers() function, the data are not received anymore, I can't figure out why.
I am wondering whether it is because of millis() usage, I am lost.
I have attached the oregon.cpp file.

oregon.cpp (3.94 KB)

try 60000UL instead of 60000

volatile word pulse;

void ext_int_1(void) {
   static word last;
   // determine the pulse length in microseconds, for either polarity
   pulse = micros() - last;
   last += pulse;
}

millis() returns an uint32_t, a word has only 16 bits. Change that.

I tried both modifications, as soon as I uncomment checkTimers() 433mhz data is not received anymore... :frowning:

You have another word which should be unsigned long or uint32_t:

void loop() {
  static int j = 0;
  cli();
  word p = pulse; //Ooops

And this un-initialized value may cause problems:

unsigned long timerInt;//Should be timerInt = 0

word p was also replaced by uint32_t, and initialising timerInt does not solve the issue.

Most likely, the stuff you do in checkTimers() takes too long and pulses are missed. The loop() function is time critical.

Almost every time you call checkTimers you do an unnecessary fill operation:

      tft.fillRect(66,288,200,20,BLACK);

That was as easy as that!
Thank you so much jremington.