Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Description
I created a stress ball that plays soft music when a person squeezes on it. The harder the person squeezes, the slower the beat will be and the music will play a lot slower. The music that plays is the prelude from Final Fantasy. I thoguht that music was very soothing and would help people calm down when being stressed.
Materials
- Pezio Speaker
- Force sensor
- Squishy Heart Shaped Stress ball
- Post-it note
- Electrical Tape
Arduino Code
int speakerPin = 13;
int forcePin = 5;
int length = 13; // the number of notes
char* notes[] = {"A#2","C2", "D2", "F2", "B2", "C3", "D3", "F3", "A#3", "C4",
"D4", "F4", "A#4", "C5", "D5", "F5", "A#5","C6", "D6", "F6", "A#6",
"F6","D6","C6","A#5","F5","D5","C5","A#4","F4","D4","C4","A#3","F3",
"D3","C3","B2","F2","D2","C2"}; // a space represents a rest
int beats[] = { 2, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 2, 5 };
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[] = {"A#2","C2", "D2", "F2", "B2", "C3", "D3", "F3", "A#3", "C4",
"D4", "F4", "A#4", "C5", "D5", "F5", "A#5","C6", "D6", "F6", "A#6"};
int tones[] = {4290,7644,6810,5727,4050,3822,3405,2864,2145,1911,
1703,1432,1073,956,851,716,536,478,426,357,268};
// 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);
}
}
}