Arduino – making pretty sounds

Posted: April 8, 2015 in technical
Tags: , ,

Cristina is girlie and she likes PINK! Unfortunately, the LED part is the only one I couldn’t make work ๐Ÿ˜ฆ ๐Ÿ˜ฆ hence no pink display for me.

The next best thing: we make pretty sounds!

Parts

1. Piezo (I have no proper speaker): well, I use it to make the (pretty) sounds

2. Light sensor + 1K Ohm resistor

I used this only in the beginning, as I wanted to make theย piezo play sounds based on the light intensity. As I am not able to control the light intensity in my room in a good-enough way to make sounds, I decided to use the distance variation to play different tones (see 3).

3. Ultrasonic sensor: well, I use it to measure the distance from the sensor to my palm and based on this variation I scale the distance from CM to Hz and play the tones within a range of musical notes:

int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440, 450};
// mid C, C#, D, D#, E, F, F#, G, G#, A, higher

How did I end up with these values?

Well, I have not. That’s just how they are. The frequency of Mid C is 261.6 Hz, and the others as above. What I tried then to ensure is that I map the distances from the Ultrasonic reader to a range of frequencies which “make sense” (between 200 and 300 Hz). As I would just flip my hand around the sensor to no longer than 100 cm, I just decided to add 200 to the distance computed by the sensor, in order to create the pitch (I am sure there are much better ways to do this).

4. Wires (the Ultrasonic sensor needs 4 – as it has 4 PINs: VCC, Trigger, Echo, Ground)

5. Yeah, and the Arduino board and breadboard, otherwise: no magic

Picture

20150408_191513

Code

v0.1 (using the Light sensor)

int speakerPin = 12;
int photocellPin = 0;

int numTones = 11;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440, 450};
// mid C C# D D# E F F# G G# A others

void setup()
{
for (int i = 0; i < numTones; i++)
{
tone(speakerPin, tones[i]);
delay(500);
}
noTone(speakerPin);
}

void loop()
{
int reading = analogRead(photocellPin);
int pitch = 200 + reading / 4;
if (pitch <= 261)
{
tone(speakerPin, tones[0]);
}
else if (pitch > 261 && pitch <= 277)
{
tone(speakerPin, tones[1]);
}
else if (pitch > 277 && pitch <= 294)
{
tone(speakerPin, tones[2]);
}
else if (pitch > 294 && pitch <= 311)
{
tone(speakerPin, tones[3]);
}
else if (pitch > 311 && pitch <= 330)
{
tone(speakerPin, tones[4]);
}
else if (pitch > 330 && pitch <= 349)
{
tone(speakerPin, tones[5]);
}
else if (pitch > 349 && pitch <= 370)
{
tone(speakerPin, tones[6]);
}
else if (pitch > 370 && pitch <= 392)
{
tone(speakerPin, tones[7]);
}
else if (pitch > 392 && pitch <= 415)
{
tone(speakerPin, tones[8]);
}
else if (pitch > 415 && pitch <= 440)
{
tone(speakerPin, tones[9]);
}
else
{
tone(speakerPin, tones[10]);
}
}

v0.2 (using the Ultrasonic sensor)

int speakerPin = 12;
int trigger=7;
int echo=6;
long time=0;
long dist=0;

int numTones = 11;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440, 450};
// mid C C# D D# E F F# G G# A others

void setup()
{
Serial.begin(9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);

for (int i = 0; i < numTones; i++)
{
tone(speakerPin, tones[i]);
delay(500);
}
noTone(speakerPin);
}

void loop()
{
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
time = pulseIn(echo, HIGH);
dist = (time/2) / 29.1;

if ( dist >= 500 || dist <= 0)
{
Serial.println(“No measurement”);
}
else
{
Serial.println(dist);

int pitch = 200 + dist ;
if (pitch <= 261)
{
tone(speakerPin, tones[0]);
}
else if (pitch > 261 && pitch <= 277)
{
tone(speakerPin, tones[1]);
}
else if (pitch > 277 && pitch <= 294)
{
tone(speakerPin, tones[2]);
}
else if (pitch > 294 && pitch <= 311)
{
tone(speakerPin, tones[3]);
}
else if (pitch > 311 && pitch <= 330)
{
tone(speakerPin, tones[4]);
}
else if (pitch > 330 && pitch <= 349)
{
tone(speakerPin, tones[5]);
}
else if (pitch > 349 && pitch <= 370)
{
tone(speakerPin, tones[6]);
}
else if (pitch > 370 && pitch <= 392)
{
tone(speakerPin, tones[7]);
}
else if (pitch > 392 && pitch <= 415)
{
tone(speakerPin, tones[8]);
}
else if (pitch > 415 && pitch <= 440)
{
tone(speakerPin, tones[9]);
}
else
{
noTone(speakerPin);
}
} // end distance measurement
delay(1000);
}

Yes, I did use the Serial.println for debugging, printing is the best form of debug ๐Ÿ™‚ ๐Ÿ™‚ ๐Ÿ™‚

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