Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
Author: Erin Knight
Description:
In this week's lab, we worked with a piezo speaker to create a caucaphony of output wonderfulness. We used sample code to make the piezo play a melody, as well as play tunes based on our input. We also created a theremin which played tones based on input from a photocell (image below).
Assignment:
Create an artifact that is both input and output.
Materials Used:
The Lion Sings Tonight:
I placed a force sensor and piezo in a stuffed lion and adapted/developed arduino code so that when you press the lion's hand, he plays "The Lion Sleeps Tonight". So he is both input (squeezing the hand) and output (singing for you).
Other ideas:
I was attempting to set it up so that if you pressed lightly, he would sing and if you pressed too hard, he would roar but I couldn't get the code to work. Luckily I have the programming course with Patrick tomorrow so maybe I will be able to be more crafty moving forward.
Pictures:
What's inside the lion:
Code:
/* Make the Lion Roar or Sing
* -----------
*
* Program to play the melody of the Lion Sleeps Tonight when you press the lion's paw.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
* D PW = 851
* (cleft) 2005 D. Cuartielles for K3
*/
int sensorPin = 0; // select the input pin for the sensor
int val = 0; // variable to store the value coming from the sensor
int speakerOut = 6;
int ledPin = 13;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 851};
byte melody[] = "4g4a4b6a4b4C4b4a6g4a4b4a4g2b9a";
byte roar[] = "9c9c9c";
int statePin = 0;
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
val = analogRead(sensorPin);
Serial.println(val/4);
if ((val/4) > 200) {
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<9;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
}