for few days now after trying the usual code for the ultrasonic module
some drifting values still exists as you can see here without measuring any body
CLEARSHEET
CLEARDATA
HX711 scale demo
ACELL,SET,B10, 114.81
CELL,SET,B10, 197.95
CELL,SET,B10, 165.94
CELL,SET,B10, 198.90
CELL,SET,B10, 198.03
CELL,SET,B10, 198.12
CELL,SET,B10, 200.32
CELL,SET,B10, 198.90
CELL,SET,B10, 151.04
CELL,SET,B10, 197.56
CELL,SET,B10, 197.95
CELL,SET,B10, 181.18
CELL,SET,B10, 197.56
CELL,SET,B10, 115.25
CELL,SET,B10, 200.20
CELL,SET,B10, 197.84
CELL,SET,B10, 197.80
CELL,SET,B10, 200.13
CELL,SET,B10, 180.86
CELL,SET,B10, 78.82
CELL,SET,B10, 121.33
CELL,SET,B10, 198.05
CELL,SET,B10, 197.56
CELL,SET,B10, 200.11
CELL,SET,B10, 140.05
CELL,SET,B10, 199.37
CELL,SET,B10, 200.32
CELL,SET,B10, 197.65
CELL,SET,B10, 198.97
CELL,SET,B10, 199.07
CLEARSHEET
CLEARDATA
HX711 scale demo
ACELL,SET,B10, 196.07
CELL,SET,B10, 198.43
CELL,SET,B10, 200.32
CELL,SET,B10, 198.60
CELL,SET,B10, 199.47
CELL,SET,B10, 200.88
CELL,SET,B10, 197.75
CELL,SET,B10, 198.11
CELL,SET,B10, 90.56
CELL,SET,B10, 198.90
CELL,SET,B10, 198.39
CELL,SET,B10, 198.41
CELL,SET,B10, 199.33
CELL,SET,B10, 198.43
CELL,SET,B10, 199.35
CELL,SET,B10, 198.05
CELL,SET,B10, 200.75
CELL,SET,B10, 196.33
CELL,SET,B10, 200.39
CELL,SET,B10, 153.51
CELL,SET,B10, 197.95
CELL,SET,B10, 197.94
CELL,SET,B10, 198.14
CELL,SET,B10, 200.22
CELL,SET,B10, 200.35
CELL,SET,B10, 197.84
CELL,SET,B10, 122.41
CELL,SET,B10, 197.94
CELL,SET,B10, 200.90
CELL,SET,B10, 198.97
and here after measuring a real person
CLEARSHEET
CLEARDATA
HX711 scale demo
ACELL,SET,B10, 196.56
CELL,SET,B10, 198.52
CELL,SET,B10, 215.37
CELL,SET,B10, 219.96
CELL,SET,B10, 217.67
CELL,SET,B10, 30.97
CELL,SET,B10, 31.71
CELL,SET,B10, 30.86
CELL,SET,B10, 33.24
CELL,SET,B10, 30.60
CELL,SET,B10, 31.71
CELL,SET,B10, 31.62
CELL,SET,B10, 32.20
CELL,SET,B10, 217.75
CELL,SET,B10, 217.69
CELL,SET,B10, 217.48
CELL,SET,B10, 32.79
CELL,SET,B10, 31.73
CELL,SET,B10, 32.56
CELL,SET,B10, 31.33
CELL,SET,B10, 32.67
CELL,SET,B10, 31.94
CELL,SET,B10, 32.39
CELL,SET,B10, 217.39
CELL,SET,B10, 32.01
CELL,SET,B10, 216.82
CELL,SET,B10, 33.17
CELL,SET,B10, 32.09
CELL,SET,B10, 218.24
CELL,SET,B10, 31.26
CLEARSHEET
CLEARDATA
HX711 scale demo
ACELL,SET,B10, 196.54
CELL,SET,B10, 200.77
CELL,SET,B10, 198.52
CELL,SET,B10, 200.66
CELL,SET,B10, 198.12
CELL,SET,B10, 198.86
CELL,SET,B10, 31.16
CELL,SET,B10, 218.14
CELL,SET,B10, 32.13
CELL,SET,B10, 30.60
CELL,SET,B10, 30.96
CELL,SET,B10, 30.96
CELL,SET,B10, 30.60
CELL,SET,B10, 30.96
CELL,SET,B10, 30.11
CELL,SET,B10, 32.01
CELL,SET,B10, 31.52
CELL,SET,B10, 31.45
CELL,SET,B10, 30.69
CELL,SET,B10, 32.39
CELL,SET,B10, 31.52
CELL,SET,B10, 31.14
CELL,SET,B10, 32.20
CELL,SET,B10, 31.14
CELL,SET,B10, 31.24
CELL,SET,B10, 31.71
CELL,SET,B10, 32.69
CELL,SET,B10, 32.20
CELL,SET,B10, 31.24
CELL,SET,B10, 31.35
CELL,SET,B10, 30.48
CELL,SET,B10, 31.18
CELL,SET,B10, 30.60
CELL,SET,B10, 29.63
CELL,SET,B10, 31.18
CELL,SET,B10, 31.16
CELL,SET,B10, 31.05
CELL,SET,B10, 30.67
CELL,SET,B10, 31.26
CELL,SET,B10, 31.14
CELL,SET,B10, 31.14
CELL,SET,B10, 31.73
CELL,SET,B10, 31.18
CELL,SET,B10, 31.33
CELL,SET,B10, 30.01
CELL,SET,B10, 30.12
CELL,SET,B10, 30.86
CELL,SET,B10, 30.39
CELL,SET,B10, 30.12
CELL,SET,B10, 30.88
CELL,SET,B10, 31.33
CELL,SET,B10, 30.97
CELL,SET,B10, 30.48
CELL,SET,B10, 32.30
CELL,SET,B10, 31.45
CELL,SET,B10, 31.07
CELL,SET,B10, 31.45
and here is the usual code
switch (GetKey("A")) {
case 'A':
BufReset();
lcd_1.clear();
lcd_1.setCursor(3, 0);
lcd_1.print("Thank You");
delay(1000);
lcd_1.clear();
State = S_Idle;
return;
}
//digitalWrite(trigPin, LOW);
for (int i = 0; i < 20; i++) {
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration_us = pulseIn(echoPin, HIGH);
distance_cm = 1.111 * (0.017 * duration_us);
distance_cm_X = abs(200 - distance_cm);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Ht: Cm");
printValueF(4, distance_cm_X);
//Serial.print("Height: ");
//Serial.println(distance_cm_X);
Serial.print("CELL,SET,B10, ");
Serial.print(distance_cm_X);
Serial.println();
//tone(BUZZER_PIN, 1000, 350); // make a beep sound
delay(2000);
}
tone(BUZZER_PIN, 1000, 350); // make a beep sound
lcd_1.clear();
State = S_Idle;
return;
}
and as you can see, the presence of offshoot values will make take average much more prone to error,
and after a lot of searching , I found specific code to remove noise from the readings
the first one is using the KALMAN filter , as you can see the code here
void usonic_transmit() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
}
double kalman(double U){
static const double R = 40;
static const double H = 1.00;
static double Q = 10;
static double P = 0;
static double U_hat = 0;
static double K = 0;
K = P*H/(H*P*H+R);
U_hat += + K*(U-H*U_hat);
P = (1-K*H)*P+Q;
return U_hat;
}
void HeightX() {
switch (GetKey("A")) {
case 'A':
BufReset();
lcd_1.clear();
lcd_1.setCursor(3, 0);
lcd_1.print("Thank You");
delay(1000);
lcd_1.clear();
State = S_Idle;
return;
}
for (int i = 0; i < 30; i++) {
usonic_transmit();
durations = pulseIn(echoPin, HIGH);
distances = 1.106 * ((durations*.034)/2);
kaldist = kalman(distances);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Ht: Cm");
printValueF(4, kaldist);
Serial.println(distances); // "Distance (in cm): %f",
Serial.println( kaldist); // "Corrected distance (in cm): %f",
Serial.print("CELL,SET,B10, ");
Serial.print(kaldist);
Serial.println();
delay(1000);
}
tone(BUZZER_PIN, 1000, 350); // make a beep sound
lcd_1.clear();
State = S_Idle;
return;
}
here the readings before measurement
CLEARSHEET
CLEARDATA
HX711 scale demo
A197.16
0
CELL,SET,B10, 0.00
198.32
39.66
CELL,SET,B10, 39.66
199.53
89.28
CELL,SET,B10, 89.28
198.3
128.43
CELL,SET,B10, 128.43
199.53
155.34
CELL,SET,B10, 155.34
198.32
171.93
CELL,SET,B10, 171.93
180.93
175.43
CELL,SET,B10, 175.43
143.14
162.84
CELL,SET,B10, 162.84
198.59
176.79
CELL,SET,B10, 176.79
198.76
185.36
CELL,SET,B10, 185.36
198.1
190.33
CELL,SET,B10, 190.33
209.25
197.72
CELL,SET,B10, 197.72
200.22
198.7
CELL,SET,B10, 198.70
118.41
167.36
CELL,SET,B10, 167.36
198.42
179.48
CELL,SET,B10, 179.48
111.57
152.97
CELL,SET,B10, 152.97
199.62
171.18
CELL,SET,B10, 171.18
197.93
181.62
CELL,SET,B10, 181.62
198.66
188.28
CELL,SET,B10, 188.28
198.32
192.2
CELL,SET,B10, 192.20
198.19
194.54
CELL,SET,B10, 194.54
198.77
196.19
CELL,SET,B10, 196.19
201.01
198.07
CELL,SET,B10, 198.07
199.15
198.49
CELL,SET,B10, 198.49
198.66
198.56
CELL,SET,B10, 198.56
198.59
198.57
CELL,SET,B10, 198.57
197.7
198.23
CELL,SET,B10, 198.23
200.18
198.99
CELL,SET,B10, 198.99
203.17
200.63
CELL,SET,B10, 200.63
197.83
199.54
CELL,SET,B10, 199.54
CLEARSHEET
CLEARDATA
HX711 scale demo
A197.38
0
CELL,SET,B10, 0.00
210.49
42.1
CELL,SET,B10, 42.10
197.76
90.41
CELL,SET,B10, 90.41
141.64
108.8
CELL,SET,B10, 108.80
94.57
103.42
CELL,SET,B10, 103.42
197.48
139.72
CELL,SET,B10, 139.72
199.15
162.82
CELL,SET,B10, 162.82
199.28
177.03
CELL,SET,B10, 177.03
197.67
185.08
CELL,SET,B10, 185.08
200.03
190.92
CELL,SET,B10, 190.92
198.61
193.92
CELL,SET,B10, 193.92
200.13
196.34
CELL,SET,B10, 196.34
120.46
166.72
CELL,SET,B10, 166.72
198.23
179.02
CELL,SET,B10, 179.02
198.68
186.7
CELL,SET,B10, 186.70
197.83
191.04
CELL,SET,B10, 191.04
198.61
194
CELL,SET,B10, 194.00
198.23
195.65
CELL,SET,B10, 195.65
200.18
197.42
CELL,SET,B10, 197.42
197.76
197.55
CELL,SET,B10, 197.55
199.45
198.29
CELL,SET,B10, 198.29
201.82
199.67
CELL,SET,B10, 199.67
198.61
199.25
CELL,SET,B10, 199.25
198.49
198.96
CELL,SET,B10, 198.96
197.67
198.45
CELL,SET,B10, 198.45
197.83
198.21
CELL,SET,B10, 198.21
200.96
199.28
CELL,SET,B10, 199.28
198.12
198.83
CELL,SET,B10, 198.83
198.7
198.78
CELL,SET,B10, 198.78
197.38
198.23
CELL,SET,B10, 198.23
and after measuring
CLEARSHEET
CLEARDATA
HX711 scale demo
A195.47
0
CELL,SET,B10, 0.00
198.79
39.76
CELL,SET,B10, 39.76
86.36
54.22
CELL,SET,B10, 54.22
34.88
47.27
CELL,SET,B10, 47.27
35.63
42.87
CELL,SET,B10, 42.87
95.53
63.19
CELL,SET,B10, 63.19
35.18
52.3
CELL,SET,B10, 52.30
1079.4
452.64
CELL,SET,B10, 452.64
34.31
289.43
CELL,SET,B10, 289.43
1088.82
601.43
CELL,SET,B10, 601.43
35.25
380.42
CELL,SET,B10, 380.42
218.35
317.15
CELL,SET,B10, 317.15
220.06
279.25
CELL,SET,B10, 279.25
810.48
486.63
CELL,SET,B10, 486.63
87.3
330.74
CELL,SET,B10, 330.74
95.44
238.88
CELL,SET,B10, 238.88
33.28
158.62
CELL,SET,B10, 158.62
33.37
109.72
CELL,SET,B10, 109.72
34.33
80.29
CELL,SET,B10, 80.29
34.22
62.31
CELL,SET,B10, 62.31
34.22
51.34
CELL,SET,B10, 51.34
34.31
44.69
CELL,SET,B10, 44.69
34.43
40.69
CELL,SET,B10, 40.69
34.76
38.37
CELL,SET,B10, 38.37
33.94
36.64
CELL,SET,B10, 36.64
33
35.22
CELL,SET,B10, 35.22
34.43
34.91
CELL,SET,B10, 34.91
34.31
34.68
CELL,SET,B10, 34.68
32.98
34.01
CELL,SET,B10, 34.01
35.25
34.5
CELL,SET,B10, 34.50
CLEARSHEET
CLEARDATA
HX711 scale demo
A29.27
0
CELL,SET,B10, 0.00
29.39
5.88
CELL,SET,B10, 5.88
29.5
13.21
CELL,SET,B10, 13.21
30.23
19.32
CELL,SET,B10, 19.32
30.82
23.67
CELL,SET,B10, 23.67
28.15
25.4
CELL,SET,B10, 25.40
29.59
27.03
CELL,SET,B10, 27.03
30.05
28.21
CELL,SET,B10, 28.21
28.94
28.49
CELL,SET,B10, 28.49
29.76
28.99
CELL,SET,B10, 28.99
29.78
29.3
CELL,SET,B10, 29.30
29.86
29.52
CELL,SET,B10, 29.52
28.65
29.18
CELL,SET,B10, 29.18
27.68
28.59
CELL,SET,B10, 28.59
29.69
29.02
CELL,SET,B10, 29.02
28.05
28.64
CELL,SET,B10, 28.64
29.69
29.05
CELL,SET,B10, 29.05
29.58
29.26
CELL,SET,B10, 29.26
29.69
29.42
CELL,SET,B10, 29.42
30.52
29.85
CELL,SET,B10, 29.85
30.16
29.97
CELL,SET,B10, 29.97
29.67
29.85
CELL,SET,B10, 29.85
28.75
29.42
CELL,SET,B10, 29.42
28.62
29.11
CELL,SET,B10, 29.11
28.75
28.97
CELL,SET,B10, 28.97
29.29
29.09
CELL,SET,B10, 29.09
31.78
30.14
CELL,SET,B10, 30.14
29.29
29.81
CELL,SET,B10, 29.81
30.61
30.12
CELL,SET,B10, 30.12
29.69
29.95
you can notice that the last 10 reading are very smooth,
the second one is much more smoother
here the code
long SonarSensor(int trigPin, int echoPin) {
//digitalWrite(trigPin, LOW);
//delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durations = pulseIn(echoPin, HIGH);
unfiltereds = (durations / 2) / speed_of_sound; //Stores preliminary reading to compare
if(durations <= 8) durations = ((inRange + 1) * speed_of_sound * 2);
//Rejects very low readings, kicks readout to outside detection range
if(lastDurations == 0) lastDurations = durations;
//Compensation parameters for intial start-up
if(durations > (5 * maxDuration)) durations = lastDurations;
//Rejects any reading defined to be out of sensor capacity (>1000)
//Sets the fault reading to the last known "successful" reading
if(durations > maxDuration) durations = maxDuration;
//Caps Reading output at defined maximum distance (~200)
if((durations - lastDurations) < ((-1) * (NoiseReject / 100) * lastDurations)){
distances = (lastDurations / 2) / speed_of_sound; //Noise filter for low range drops
}
distances = 1.099 * ((durations / 2) / speed_of_sound);
lastDurations = durations; //Stores "successful" reading for filter compensation
return distances;
}
void HeightX() {
switch (GetKey("A")) {
case 'A':
BufReset();
lcd_1.clear();
lcd_1.setCursor(3, 0);
lcd_1.print("Thank You");
delay(1000);
lcd_1.clear();
State = S_Idle;
return;
}
for (int i = 0; i < 30; i++) {
//usonic_transmit();
//durations = pulseIn(echoPin, HIGH);
//distances = 1.106 * ((durations*.034)/2);
//kaldist = kalman(distances);
SonarSensor( trigPin, echoPin);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Ht: Cm");
printValueF(4, distances);
Serial.println(distances); // "Distance (in cm): %f",
//Serial.println( kaldist); // "Corrected distance (in cm): %f",
Serial.print("CELL,SET,B10, ");
Serial.print(distances);
Serial.println();
delay(1000);
}
tone(BUZZER_PIN, 1000, 350); // make a beep sound
lcd_1.clear();
State = S_Idle;
return;
}
and here are the reading before measuring
CLEARSHEET
CLEARDATA
HX711 scale demo
A142
CELL,SET,B10, 142
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
198
CELL,SET,B10, 198
197
CELL,SET,B10, 197
200
CELL,SET,B10, 200
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
198
CELL,SET,B10, 198
201
CELL,SET,B10, 201
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
198
CELL,SET,B10, 198
178
CELL,SET,B10, 178
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
197
CELL,SET,B10, 197
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
200
CELL,SET,B10, 200
198
CELL,SET,B10, 198
197
CELL,SET,B10, 197
200
CELL,SET,B10, 200
200
CELL,SET,B10, 200
200
CELL,SET,B10, 200
198
CELL,SET,B10, 198
197
CELL,SET,B10, 197
198
CELL,SET,B10, 198
and after measuring a real person
CLEARSHEET
CLEARDATA
HX711 scale demo
A196
CELL,SET,B10, 196
200
CELL,SET,B10, 200
198
CELL,SET,B10, 198
198
CELL,SET,B10, 198
28
CELL,SET,B10, 28
30
CELL,SET,B10, 30
29
CELL,SET,B10, 29
30
CELL,SET,B10, 30
29
CELL,SET,B10, 29
30
CELL,SET,B10, 30
29
CELL,SET,B10, 29
31
CELL,SET,B10, 31
30
CELL,SET,B10, 30
31
CELL,SET,B10, 31
30
CELL,SET,B10, 30
30
CELL,SET,B10, 30
30
CELL,SET,B10, 30
30
CELL,SET,B10, 30
30
CELL,SET,B10, 30
30
CELL,SET,B10, 30
30
CELL,SET,B10, 30
29
CELL,SET,B10, 29
30
CELL,SET,B10, 30
31
CELL,SET,B10, 31
29
CELL,SET,B10, 29
31
CELL,SET,B10, 31
31
CELL,SET,B10, 31
32
CELL,SET,B10, 32
30
CELL,SET,B10, 30
31
CELL,SET,B10, 31
as you can see the second one gives much smoother readings
my question now is , which code would give more reliable averages based on the analysis of the code and results of both techniques?