A shout out to @Frank Buss for optimizing the code. I was struggling to solve some of the bugs there , for quite some time.
Here is his description of what he has done -
"
Hi @Debargha Ganguly. As promised, I wrote a better Arduino script. This is your original code from your paper:
https://gist.github.com/FrankBuss/d78911fae66d83bbf234b28f27b97460
It has several problems. I rewrote it completely with interrupts. The improvements:
- the measurement interval is fixed at 16 Hz, so that it has a 60 ms measurement cycle, as suggested by the datasheet of the HC-SR04
- the output tone is continuous and with less distortions than your version (there are still some minor distortions when the tone value is changed, but this would require more work to make it perfect)
- on timeout, the speaker is disabled
- the main loop is free to do other things, all measurements and the speaker output is done in the interrupt, which might be useful for your speech output, to put the distance data out at a slower rate than what you hear in the speaker
- high end of the frequency is changed to 5 kHz for 4 m, instead of 15 kHz, for a wider range
- the resolution for the distance is changed from cm to mm
"
The GitHub repository can be found here -
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.