Last active
September 6, 2018 22:55
-
-
Save silbo/f52f74a7a6a2800a35f189fbc8706a77 to your computer and use it in GitHub Desktop.
Rudolph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Play Melody | |
* ----------- | |
* | |
* Program to play a simple melody | |
* | |
* Tones are created by quickly pulsing a speaker on and off | |
* using PWM, to create signature frequencies. | |
* | |
* Each note has a frequency, created by varying the period of | |
* vibration, measured in microseconds. We'll use pulse-width | |
* modulation (PWM) to create that vibration. | |
* We calculate the pulse-width to be half the period; we pulse | |
* the speaker HIGH for 'pulse-width' microseconds, then LOW | |
* for 'pulse-width' microseconds. | |
* This pulsing creates a vibration of the desired frequency. | |
* | |
* (cleft) 2005 D. Cuartielles for K3 | |
* Refactoring and comments 2006 [email protected] | |
* See NOTES in comments at end for possible improvements | |
*/ | |
// TONES ========================================== | |
// Start by defining the relationship between | |
// note, period & frequency. | |
const int c4 = 261; // Hz | |
const int d4 = 294; // Hz | |
const int e4 = 329; // Hz | |
const int f4 = 349; // Hz | |
const int g4 = 392; // Hz | |
const int a4 = 440; // Hz | |
const int b4 = 493; // Hz | |
const int c5 = 523; // Hz | |
// Define a special note, 'R', to represent a rest | |
const int R = 0; | |
// MELODY and TIMING ======================================= | |
// melody[] is an array of notes | |
const int melody[] = { | |
g4, a4, g4, e4, c5, a4, g4, | |
g4, a4, g4, a4, g4, c5, b4, | |
f4, g4, f4, d4, b4, a4, g4, | |
g4, a4, g4, a4, g4, a4, e4, | |
R | |
}; | |
// beats[] is and array which sets each note's relative | |
// length (higher #, longer note) | |
const int beats[] = { | |
16, 16, 32, 32, 32, 32, 96, | |
16, 16, 16, 16, 32, 32, 128, | |
16, 16, 32, 32, 32, 32, 96, | |
16, 16, 16, 16, 32, 32, 128, | |
128 | |
}; | |
// pin numbers of corresponding lights, or "0" if no light | |
const int lights[] = { | |
9, 2, 3, 2, 3, 9, 3, | |
9, 2, 3, 9, 2, 3, 2, | |
9, 3, 9, 2, 3, 9, 2, | |
9, 3, 2, 9, 3, 2, 3, | |
0 | |
}; | |
// Melody length, for looping | |
const int melody_length = 29; | |
// Set overall tempo | |
const int tempo = 15; | |
// SETUP ============================================ | |
// Set up speaker on pin A5 | |
const int speakerPin = A5; | |
void setup() { | |
pinMode(2, OUTPUT); | |
pinMode(3, OUTPUT); | |
pinMode(9, OUTPUT); | |
} | |
// LET THE WILD RUMPUS BEGIN ============================= | |
void loop() { | |
// Set up a counter to pull from melody[] and beats[] | |
for (int i = 0; i < melody_length; i++) { | |
// Tone duration | |
long duration = beats[i] * tempo; | |
digitalWrite(lights[i], HIGH); | |
// Play the note | |
tone(speakerPin, melody[i], duration); | |
digitalWrite(lights[i], LOW); | |
// Wait the note to finish | |
delay(duration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment