Need help understanding the code

This code measures pressure using a sensor and then detects if a fall has happened.
I have a hard time understanding how does the code work so can someone explain what each statement does? mainly what does size do and what does it mean that it is being divided by size

And Is there a way to minimize the data calculated as I noticed that my Arduino outputs data slower than usual.

 ret = Dps310PressureSensor.measurePressureOnce(Detection_array[0], oversampling);
      state1 = Detection_array[0];
 for (i = 1; i < 9; i++)
  {
     ret = Dps310PressureSensor.measurePressureOnce(Detection_array[i], oversampling);
       if (Detection_array[i] - Detection_array[i - 1] < 5)
      {
        state1 += Detection_array[i];
      }
      else
      {
        size -= 1;
      }
  } 
 state1 = state1 / size;
 delay(100);

Full Code:

float Detection_array[10];
  uint8_t oversampling = 7;
  int16_t ret;
  int i;
  int size = 10;
  int state1;
  int state2;


      ret = Dps310PressureSensor.measurePressureOnce(Detection_array[0], oversampling);
      state1 = Detection_array[0];
 for (i = 1; i < 9; i++)
  {
     ret = Dps310PressureSensor.measurePressureOnce(Detection_array[i], oversampling);
       if (Detection_array[i] - Detection_array[i - 1] < 5)
      {
        state1 += Detection_array[i];
      }
      else
      {
        size -= 1;
      }
  } 
 state1 = state1 / size;
 delay(100);


      ret = Dps310PressureSensor.measurePressureOnce(Detection_array[0], oversampling);
      state2 = Detection_array[0];
 for (i = 1; i < 9; i++)
  {
      ret = Dps310PressureSensor.measurePressureOnce(Detection_array[i], oversampling);
      if (Detection_array[i] - Detection_array[i - 1] < 5)
      {
        state2 += Detection_array[i];
      }
      else
      {
        size -= 1;
      }
  }
  state2 = state2 / size;

 if (ret != 0)
   {
    Serial.print("FAIL! ret = ");
    Serial.println(ret);
   }
/*Calculate the difference in air pressure to determine if you fall*/
    else if (state2 - state1 > 4)
     {
      Serial.println("You fell down. Do you need help?");
      delay(5000);
     }
    else
      Serial.println("It's ok!");
}

For your first code extract, it looks like it starts off taking 9 samples of pressure. It accepts a sample if it is within 5 units of the previous sample. Variable size is, at the end, the number of accepted samples and state1 is the total of the accepted samples. This: state1 = state1 / size ; means state1 now holds an average of the accepted samples.

Have you asked the person that wrote the code?

-jim lee

jimLee: Have you asked the person that wrote the code?

-jim lee

There is no way of communicating or identifying who is the writer of the code.

your full code listing isn’t complete; there are no functions including setup() and loop()

looks like measurePressureOnce() measures the pressure several times and the following code sums the samples to calculate the average. But is skips samples if there a delta > 5. why would this be necessary?

    if (Detection_array[i] - Detection_array[i - 1] < 5)
        state1 += Detection_array[i];
    else
        size -= 1;

another approach is leaky integration that continually averages each new sample

A += (sample - A) * K K < 1

gcjr:
another approach is leaky integration that continually averages each new sample

A += (sample - A) * K K < 1

Can you please give more explanation of how can leaky integration be used in this case as what does the multiplication by k means and that K < 1

the code you posted is collecting a number of samples and taking an average (besides throwing some out). However, the average for the next iteration is independent of the previous. this means that despite taking the time to make multiple measurements, the averages can be abruptly different.

the equation I posted for leaky integration is the basic equation for a simple low-pass filter. If K is 1/4, and there is a step input, sample goes from zero to 1, the average A will become ~0.95 after 12 iterations following a logarithmic curve similar to 2 RC time constants. If this is an issue, just set A to the first sample value.

of course the advantage of this approach is there is just one measurement and one calculation per sampling period and the result is smoothed. K can also be different depending on s > A. There can be separate attack and decay rates that allow the average to more likely represent peaks or valleys (e.g. background noise)