The Singing Pen
Description
Note: this is not yet finished as I needed long wires soldered to the light sensor.
The idea is a pen that sings melodiously when it's uncapped. There is a photosensor embedded next to the tip and when the light drops below a specific value, the piezo speaker, taped onto the other end of the pen, is activated. The circuit is quite simple and can be used for a number of 'light activated singing devices' such as a box (sings when opened), etc. The main concition is that when closed the light sensor will be in the dark. The code is the 'sing melody' code except with the light sensor reading stuck in there. Inside the look, the value of the light sensor is checked. If above a specific point, the music is played.
The pen I chose is a a Bic - it's easily dissasembled, cheap, and has a black cap. One catch is that the cap has a few air slits in it (this is why a black marker with a solid cap would do better). I decided to block the air slits by stuffing paper inside the cap.
The circuit, shown below, is quite simple and works - the piezo sings when light sensor is not covered. However, as noted above, the final dissassebly required longer wired and will be done in the next few days.

pen-parts

pen-circuit
Materials
Light sensor, piezo (as speaker), pot (for volume control), red/red/brown/gold resistor (for light sensor), Bic pen.
Code
int ledPin = 13;
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[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
int potPin = 0; // light sensor pin
int potVal = 0; // light sensor output
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
Serial.begin(9600);
}
void loop() {
potVal = analogRead(potPin); // read value from the sensor
//potVal = potVal*2; // process the value a little
Serial.print("pot val = ");
Serial.println(potVal);
if (potVal > 60) {
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<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(50);
}
}
}
}
}
}