Alright, so this is a joint commlab/physcomp post. I’m supposed to document one of my projects for commlab, so it all worked out.
Here’s the video.
I started toying around with the servo, but I really wanted to play with the piezo. I wanted to create a different sound based on the user’s input. I had a force sensor, so I jury rigged some code up to do interesting stuff. At first there was a problem when I tried to upgrade the arduino program and found that it was broken. Oh well. Back down to version 0011.
So, what i was trying to do is let a user make a 3-note song. But this sound stuff is harder than it seems. I kept having trouble getting the delay right for to seperate the notes, so really what you’re hearing in the video is 3 notes being played very fast, but it sounds like one sound. Still, it’s generally what I was aiming for. I need to figure out the delays more. But not now. Later.
And here’s the code:
int speakerOut = 9; // Set up speaker on a PWM pin (digital 9, 10 or 11)
int analogValue = 0; // the value returned from the analog sensor
int analogPin = 0; // the analog pin that the sensor's on
void setup() {
pinMode(speakerOut, OUTPUT);
Serial.begin(9600);
}
int notes[] = { 261, 261, 261 };
int times[] = { 1, 1, 1 };
int i=0;
int j=0;
int state=0;
unsigned long time=0;
void loop() {
if(j>3){j=0;}
if(j==0)
{
Serial.println("Hi! Enter first note.");
getNote(j);
Serial.println(notes[j]);
Serial.println(times[j]);
Serial.println("Ok! Processing the note.");
delay(2000);
Serial.print("enddelay");
}else if(j==1)
{
getNote(j);
Serial.println("Hi! Enter second note.");
Serial.println(notes[j]);
Serial.println(times[j]);
Serial.println("Ok! Processing the note.");
delay(2000);
}else if(j==2)
{
getNote(j);
Serial.println("Hi! Enter 3rd note.");
Serial.println(notes[j]);
Serial.println(times[j]);
Serial.println("Ok! Processing the note.");
delay(2000);
}else if(j==3)
{
Serial.println("Who's awesome? You're awesome!");
Serial.println("Ok, listen to your tune and whenever
you're ready to stop, just press the button.");
playIt();
Serial.println("Back!");
delay(2000);
}
j++;
}
//gets the user input note and how long it took them to input it
void getNote(int i)
{
time=millis();
int analogValueOld=0;
while(1)
{
analogValue = analogRead(analogPin);
if(analogValue > 4)
{
while(analogValue > 4)
{
analogValue = analogRead(analogPin);
if(analogValueOld < analogValue) {analogValueOld=analogValue;}
}
notes[i]=map(analogValueOld,0,870,3830,1912);
times[i]=abs(millis()-time);
return;
}
}
}
//play the tune till user stops it
void playIt()
{
while(1)
{
//Serial.println(analogValue);
analogValue = analogRead(analogPin);
while(analogValue < 50)
{
if(analogValue >=50)
{
digitalWrite(speakerOut,LOW);
return; // in theory, this should return out of the function
//but it's acting like a break
}
for(int i=0;i<3;i++)
{
for(int j=times[i]*1000;j>0;j-=(notes[i]/2))
{
digitalWrite(speakerOut,HIGH);
delayMicroseconds(notes[i]/ 2);
digitalWrite(speakerOut,LOW);
delayMicroseconds(notes[i]/ 2);
}
delayMicroseconds(times[1]*times[2]*times[3]*150);
}
analogValue = analogRead(analogPin);
}
return; // so that's why i had to use two return statements
}
}