Following the idea of creating some larger “musical” system using electrical engineering which I touched upon in the GLSL post, I think I
will probably require some sort of basic instrument as a starting point. I have been looking at some basic arduino sensors that I have to see what I can use to generate noise.
I decided to go with a HC-SR04 Ultrasonic Sensor. The HC-SR04 sensor measures distance away from the nearest object in it’s cone of vision. The sensor has a transmitter
and a reciever (called the trig and echo pin/sensor respectively). This sensor uses the concept of the speed of sound being a constant, with this assumption,
a sound wave is emitted from the trig emitter and received by the echo receiver. By using some basic math involving the speed of sound and the time
between ultra sound emission and receiving, it is possible to calculate the distance of an object from the sensor.
Here is an image detailing distance calculation, speed * time is divided by 2 due to the fact that the sound has to travel between the reciever and object twice.
I then began to wire the sensor, having it print the distance readings. I did some testing and the sensor fails to measure the
distance of very curved objects, thin objects, and very near objects which are all reasonable flaws for a sensor that relies on sound. Curved objects likely alter the reflection of sound waves, thin objects create a small reflection of sound compared to the sound going past the object
and very near objects are likely very strange due to the path that sound may have to travel to leave a transmitter and reach another sensor that is not in the same location. Anyways, after preliminary testing, it seemed
as long as the object used to reflect the ultrasound is reasonable, everything should function well.
After this sensor is functioning properly, I had to add a passive buzzer. Passive buzzers require square waves in order to function. The voltage must be rapidly alternating between
a high and low voltage in order to vibrate a magnet inside the speaker. When this magnet vibrates fast enough its sound can be measured in terms of musical notes. At around 262Hz, the corresponding note
becomes C4, the middle C on the piano. Using a table to find corresponding frequencies to notes, we can just set certain distances to certain frequencies by using the
tone() function in arduino, which handles any details to generate a square wave at a 50 percent duty cycle.
The sound was working pretty well but I noticed random fluctuations in the sound of the instrument despite my hands not being at the distance required to make that noise. I realized that there are sometimes incorrect readings that cause a large
spike in the pitch of the sound. I have recognized historical dilemmas like this in signal processing. I can either using a convolution filter or a simpler averaging filter to smooth out a dataset. I decided to go with the simpler apporach of using an averaging filter.
Using an array to store values and cycling through values by filling them up chronologically, I was able to calculate the average of the last 10 distance samples, then rounding it to the nearest integer to get very stable readings. Using the averaging over time,
rounding, as well as giving a generous range for each distinct musical note allowed me to make a pretty dependable instrument. In order to play a rest, I need to disable the speaker which I did by adding a button that acts as a switch, creating an open circuit whenever
the button is not pressed. This effectively gives me an instrument that can play any melody that does not have overlapping notes, though perhaps a strong understanding of music theory could still make me able to replicate any musical combination with a single speaker since it seems to be possible with
modern technology.
^the main behavior of the code is shown here, stabilization[] is used as the sliding averaging window, the calculations for distance as well as rounding are heres as well Link to arduino code
I then wanted to see if I could incorporate an LCD screen into the build as well. For example, displaying notes that you are playing could be useful, or even some variation of sheet music could be very insteresting as well. I followed this simple hello world tutorial to get it running,
most of the work done in this section was correctly wiring the circuit. The final build is quite busy and dense, especially with so many devices all coneected to the same power source and ground. I have attached some images/videos of the build below:
Me playing the chorus of my current favorite song!
Edited Version:
Link to arduino code