Need help understanding the code

This code is a demo for Grove - High Precision Barometric Pressure Sensor DPS310 that 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 outputs values are slower than usual.

Code:
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;

Full code:

#include <Dps310.h>

Dps310 Dps310PressureSensor = Dps310();

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  Dps310PressureSensor.begin(Wire);
  Serial.println("Init complete!");
}

void loop()
{
  float Detection_array[10];
  uint8_t oversampling = 7;
  int16_t ret;
  int i;
  int size = 10;
  int state1;
  int state2;
/*In the following two cycles, the pressure state at the pre and post time was detected respectively.
  The sampling quantity was 10. The values with large deviation were removed, and the average value was calculated.*/
      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!");
}

Well, the ‘size’ variable is just a reverse counter…at least that the way they’re using it in this code. I’m not sure of the significance of the name “size” though–or why they chose that name. But it’s simply counting backwards from 10, and then they use it in the denominator of their calculations. The one thing that sticks out at me immediately is the potential for a divide-by-zero operation, because they’re not checking to see if it’s zero (0) before using it as a denominator. So then, can it ever become zero? It would sure seem so–because nowhere are they re-setting, so eventually it could (in theory at least) get decremented down to zero on its way to becoming negative.

As to the other parts of the code, it looks to me as though (as their comment states) they are simply measuring pressure at two time intervals, multiple times each. They are grabbing 8 measures each in their two ‘for’ loops. They store each measurement in their “Detection_array” and then compute the difference between it and the preceding measurement…and then relating that difference to the (magic) number 5. Why? I’m not sure–that’s a magic number, which is pretty silly really…because no one knows why you’ve chosen that particular number. I would have done something like this:

#define THRESHOLD_VAL 5

…at the top of the code, for whatever that value ‘5’ represents. As it stands now though, I have no idea what they’re trying to accomplish with those comparisons–but they’re pretty important, especially since they’re decrementing the ‘size’ value based upon the comparison. So this means they will potentially end up trying to dividing by zero too, because of that.

So without having their Dps310 library to look through, or at least their Dps310.h file to go by, I can’t really say why they are doing what they’re doing. It is interesting though that their Dps310.begin() method seems to take an i2c object? They’re passing in something called “Wire,” which suggests they are using the same “Wire” object name used in the Arduino i2c library. But again, without looking at their library code I can’t be sure. I don’t see a Wire.h being included though, and “Wire” is not declared anywhere in the cost you posted, so I’m not really sure what they’re doing. My guess though is that it’s some sort of reference to an i2c object.

TB

1 Like