sonar.ino 1.27 KB
#include "sonar.h"

void setup_sonar() {
  pinMode(1, FUNCTION_3);
  pinMode(3, FUNCTION_3);

  String temp(bmp.readTemperature());
  bmp.begin(0x76);

  /* bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /\* Operating Mode. *\/ */
  /*                 Adafruit_BMP280::SAMPLING_X2,     /\* Temp. oversampling *\/ */
  /*                 Adafruit_BMP280::SAMPLING_X16,    /\* Pressure oversampling *\/ */
  /*                 Adafruit_BMP280::FILTER_X16,      /\* Filtering. *\/ */
  /*                 Adafruit_BMP280::STANDBY_MS_500); /\* Standby time. *\/ */

}

void loop_sonar(int counter) {
  liquidLevel = liquid_level();
  measurements[counter % 5] = liquidLevel;
}

double liquid_level() {
  /* temperature = bmp.readTemperature(); */
  /* V (m/s) = 331.3 + (0.606 × T) */
  /* double speedOfSound = 331.3 + ( 0.606 * temperature ); */
  double speedOfSound = 331.3 + ( 0.606 * 20 );

  double liquidLevel =
    containerBase
    - ((
        /* dubugLiquid  */
        sonar.ping_median(5)
        * speedOfSound
        / 2 )
       / 1000
       );
  return liquidLevel;
}

double liquid_level_avg() {
  int liquidLevelAvg =
    ( measurements[0]
      + measurements[1]
      + measurements[2]
      + measurements[3]
      + measurements[4]
      ) / 5;
  return liquidLevelAvg;
}