PComp | Week 4 | Theremin v.2
Final draft of the week: testing hand positions and notes. Also, trying to play the Do-Re-Mi song.
For this week, I wanted to improve on the Theremin prototype I created last week. There are several things I wanted to refine:
Filter and map input from the IR sensor properly, resulting in more stable pitch output.
Create a volume knob because the default volume is way too noisy.
Make it so I could play a song with the prototype.
I started with googling how to control the volume of the speaker output.
There wasn't a built-in function, but Connor Nishijima found a hack which involves PWM in a frequency beyond our hearing capability, so that the speaker can sound quieter / louder.
I used his library and a potentiometer to make the volume controls.
potVal = analogRead(potPin);
volume = map(potVal, 0, 1023, 0, 300);
vol.tone(speakerPin, melody[average-1], volume);
Next up is to smoothen the input of IR sensor.
The input values were very unstable. My idea then was to take the average values instead of individual ones. I achieve this using the following function:
//map the IR raw values to pitch[0] (middle C) to pitch [5] (A4)
pitch = map(distance, 150, 550, 0, 5);
//smoothen pitch values total = total - readings[readIndex]; readings[readIndex] = pitch; total = total + readings[readIndex]; readIndex = readIndex +1;
if(readIndex >= numReadings) { readIndex = 0; }
average = total / numReadings; delay(5);
This particular line
//map the IR raw values to pitch[0] (middle C) to pitch [5] (A4)
pitch = map(distance, 150, 550, 0, 5);
Was really hard to nail. It took me a lot of trial and error to get the correct values mapped.
I tried mapping IR values to actual sound frequency, so that I could get smooth glissando sound like real theremin, but the output was too unstable. And even with the values mapped to a discreet array of notes, I could only get 4 stable notes from moving my hand away/closer to the IR sensor.
I think it's because the distance to output relationship is an exponential instead of linear one.
I had to resort to making a switch button into an octave / note switch, in order for me to get full 8 notes, from C4 to C5.
And the end result is unfortunately still far from real theremin sound. I want smooth glissando sound and the ability to mimic vibrato.
Note to self for next iteration:
Consider using other sensors, maybe stretch sensor to get more stable output
Try using another value-smoothing algorithm
Have another sensor as vibrato control
Consider using mp3 shield to output less-annoying sounds