/* Play Melody
* -----------
*
* Tone created by (cleft) 2005 D. Cuartielles for K3
* Refactoring and comments 2006 clay.shirky@nyu.edu
* http://code.google.com/p/rbots/source/browse/trunk/StarterKit/Lesson5_PiezoPlayMelody/Lesson5_PiezoPlayMelody.pde
*
*
* added a Photocell that actives the song once darkness has fallen upon it
*
* added a Potentiometer to change speed of song
*/
#define c3 7634
#define d3 6803
#define e3 6061
#define f3 5714
#define g3 5102
#define a3 4545
#define b3 4049
#define c4 3816 // 261 Hz
#define d4 3401 // 294 Hz
#define e4 3030 // 329 Hz
#define f4 2865 // 349 Hz
#define g4 2551 // 392 Hz
#define a4 2272 // 440 Hz
#define a4s 2146
#define b4 2028 // 493 Hz
#define c5 1912 // 523 Hz
#define d5 1706
#define d5s 1608
#define e5 1517
#define f5 1433
#define g5 1276
#define a5 1136
#define a5s 1073
#define b5 1012
#define c6 955
#define R 0 // Define a special note, 'R', to represent a rest
int potPinA = 0;
int potPinB = 3;
int speakerPin = 8; // select the pin for the LED
int valueA = 0; // variable to store the value coming from the sensor
int valueB = 0; // variable to store the value coming from the sensor
int play = 0;
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}
//star wars theme
int melody[] = { f4, f4, f4, a4s, f5, d5s, d5, c5, a5s, f5, d5s, d5, c5, a5s, f5, d5s, d5, d5s, c5};//
int beats[] = { 21, 21, 21, 128, 128, 21, 21, 21, 128, 64, 21, 21, 21, 128, 64, 21, 21, 21, 128 };
//super mario theme
//int melody[] = {e5, e5, R, e5, R, c5, e5, R, g5, R, R, R, g4, R, R, R, c5, R, R, g4, R, R, e4};
//int beats[] = {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 , 16, 16, 16, 16, 16, 16, 8, 16, 8, 16, 16};
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
long tempo = 10000; // Set overall tempo
int pause = 1000; // Set length of pause between notes
int rest_count = 50; //<-BLETCHEROUS HACK; See NOTES
// Initialize core variables
int toneM = 0;
int beat = 0;
long duration = 0;
void tempoFunction(){
if (valueA < 50) {
tempo = 15000; // Slow tempo
}
else if (valueA > 51 && valueA < 499) {
tempo = 10000; // Middle Speed
}
else if (valueA > 500 && valueA < 999) {
tempo = 5000; // Fast
}
else if (valueA > 1000) {
tempo = 3000; // Fast
}
}
void playTone() {
long elapsed_time = 0;
if (toneM > 0) { // if this isn't a Rest beat, while the tone has
// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerPin,HIGH);
delayMicroseconds(toneM / 2);
// DOWN
digitalWrite(speakerPin, LOW);
delayMicroseconds(toneM / 2);
// Keep track of how long we pulsed
elapsed_time += (toneM);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}
void loop() {
digitalWrite(speakerPin, LOW);
valueA = analogRead(potPinA); // read the value from the sensor, between 0 - 1024
valueA = valueA;
Serial.print("Value A = ");
Serial.println(valueA);
valueB = analogRead(potPinB); // read the value from the sensor, between 0 - 1024
Serial.print("Value B = ");
Serial.println(valueB);
if ( valueB < 5)
{
tempoFunction();
for (int i=0; i<MAX_COUNT; i++) {
toneM = melody[i];
beat = beats[i];
tempoFunction();
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes...
delayMicroseconds(pause);
}
}
}