User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Froggie Goes a-Courtin'

Submitted by agreiner on Tue, 10/07/2008 - 21:39

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators: agreiner

Description

A little white box becomes a music box. When you open the lid, it starts to play the song Froggie Went a-Courtin'. Inside, all you see is a little froggie on a bed of soft white fluff. Beneath the fluff is a photocell that is sensitive enough to detect light through the fluff, but not if the lid is closed. Also under the fluff is a piezo speaker. On the circuit board, there is an LED that indicates activity.

Materials

Arduino board
wires
breadboard
two resistors
LED
piezo speaker
photocell

Arduino code

/* Music Box
* ------------
*
* Program to play a song depending on the input from a photo cell.
*
* 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            588 Hz            1701     850
* E            658 Hz            1520     760
* F            698 Hz            1432     716
* G            784 Hz            1276     638
*
* Annette Greiner 2008
*
*/
int potPin = 0;
int ledPin = 9;
int speakerPin = 7;

// note names and their corresponding half-periods 
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G' }; 
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 850, 760, 716, 638};
//froggie went a-courtin'
byte song[] = "ccffffffaaggffddccffffffccffffffffaCCCCCCCDDCCaaffggfffffgaaffddccffffffccffffff";
int songlength = 80;
//alternative song - Study by Mauro Giuliani
//byte song[] = "CgggCgDgggDgEgFgEgDgggggFgggGgEgggCgDaaabgcCgecc";
//int songlength = 48;
int ledState = LOW;
int count = 0;
int photoval = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
beginSerial(9600);
Serial.println("ready");
}

void loop() {
char songByte;
digitalWrite(speakerPin, LOW);
for(int j=0; j<songlength; j++){
songByte = song[j];
if (songByte != -1) {
Serial.print(songByte,BYTE);
ledState = !ledState;           // flip the LED state
digitalWrite(ledPin, ledState); // write to LED
}
photoval = analogRead(potPin);
if(photoval >= 10){ //only play if the photocell is getting some light
for (count=0;count<=12;count++) {  // look for the note
if (names[count] == songByte) {  // ahh, found it
for( int i=0; i<50; i++ ) {   // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
}
delay(10);//gives a crisper finish to each tone
}
}
}//photoval>10 loop

}//song loop
}

photo