Description
In this lab, a tiger toy made out of paper and cardboard was created. When one attempts to sneak up on the tiger from behind it, a photocell embedded in the back of the tiger will sense a decrease in light. This will trigger the piezo speaker that is attached to the tiger to play the "Eye of the Tiger" melody, and a green LED in the tiger's eye will blink to the tune of the music. As an extra bonus, if the light level is decreased to an especially low level, the song that is triggered will change to the "Billionaire" melody. The input trigger of the photocell light sensor occurs very close to the outputs of the piezo speaker and the green LED.
Looking back, I would have changed how I wrote the code for the melodies being played. Instead of creating a separate for loop for each note, I would have created 3 different arrays that contain the note's pulsewidth, the number of cycles to play the note, and the delay that follows after the note. This way, only two for loops could be used to to play a melody by iterating through the indexes of the song. This would be a much cleaner and elegant way to write the code. Next time, I will spend more time designing my code implementation before launching into it.
Components Used
1 - Arduino Uno
1 - 220 ohm resistor
1 - 10K ohm resistor
1 - green LED
1 - photocell
1 - piezo speaker
1 - breadboard
Code
/* Lab 5 - Eye of the Musical Tiger
* Brian McRae
* 3/5/13
*
* Modified Sound Serial (aka Keyboard Serial)
* ------------
*
* Program to play tones depending on the data coming from the serial port.
*
* 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 277 Hz 3610 1805
* D 294 Hz 3400 1700
* e 311 Hz 3215 1608
* E 329 Hz 3038 1519
* F 349 Hz 2864 1432
* g 370 Hz 2703 1352
* G 392 Hz 2550 1275
* a 415 Hz 2410 1205
* A 440 Hz 2272 1136
* b 466 Hz 2146 1073
* B 493 Hz 2028 1014
* c 523 Hz 1912 956
*
*
*
* (cleft) 2005 D. Cuartielles for K3
*
* Updated by Tod E. Kurt <tod@todbot.com> to use new Serial. commands
* and have a longer cycle time.
*
*/
int ledPin = 13;
int speakerPin = 7;
int photoCell = A0;
// note names and their corresponding half-periods
byte names[] ={
'C', 'd', 'D', 'e', 'E', 'F', 'g', 'G', 'a', 'A', 'b', 'B', 'c'};
int tones[] = {
1915, 1805, 1700, 1608, 1519, 1432, 1352, 1275, 1205, 1136, 1073, 1014, 956};
int serByte = -1;
int ledState = LOW;
int count = 0;
int brightness_val = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
brightness_val = analogRead(photoCell);
Serial.println(brightness_val);
digitalWrite(speakerPin, LOW);
if (brightness_val < 500 ) {
if (brightness_val > 400) {
// PLAY EYE OF THE TIGER SONG
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(1000);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(1200);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(1200);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//e
for( int i=0; i<150; i++ ) { // play it for 150 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[4]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[4]);
}
digitalWrite(ledPin, LOW);
delay(1500);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(1000);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(1200);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(1200);
//G
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[8]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[8]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//e
for( int i=0; i<150; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[4]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[4]);
}
digitalWrite(ledPin, LOW);
delay(1000);
}
}
// PLAY BILLIONAIRE SONG
if (brightness_val < 400) {
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(50);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(50);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(50);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(50);
//F
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(50);
//a
for( int i=0; i<100; i++ ) { // play it for 100 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[9]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[9]);
}
digitalWrite(ledPin, LOW);
delay(100);
//b
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[11]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[11]);
}
digitalWrite(ledPin, LOW);
delay(100);
//F
for( int i=0; i<150; i++ ) { // play it for 150 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//F
for( int i=0; i<150; i++ ) { // play it for 150 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[6]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[6]);
}
digitalWrite(ledPin, LOW);
delay(333);
//e
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[4]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[4]);
}
digitalWrite(ledPin, LOW);
delay(50);
//d
for( int i=0; i<30; i++ ) { // play it for 30 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[2]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[2]);
}
digitalWrite(ledPin, LOW);
delay(150);
//C
for( int i=0; i<100; i++ ) { // play it for 100 cycles
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[1]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[1]);
}
digitalWrite(ledPin, LOW);
delay(1000);
}
}