sonar.ino
850 Bytes
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
#include "sonar.h"
void setup_sonar() {
/* pinMode(1, FUNCTION_3); */
/* pinMode(3, FUNCTION_3); */
/* bmp.begin(); */
}
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;
}