change tone sounds by altering the waveforms

Dependencies:   mbed

Fork of 5_songs by MakingMusicWorkshop

Committer:
maclobdell
Date:
Tue May 10 16:49:06 2016 +0000
Revision:
1:8922cc02aec7
Parent:
0:0fb17f0d39e7
Child:
2:610196c7b41c
tested, converted to rtos timing, removed code to be added by attendees

Who changed what in which revision?

UserRevisionLine numberNew 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 1:8922cc02aec7 3 #include "rtos.h" // using the rtos for timing features. Just want to block the play_song function until it is time to play the next note.
maclobdell 0:0fb17f0d39e7 4
maclobdell 0:0fb17f0d39e7 5 PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation)
maclobdell 0:0fb17f0d39e7 6
maclobdell 0:0fb17f0d39e7 7 static int BPM = 120;
maclobdell 0:0fb17f0d39e7 8
maclobdell 0:0fb17f0d39e7 9 static void silence() {
maclobdell 0:0fb17f0d39e7 10 buzzer.write(0.0f); // silence!
maclobdell 0:0fb17f0d39e7 11 }
maclobdell 0:0fb17f0d39e7 12
maclobdell 0:0fb17f0d39e7 13 // this is our function that plays a tone.
maclobdell 0:0fb17f0d39e7 14 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
maclobdell 0:0fb17f0d39e7 15 static void play_tone(int tone) {
maclobdell 0:0fb17f0d39e7 16 buzzer.period_us(tone);
maclobdell 0:0fb17f0d39e7 17 buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud
maclobdell 0:0fb17f0d39e7 18 }
maclobdell 0:0fb17f0d39e7 19
maclobdell 0:0fb17f0d39e7 20 static void play_song(int notes_left, int* melody, int* duration) {
maclobdell 1:8922cc02aec7 21
maclobdell 0:0fb17f0d39e7 22 // YOUR CODE HERE
maclobdell 1:8922cc02aec7 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 0:0fb17f0d39e7 28 // declare a melody
maclobdell 0:0fb17f0d39e7 29 int melody[] = {
maclobdell 0:0fb17f0d39e7 30 NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4,
maclobdell 0:0fb17f0d39e7 31 NOTE_D4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4,
maclobdell 0:0fb17f0d39e7 32 NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4,
maclobdell 0:0fb17f0d39e7 33 NOTE_C4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4
maclobdell 0:0fb17f0d39e7 34 };
maclobdell 0:0fb17f0d39e7 35
maclobdell 0:0fb17f0d39e7 36 // note durations: 4 = quarter note, 8 = eighth note, etc.:
maclobdell 0:0fb17f0d39e7 37 int duration[] = {
maclobdell 0:0fb17f0d39e7 38 4, 4, 4, 4, 4, 4, 2,
maclobdell 0:0fb17f0d39e7 39 4, 4, 2, 4, 4, 2,
maclobdell 0:0fb17f0d39e7 40 4, 4, 4, 4, 4, 4, 4,
maclobdell 0:0fb17f0d39e7 41 4, 4, 4, 4, 4, 2
maclobdell 0:0fb17f0d39e7 42 };
maclobdell 0:0fb17f0d39e7 43
maclobdell 0:0fb17f0d39e7 44 // melody & duration are on the heap, need to get them on the stack
maclobdell 0:0fb17f0d39e7 45 int *m = new int[sizeof(melody) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 46 memcpy(m, melody, sizeof(melody));
maclobdell 0:0fb17f0d39e7 47 int *d = new int[sizeof(duration) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 48 memcpy(d, duration, sizeof(duration));
maclobdell 0:0fb17f0d39e7 49
maclobdell 0:0fb17f0d39e7 50 if (sizeof(melody) != sizeof(duration)) {
maclobdell 0:0fb17f0d39e7 51 printf("Melody and duration do not have same number of elements! Aborting!\r\n");
maclobdell 0:0fb17f0d39e7 52 return;
maclobdell 0:0fb17f0d39e7 53 }
maclobdell 0:0fb17f0d39e7 54
maclobdell 0:0fb17f0d39e7 55 play_song(sizeof(melody) / sizeof(int), m, d);
maclobdell 0:0fb17f0d39e7 56 }