Hi all, I'm trying to build a Pad for a DancePad, that game where you press buttons on the floor at the rythm of a song.
Well, the pads on the floor, due to the force we use can break, so I wanted to try something new, using a distance sensor, the main concept is, if the distance is lower than a specified error I send the pressed button instruction, if goes up the distance over certain error, the button is released.
The base of the code is done, and is working fine at some extent, the lack of presicion of HC-SR04 is something to handle, 2mm for this game is too much, but I have found two ways to handle this.
- Take n samples, calc the average, I need to wait get the n samples
- Take n samples, remove the oldest element, add a new one, calc the average, While higher the n_samples, I need to update enough samples to notice something changed
In particular, this works pretty well, the error was reduced a lot, from 15 of error (with one value) to 5 with method 1 and 7 with method 2, but still more samples to get, means more time I need.
Oks, here is the point, how fast we press a pad? in a hard song, can be 3 times per second, a lot and still not the hardest song, the average is 333ms, and the time the pad goes up is still fast, while faster the pad goes up, we can play hardest songs.
The HC-SR04 has enough sample rate to get a lot of samples per second, but no idea why, Arduino is doing weird things, I'm using Arduino Leonardo.
const int Trigger = 2;
const int Echo = 3;
void setup() {
Serial.begin(115200);
pinMode(Trigger, OUTPUT);
pinMode(Echo, INPUT);
digitalWrite(Trigger, LOW);
}
void loop()
{
long t;
digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger, LOW);
t = pulseIn(Echo, HIGH);
Serial.println(t);
delay(11);
}
The above is a sample code to describe what happens, first, to get samples with arduino, the time we need per sample is the delay function, which is 11, if I want to take 4 samples, will be at least 44ms.
As described above, the game works with a pretty fast speed, but should not be enough speed for digital things, so what is the matter?
The last delay(11) is the bottleneck, the program works great when you use 11 as delay, but use 10 instead and all the program slow down! you might think, is because I'm sending too much through the Serial, but I tested this on the project too, only showing when a button is pressed or released.
When press I check the times on the monitor, with 11 is pretty fast, with 1 or 2 samples I can get 33-250ms of delay, but going down delay to 10 or lower the delay of the messages are over 750ms, when I print if something is pressed or released only prints if changes, so there is almost not data send through Serial. Because the point is only know when the Pad is pressed and released.
So, the delay part is one issue.
The second issue is... zeros.
When the delay is lower than 11, what happens is the HC-SR04 starts getting two interleaced measures, one with 0, and the measure.
13:10:20.143 -> Distance 231 :0
13:10:20.143 -> Distance 232 :231
Very weird, I tested use delay 10 and keep it getting samples, and then for some reason started working fine, but is almost luck, no idea why happens this.
If I try to get two samples and then use delay(11), still the zeros problem.
With this two issues, I'm unable to get better presicion with HC-SR04, but no idea why happens.
Someone know why?
(I'm using HC-SR04 because is cheap)
Thx!