sonar.ino
1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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;
}