/* PlayMelody v3 affecting speed via Knob * ------------- * * Plays a melody stored in an array a la Nokia ringtone style * Durations of notes are Stored in just one number * * <http://blushingboy.org> * (c) 2008 D. Cuartielles for K3 */ // define the notes as finals, just to save // variable space, you could add some more // notes //#define TIC 50 // basic note duration int TIC=50; #define SILENCE 20 // basic separation between notes int speakerOut = 3; 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 MAX_COUNT = 24; // it is the amount of notes in the melody void playNote(int pin, int tone, int duration) { Serial.println(tone); for(int i=0; i<duration; i++) { digitalWrite(pin, HIGH); delayMicroseconds(tone); // the time here will determine the tone digitalWrite(pin, LOW); delayMicroseconds(tone); // the time here will determine the tone } } void setup() { pinMode(speakerOut, OUTPUT); Serial.begin(9600); } void loop() { // ENGINE for (int i = 0; i < MAX_COUNT; i++) { TIC = analogRead(5); if (TIC < 10) { } else { TIC = TIC / 10; if (melody[i*2 + 1] == 'p') { // make a pause of a certain size digitalWrite(speakerOut, LOW); delay(TIC*(melody[i*2] - 48)); // NOT VERY PRECISE TIMING } else { for (int j=0;j<8;j++) if (names[j] == melody[i*2 + 1]) playNote(speakerOut, tones[j], (melody[i*2] - 48) * TIC); } } delayMicroseconds(SILENCE); } }