Processing a certain number of sensor values

Hello. I have a problem and I need a sample code structure for this issue. We have a circuit that measures insulation resistance. This value is sampled every 500 ms. Our circuit sometimes gives a low value momentarily. In this case, the alarm is activated immediately and the alarm works for a very short time until a new value arrives. We do not want this short-term triggering.

For example, we want to take and process 10 sensor values ​​in 5 seconds. If the sensor values ​​are lower than 100 tree times in a row, then we want the alarm to be triggered.

How can we create this code structure? Thanks in advance for your support.

tizo1Zaman = millis();
 if(tizo1Zaman-tizo2Zaman > 500)
    {     
    if(izolasyon_direnci<100)
    {      
      digitalWrite(flasor, HIGH); 
    }   
    else
    { 
     digitalWrite(flasor, LOW); 
     }
     tizo1Zaman=tizo2Zaman;

Start with a counter at zero. Every time you take a sensor reading, if it is lower than 100 add 1 to the count. If it is greater than 100 set the counter to zero. If the value of the counter ever exceeds 2 then sound the alarm

1 Like
int GetMeasurment() {
  int Total = 0;
  int ErrorCounter = 0;
  unsigned long StartTenReads = millis();
  unsigned long oldMilis = StartTenReads;
  while (millis() - StartTenReads > 5010) {
    if (millis() - oldMilis >= 500) {
      oldMilis += 500;
      int izolasyon_direnci = analogRead(A0);
      if (izolasyon_direnci < 100)ErrorCounter++;
      Total += izolasyon_direnci;
      if (ErrorCounter > 2) {
        Total = 0;
        break;
      }
    }
  }
  return Total / 10;
}

I tried but this code didn't work. The function always returns 0.

I will try to do this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.