Input/output- De-stressing Musical Eff

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators:

I am re-inventing a foam stress egg, which is originally designed to be squeezed when stressed, but now it will play soothing music the harder  you squeeze.

I have easily placed a force sensor and piezo speaker inside the egg. The foam material of the egg is perfect for pressure sensing, and hopefully the calmer melodies will help relax the stressed squeezer.

 

Here is the inital code that I came up with. I composed a little meloody in minor that is more calming. However, to make it more interesting, I am interested in using my program max/msp to play calming soundfiles, such as wind chimes or sea shells, and then with the added force sensor, filter the sound higher, yet slower. Unfortunately Arduino is not working with this program yet, but I will try and make the force sensor talk to  my computer with a different microcontroller.

I think realxing sounds, rather than a melody, would be a better application for this excercise.

int speakerPin = 7;
int forcePin = 5;

int length = 9; // the number of notes
char* notes[] = {"C2","C2", "D#2", "D2", "C2", "F2", "D#", "D2", "C2",}; // a space represents a rest

int beats[] = { 2, 2, 2, 2, 4, 4, 4, 4, 4 };
int tempo = 100;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char* note, int duration) {
char* names[] = {"C2","C2", "D#2", "D2", "C2", "F2", "D#", "D2", "C2",};
int tones[] =   {1915, 1915, 1800, 1700, 1915, 1432, 1800, 1700, 1915};

// play the tone corresponding to the note name
int nums = sizeof(names)/sizeof(char*);
for (int i = 0; i < nums; i++) {
if (strcmp(names[i], note)) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
/*
int forceVal = analogRead(forcePin); // read the value from the sensor, 0-1023
forceVal = forceVal;
Serial.print("F");
Serial.println(forceVal);       // writing the value to the PC via serial connection
delay(10);

if (forceVal < 100)
tempo = tempo-(100*(forceVal+1));
*/
for (int i = 0; i < length; i++) {
int forceVal = analogRead(forcePin); // read the value from the sensor, 0-1023
if (forceVal < 1023) {
Serial.print("F");
Serial.println(forceVal);       // writing the value to the PC via serial connection
delay(10);

if (forceVal < 100)
tempo = tempo-(100*(forceVal+1));  

if (strcmp(notes[i], " ") == 0) {
delay(tempo); // rest
} else {
playNote(notes[i], tempo);
}

// pause between notes
delay(tempo / 2);
}
}

}