arduino intro – Justin Franklin
Discrete pitch theremin
For this week’s assignment in the makerspace 490 class, we were introduced to arduinos and the arduino IDE. I have some experience programming, but not with arduinos. It was something that i’ve been meaning to get into, so I was definitely excited to get started working with them.
The first thing we did in class was get comfortable using the arduino IDE. We worked off of a program that caused an LED to blink on and off. Then we began changing the code to create an SOS signal. Then we attempted to control the light of the LED with a touch sensor. This was all very fun, and I was sort of surprised how simple it was.
I thought about what I could do for my project but I wasn’t sure for the longest time. I wanted to make a car alarm at first, but one of the requirements was to have multiple thresholds, and I couldn’t think of how to incorporate that. The sensor that came in my kit was an ultrasonic sensor, one that uses doppler to detect the distance of an object from the sensor. I thought it might make a good way to control sound just by waving your hand.
The Theremin is instrument that produces sound without the player touching it. It uses two antennas to detect the proximity of a players hand to the instrument, and changes pitch and volume accordingly. Although very interesting, the theremin is a notably difficult instrument to play, due to its lack of discrete pitches. This means that as you move your hand you continuously sweep through the frequency spectrum, without ever locking onto a tuned note. The only way to stop on a note is to do so by ear, and playing a string of notes or a melody, is exponentially more difficult.
My idea was to use the ultrasonic sensor of the arduino to create a similar instrument to the theremin but one that utilized discrete pitch values, thus being easier to play, while maintaining the novelty of playing an instrument without actually touching it.
So, I decided to get to work and begin tinkering with this thing. I first attempted to do this on my macbook at home. To my dismay, it didn’t work. My macbook would not recognize the arduino was plugged into it. After some research I found that although there was a fix to this, it required a little too much for me to be comfortable with, so I decided to go to the fablab and use the computers there. Once there, I began looking for some code from others that where using the sensor. Luckily, there was a lot, and I decided to use some of it to test the supersonic sensor. From the code, I could see which pins were being sent to different parts of the sensor. I assembled the sensor accordingly. I then took a look through the code to see what was going on. I have a vague familiarity with the details of how doppler works, so i felt like I understood pretty much what was going on. I also grabbed a piezo speaker to use for sound output.
My first go at this just made a sustained beep noise that was very unpleasant, and I quickly unconnected the speaker. But I was actually pretty excited by that, because honestly just getting sound on my first try was sort of a milestone. Something wasn’t working quite right though. The sensor didn’t seem to be changing values at all. This problem took me a while to figure out until I realized I had placed my variables in the setup loop instead of the main loop. This meant that the sensor only polled one value at setup time, but never refreshed it. Once I moved the variables down to the correct place, things were looking nice. Then I just included a big IF ELSE statement that determines what to do if the object in front of the sensor is so many inches away from it. I made seven separate states, depending on distance of the object, each state corresponding to a different note of an A major scale. Unfortunately the tone() function of the arduino only takes integers for its frequency input value, so the tuning is not exact, because I had to round to the nearest integer. The difference is not very substantial.
int echoPin = 12; // Echo
long duration, cm, inches;
int piezoPin = 8;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = (duration/2) / 74;
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = (duration/2) / 74;
Serial.print(inches);
if(inches < 4){
tone(piezoPin, 440,100);
delay(300);
}
else if(inches<5){
tone(piezoPin, 494,100);
delay(300);
}
else if(inches<6){
tone(piezoPin,554,100);
delay(300);
}
else if(inches<7){
tone(piezoPin,587,100);
delay(300);
}
else if(inches<8){
tone(piezoPin,659,100);
delay(300);
}
else if(inches<9){
tone(piezoPin,740,100);
delay(300);
}
else if(inches<10){
tone(piezoPin,831,100);
delay(300);
}
else{
tone(piezoPin, 0, 100);
delay(300);
}
}
created by Rui Santos, https://randomnerdtutorials.com

