Description:
In this lab, the goal was to get familiar with the piezo speaker as a form of output. I was immediately intregued by the musical portion of the labs, and immediately went to work searching for music that was in the key of C major, as the piezo speaker does not seem to be able to play any sharps or flats. As I browsed the list, I thought of ideas that would go along with sensors, and thought of the connection I make with music and the time of day. As 'Kokomo' by the Beach Boys is in C major, I thought of beach drives and light and how music serves to enhance that experience.
I ended up modifying the play melody code to play 'Kokomo' by The Beach Boys, and connected the photocell sensor so that it would play when it was exposed to 'sun' light, and stop when I put the sunglasses on.
COMPONENTS USED
1- Arduino Uno
1- piezo buzzer
2- 10 k Resistor
1- Breadboard
1- Photocell sensor
1- Sunglasses
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* 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
* E
* F
* G
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int sensorPin = 0;
int speakerOut = 7;
int val= 0;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'p'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 852, 759, 716, 638, 568, 506, 0};
byte melody[] = "2b2p2C2p2C4p2b2p2C2p2C4p4E2p2C2p2C2p2b2p2C2p2C2p2E2p2F2p2E2p4p2E2p2F2p2F2p2p2p4G2p2F2p2G2p2F2p2F2p2E2p2F2p2F8p8p";
// 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 = 40;
int statePin = LOW;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
}
void loop() {
val=analogRead(speakerOut)/5;
Serial.println(val);
analogWrite(speakerOut, val/4 );
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(500);
}
}
}
}
}
- Login to post comments