change tone sounds by altering the waveforms
Dependencies: mbed
Fork of 5_songs by
main.cpp@5:7003c9f6d633, 2016-05-16 (annotated)
- Committer:
- maclobdell
- Date:
- Mon May 16 18:42:29 2016 +0000
- Revision:
- 5:7003c9f6d633
- Parent:
- 4:02dc2ab8eabb
- Child:
- 6:406f73a0eb49
slowed the BPM down a bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 0:0fb17f0d39e7 | 1 | #include "mbed.h" // this tells us to load mbed related functions |
maclobdell | 1:8922cc02aec7 | 2 | #include "tones.h" // list of all the tones and their frequencies |
maclobdell | 0:0fb17f0d39e7 | 3 | |
maclobdell | 0:0fb17f0d39e7 | 4 | PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation) |
maclobdell | 0:0fb17f0d39e7 | 5 | |
maclobdell | 5:7003c9f6d633 | 6 | static int BPM = 80; |
maclobdell | 0:0fb17f0d39e7 | 7 | |
maclobdell | 0:0fb17f0d39e7 | 8 | static void silence() { |
maclobdell | 0:0fb17f0d39e7 | 9 | buzzer.write(0.0f); // silence! |
maclobdell | 0:0fb17f0d39e7 | 10 | } |
maclobdell | 0:0fb17f0d39e7 | 11 | |
maclobdell | 0:0fb17f0d39e7 | 12 | // this is our function that plays a tone. |
maclobdell | 0:0fb17f0d39e7 | 13 | // Takes in a tone frequency, and after duration (in ms.) we stop playing again |
maclobdell | 0:0fb17f0d39e7 | 14 | static void play_tone(int tone) { |
maclobdell | 2:610196c7b41c | 15 | buzzer.period_us(1000000/(tone)); |
maclobdell | 0:0fb17f0d39e7 | 16 | buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud |
maclobdell | 0:0fb17f0d39e7 | 17 | } |
maclobdell | 0:0fb17f0d39e7 | 18 | |
maclobdell | 0:0fb17f0d39e7 | 19 | static void play_song(int notes_left, int* melody, int* duration) { |
maclobdell | 1:8922cc02aec7 | 20 | |
maclobdell | 0:0fb17f0d39e7 | 21 | // YOUR CODE HERE |
maclobdell | 5:7003c9f6d633 | 22 | |
maclobdell | 2:610196c7b41c | 23 | |
maclobdell | 0:0fb17f0d39e7 | 24 | } |
maclobdell | 0:0fb17f0d39e7 | 25 | |
maclobdell | 0:0fb17f0d39e7 | 26 | // this code runs when the microcontroller starts up |
maclobdell | 1:8922cc02aec7 | 27 | int main() { |
maclobdell | 2:610196c7b41c | 28 | |
maclobdell | 0:0fb17f0d39e7 | 29 | // declare a melody |
maclobdell | 0:0fb17f0d39e7 | 30 | int melody[] = { |
maclobdell | 2:610196c7b41c | 31 | NOTE_G4, NOTE_G4, NOTE_G4, NOTE_DS3, NOTE_AS4, |
maclobdell | 2:610196c7b41c | 32 | NOTE_G4, NOTE_DS3, NOTE_AS4, NOTE_G4, |
maclobdell | 2:610196c7b41c | 33 | NOTE_D4, NOTE_D4, NOTE_D4, NOTE_DS4, NOTE_AS4, |
maclobdell | 2:610196c7b41c | 34 | NOTE_FS4, NOTE_DS4, NOTE_AS4, NOTE_G4 |
maclobdell | 0:0fb17f0d39e7 | 35 | }; |
maclobdell | 2:610196c7b41c | 36 | |
maclobdell | 0:0fb17f0d39e7 | 37 | // note durations: 4 = quarter note, 8 = eighth note, etc.: |
maclobdell | 0:0fb17f0d39e7 | 38 | int duration[] = { |
maclobdell | 2:610196c7b41c | 39 | 2, 2, 2, 4, 4, |
maclobdell | 2:610196c7b41c | 40 | 2, 4, 4, 1, |
maclobdell | 2:610196c7b41c | 41 | 2, 2, 2, 4, 4, |
maclobdell | 2:610196c7b41c | 42 | 2, 4, 4, 1 |
maclobdell | 2:610196c7b41c | 43 | }; |
maclobdell | 0:0fb17f0d39e7 | 44 | |
maclobdell | 0:0fb17f0d39e7 | 45 | // melody & duration are on the heap, need to get them on the stack |
maclobdell | 0:0fb17f0d39e7 | 46 | int *m = new int[sizeof(melody) / sizeof(int)]; |
maclobdell | 0:0fb17f0d39e7 | 47 | memcpy(m, melody, sizeof(melody)); |
maclobdell | 0:0fb17f0d39e7 | 48 | int *d = new int[sizeof(duration) / sizeof(int)]; |
maclobdell | 0:0fb17f0d39e7 | 49 | memcpy(d, duration, sizeof(duration)); |
maclobdell | 0:0fb17f0d39e7 | 50 | |
maclobdell | 0:0fb17f0d39e7 | 51 | if (sizeof(melody) != sizeof(duration)) { |
maclobdell | 0:0fb17f0d39e7 | 52 | printf("Melody and duration do not have same number of elements! Aborting!\r\n"); |
maclobdell | 2:610196c7b41c | 53 | return 1; |
maclobdell | 0:0fb17f0d39e7 | 54 | } |
maclobdell | 0:0fb17f0d39e7 | 55 | |
maclobdell | 0:0fb17f0d39e7 | 56 | play_song(sizeof(melody) / sizeof(int), m, d); |
maclobdell | 2:610196c7b41c | 57 | |
maclobdell | 2:610196c7b41c | 58 | return 0; |
maclobdell | 0:0fb17f0d39e7 | 59 | } |