Mac Lobdell / Mbed 2 deprecated 3_sound

Dependencies:   mbed

Fork of 3_sound 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 InterruptIn btn2(SW2);               // we create a variable 'btn2', use it as an in port
00005 InterruptIn btn3(SW3);               // we create a variable 'btn3', use it as an in port
00006 
00007 PwmOut buzzer(D3);                   // our buzzer is a PWM output (pulse-width modulation)
00008 
00009 Timeout tone_timeout;  //TimeOut = a interrupt to call a function after a specified delay
00010 
00011 static void silence() {
00012     buzzer.write(0.0f); // silence!
00013 }
00014 
00015 // this is our function that plays a tone. 
00016 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
00017 static void play_tone(int tone, int duration) {
00018     buzzer.period_us(tone);
00019     buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud
00020     
00021     // we wait for duration ms. and then call the silence function
00022     tone_timeout.attach_us(&silence, duration*1000); // setup tone_timeout to call silence after duration ms
00023 }
00024 
00025 // YOUR CODE HERE
00026 //REMOVE
00027 static void play_note1() {
00028     play_tone(NOTE_C4, 200);
00029 }
00030 static void play_note2() {
00031     play_tone(NOTE_D4, 200);
00032 }
00033 //END REMOVE
00034 
00035 // this code runs when the microcontroller starts up
00036 int main() {
00037     btn2.fall(play_note1);
00038     btn3.fall(play_note2);
00039 }