Here is my setup (container lid is normally closed):
The white enclosure holds two JSN-SR04T sensors, which are mounted on the lid.
Sensor height (relative to ground): 140cm
Maximum expected measurement distance (empty container):230cm
Expected measurement distance (picture): 120 to 150cm
Actual readings: 220 to 330cm
Here's my code (for sensor 1 - code for sensor 2 is identical):
for (i=0; i<59; i++) {
digitalWrite(TRIG1, LOW);
delay(2);
digitalWrite(TRIG1, HIGH);
delay(10);
digitalWrite(TRIG1, LOW);
d1[i] = pulseIn(ECHO1, HIGH);
d1[i] = d1[i]/58; // convert to centimeters
}
d1len = sizeof(d1) / sizeof(d1[0]);
qsort(d1, d1len, sizeof(d1[0]), sort_desc);
fd1 = d1[31];
Note: they do not take measurements at the same time. First it's sensor 1, then sensor 2
Essentially, it takes 59 readings and uses the median measurement value as the distance it reports. The last 6 pings or so, this has been fluctuating around 220 to 275cm, which is far more than it should possibly measure.
When I'm testing things indoor, everything seems fine, so that brings me to the specific testing environment. My assumption is that part of the signal is bouncing off the metal container walls, producing a lot of noise and as the interval time between measurements isn't long enough, give wrong measurements.
My question is, does this assumption sound plausible? What would be the best way to deal with this issue (e.g. how long would all this signal interference last)? The sensor is a bit in a faraway spot and I can't yet update it remotely, so I first would like to see what people think before I spend a lot of time updating the code on it.