Daniel Rowlands Physical computing project 2- Complex Arduino theremin.

After deciding to continue with a theremin, I looked into various ways of making the project more complex. After experimenting with different types of sensor, I decided to add an ultrasonic distance sensor to control the volume of the sound output. I did some research into coding an ultrasonic sensor to control volume, but quickly realised that it would not be easy so I would have to find an alternative way of doing it.

I knew that using a potentiometer to control volume was easy, so I wanted to know if I could somehow control a potentiometer using an ultrasonic sensor. While looking into it I found a project where someone had used a servo to turn a potentiometer, so I decided to use my ultrasonic sensor to control a servo which would turn a potentiometer.

http://makezine.com/projects/arduino-theremin/

VIDEO0076_0000020492

final theremin diagram_bb

After building the project I needed to write the code as there was no code with the project online,

After looking around for a while I found bits and bobs that I could use to code my project, I used some ultrasonic sensor code from instructables.

http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/

Most of the rest of the code was found in various places online, although it needed a lot of modifying after to get it to upload. Once I managed to upload the code I had a lot of trouble trying to get the values right so that the volume range would be sufficient.

#include <NewPing.h>
#include <Servo.h>
#define trigpin 2
#define echopin 4
#define MAX_DISTANCE 300
NewPing sonar(trigpin, echopin, MAX_DISTANCE);

Servo myservo;

int val;

int prPin = 0;

int prReading;

int buzzerPin = 5;

long buzzerFreq;

long BUZZ_FREQ_MAX = 2500;

long PR_MAX = 1023;

//trin n echo pin se

void setup() {
Serial.begin(115200);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(buzzerPin, OUTPUT);
myservo.attach(10);
}

void loop() {
prReading = analogRead(prPin);

buzzerFreq = (prReading * BUZZ_FREQ_MAX) / PR_MAX;

buzz(buzzerPin, buzzerFreq, 10);

delay(30);
unsigned int uS = sonar.ping();
Serial.print(“Ping: “);
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println(“cm”);
unsigned int cm = uS / US_ROUNDTRIP_CM;
if(cm >= 45) {
val= 115;
}
if(cm <= 39) {
val= 170;
}
if(cm <= 37) {
val= 170;
}
if(cm <= 33) {
val= 170;
}
if(cm <= 29) {
val= 170;
}
if(cm <= 27) {
val= 170;
}
if( cm <= 25) {
val = 170;
}
if(cm <= 24) {
val= 165;
}
if(cm <= 22) {
val= 160;
}
if(cm <= 20) {
val= 155;
}
if(cm <= 18) {
val= 150;
}
if(cm <= 16) {
val= 145;
}
if(cm <= 14) {
val= 140;
}
if(cm <= 12) {
val= 135;
}
if(cm <= 10) {
val= 130;
}
if(cm <= 8) {
val= 125;
}
if(cm <= 6) {
val= 120;
}
if(cm <= 4) {
val= 115;
}

myservo.write(val);
delay(50);
}

void buzz(int targetPin, long frequency, long length) {

long delayValue = 5000000/frequency/2;

long numCycles = frequency * length/ 1000;

for (long i=0; i < numCycles; i++){

digitalWrite(targetPin,HIGH);

delayMicroseconds(delayValue);

digitalWrite(targetPin,LOW);

delayMicroseconds(delayValue);

}}

Once I had everything working I simply made a cylindrical container for the theremin, added a switch and made some simple instructions and plugged in a large speaker driver as the one I had was far to quiet.

.presentation board

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s