Arduino Code
/*
* 10/03/07 Tangible User Interfaces Lab 5
* Eun Kyoung Choe
*
* Gets input from photocell, plays different music depending on the light value;
* Applied to the ringtone of a cell phone, which automatically changes.
* Light or dark (outside/inside, day/night), etc.
*
* 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
*
* (cleft) 2005 D. Cuartielles for K3
*/
int potPin = 0;
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[52];
byte morning[] = "2c2p2c2d2e2p2e2f2g2p2a2g8e2g2p2f2e8d2f2p2e2d8c8p"; // korean folk song
byte night[] = "2c2p2c2p2g2p2g2p2a2p2a2p8g2f2p2f2p2e2p2e2p2d2p2d2p8c"; // twinkle twinkle little star
int val = 0; // input from the photocell
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(speakerOut, OUTPUT);
beginSerial(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerOut, LOW);
val = analogRead(potPin);
if (val>900) { // if it's morning
for ( int n=0 ; n<52 ; n++ )
{
melody[n] = morning[n]; // play morning song
}
}
else { // if it's night
for ( int n=0 ; n<52 ; n++ )
{
melody[n] = night[n]; // play night song
}
}
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;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);
}
}
}
}
}