Mac Lobdell / Mbed 2 deprecated 5_songs

Dependencies:   mbed

Fork of 5_songs by MakingMusicWorkshop

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"       // this tells us to load mbed  related functions
00002 #include "tones.h"                   // list of all the tones and their frequencies
00003 
00004 PwmOut buzzer(D3);                   // our buzzer is a PWM output (pulse-width modulation)
00005 
00006 static int BPM = 120;
00007 
00008 static void silence() {
00009     buzzer.write(0.0f); // silence!
00010 }
00011 
00012 // this is our function that plays a tone. 
00013 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
00014 static void play_tone(int tone) {
00015     buzzer.period_us(tone);
00016     buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud
00017 }
00018 
00019 static void play_song(int notes_left, int* melody, int* duration) {
00020     // YOUR CODE HERE
00021 }
00022 
00023 // this code runs when the microcontroller starts up
00024 void main() {
00025     // declare a melody
00026     int melody[] = {
00027         NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, 
00028         NOTE_D4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, 
00029         NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, 
00030         NOTE_C4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4
00031     };
00032 
00033     // note durations: 4 = quarter note, 8 = eighth note, etc.:
00034     int duration[] = {
00035         4, 4, 4, 4, 4, 4, 2, 
00036         4, 4, 2, 4, 4, 2,
00037         4, 4, 4, 4, 4, 4, 4,
00038         4, 4, 4, 4, 4, 2
00039     };
00040     
00041     // melody & duration are on the heap, need to get them on the stack
00042     int *m = new int[sizeof(melody) / sizeof(int)];
00043     memcpy(m, melody, sizeof(melody));
00044     int *d = new int[sizeof(duration) / sizeof(int)];
00045     memcpy(d, duration, sizeof(duration));
00046     
00047     if (sizeof(melody) != sizeof(duration)) {
00048         printf("Melody and duration do not have same number of elements! Aborting!\r\n");
00049         return;
00050     }
00051     
00052     play_song(sizeof(melody) / sizeof(int), m, d);
00053 }