for testing

Dependencies:   mbed

Fork of 3_sound by MakingMusicWorkshop

Committer:
maclobski
Date:
Tue May 10 03:32:09 2016 +0000
Revision:
1:e3b759c26b28
Parent:
0:586507ee54a5
for testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:586507ee54a5 1 #include "mbed.h" // this tells us to load mbed related functions
maclobdell 0:586507ee54a5 2 #include "tones.h" // list of all the tones and their frequencies
maclobdell 0:586507ee54a5 3
maclobdell 0:586507ee54a5 4 InterruptIn btn2(SW2); // we create a variable 'btn2', use it as an in port
maclobdell 0:586507ee54a5 5 InterruptIn btn3(SW3); // we create a variable 'btn3', use it as an in port
maclobdell 0:586507ee54a5 6
maclobdell 0:586507ee54a5 7 PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation)
maclobdell 0:586507ee54a5 8
maclobdell 0:586507ee54a5 9 Timeout tone_timeout; //TimeOut = a interrupt to call a function after a specified delay
maclobdell 0:586507ee54a5 10
maclobdell 0:586507ee54a5 11 static void silence() {
maclobdell 0:586507ee54a5 12 buzzer.write(0.0f); // silence!
maclobdell 0:586507ee54a5 13 }
maclobdell 0:586507ee54a5 14
maclobdell 0:586507ee54a5 15 // this is our function that plays a tone.
maclobdell 0:586507ee54a5 16 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
maclobdell 0:586507ee54a5 17 static void play_tone(int tone, int duration) {
maclobdell 0:586507ee54a5 18 buzzer.period_us(tone);
maclobdell 0:586507ee54a5 19 buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud
maclobdell 0:586507ee54a5 20
maclobdell 0:586507ee54a5 21 // we wait for duration ms. and then call the silence function
maclobdell 0:586507ee54a5 22 tone_timeout.attach_us(&silence, duration*1000); // setup tone_timeout to call silence after duration ms
maclobdell 0:586507ee54a5 23 }
maclobdell 0:586507ee54a5 24
maclobdell 0:586507ee54a5 25 // YOUR CODE HERE
maclobski 1:e3b759c26b28 26 //REMOVE
maclobski 1:e3b759c26b28 27 static void play_note1() {
maclobski 1:e3b759c26b28 28 play_tone(NOTE_C4, 200);
maclobski 1:e3b759c26b28 29 }
maclobski 1:e3b759c26b28 30 static void play_note2() {
maclobski 1:e3b759c26b28 31 play_tone(NOTE_D4, 200);
maclobski 1:e3b759c26b28 32 }
maclobski 1:e3b759c26b28 33 //END REMOVE
maclobdell 0:586507ee54a5 34
maclobdell 0:586507ee54a5 35 // this code runs when the microcontroller starts up
maclobdell 0:586507ee54a5 36 int main() {
maclobdell 0:586507ee54a5 37 btn2.fall(play_note1);
maclobdell 0:586507ee54a5 38 btn3.fall(play_note2);
maclobdell 0:586507ee54a5 39 }