ok

Dependencies:   mbed

Fork of Book-PE_09-10_Metronomex by avnish aggarwal

Committer:
avnisha
Date:
Wed Oct 16 06:21:06 2013 +0000
Revision:
2:d1d15e8c083f
Parent:
1:733a0de4d34c
Child:
3:0de69a4b8aac
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robt 0:d7c850cdbc39 1 /*Program Example 9.10: Metronome. Uses Ticker to set beat rate
avnisha 1:733a0de4d34c 2
avnisha 1:733a0de4d34c 3
avnisha 1:733a0de4d34c 4 //BusIn joy(p15,p12,p13,p16);
avnisha 1:733a0de4d34c 5 */
robt 0:d7c850cdbc39 6 #include "mbed.h"
robt 0:d7c850cdbc39 7 #include <stdio.h>
robt 0:d7c850cdbc39 8 Serial pc(USBTX, USBRX);
avnisha 1:733a0de4d34c 9 DigitalIn up_button(p15);
avnisha 1:733a0de4d34c 10 DigitalIn down_button(p16);
avnisha 1:733a0de4d34c 11 DigitalOut redled(LED1); //displays the metronome beat
avnisha 2:d1d15e8c083f 12 Ticker beat_rate;
avnisha 2:d1d15e8c083f 13 PwmOut spkr(p26);
avnisha 2:d1d15e8c083f 14
robt 0:d7c850cdbc39 15 void beat(void);
avnisha 1:733a0de4d34c 16 float period (1); //metronome period in seconds, inital value 0.5
avnisha 1:733a0de4d34c 17 int rate (60); //metronome rate, initial value 120
robt 0:d7c850cdbc39 18
robt 0:d7c850cdbc39 19 int main() {
robt 0:d7c850cdbc39 20 pc.printf("\r\n");
robt 0:d7c850cdbc39 21 pc.printf("mbed metronome!\r\n");
robt 0:d7c850cdbc39 22 pc.printf("_______________\r\n");
robt 0:d7c850cdbc39 23 period = 1;
robt 0:d7c850cdbc39 24 redled = 1; //diagnostic
robt 0:d7c850cdbc39 25 wait(.1);
robt 0:d7c850cdbc39 26 redled = 0;
robt 0:d7c850cdbc39 27 beat_rate.attach(&beat, period); //initialises the beat rate
robt 0:d7c850cdbc39 28 //main loop checks buttons, updates rates and displays
robt 0:d7c850cdbc39 29 while(1){
robt 0:d7c850cdbc39 30 if (up_button ==1) //increase rate by 4
robt 0:d7c850cdbc39 31 rate = rate + 4;
robt 0:d7c850cdbc39 32 if (down_button ==1) //decrease rate by 4
robt 0:d7c850cdbc39 33 rate = rate - 4;
robt 0:d7c850cdbc39 34 if (rate > 208) //limit the maximum beat rate to 208
robt 0:d7c850cdbc39 35 rate = 208;
robt 0:d7c850cdbc39 36 if (rate < 40) //limit the minimum beat rate to 40
robt 0:d7c850cdbc39 37 rate = 40;
robt 0:d7c850cdbc39 38 period = 60/rate; //calculate the beat period
robt 0:d7c850cdbc39 39 pc.printf("metronome rate is %i\r", rate);
robt 0:d7c850cdbc39 40 //pc.printf("metronome period is %f\r\n", period); //optional check
robt 0:d7c850cdbc39 41 wait (0.5);
robt 0:d7c850cdbc39 42 }
robt 0:d7c850cdbc39 43 }
robt 0:d7c850cdbc39 44
robt 0:d7c850cdbc39 45 void beat() { //this is the metronome beat
robt 0:d7c850cdbc39 46 beat_rate.attach(&beat, period); //update beat rate at this moment
avnisha 2:d1d15e8c083f 47
avnisha 2:d1d15e8c083f 48 // speaker
avnisha 2:d1d15e8c083f 49 spkr.period(1.0/5000);
avnisha 2:d1d15e8c083f 50 spkr=0.5;
avnisha 2:d1d15e8c083f 51
avnisha 2:d1d15e8c083f 52 // led
robt 0:d7c850cdbc39 53 redled = 1;
robt 0:d7c850cdbc39 54 wait(.1);
avnisha 2:d1d15e8c083f 55
avnisha 2:d1d15e8c083f 56 // turn off speaker and led
robt 0:d7c850cdbc39 57 redled = 0;
avnisha 2:d1d15e8c083f 58 spkr = 0.0;
robt 0:d7c850cdbc39 59 }
robt 0:d7c850cdbc39 60
avnisha 2:d1d15e8c083f 61 #ifdef spkr
avnisha 2:d1d15e8c083f 62 DigitalIn fire(p14);
avnisha 2:d1d15e8c083f 63 PwmOut spkr(p26);
avnisha 2:d1d15e8c083f 64
avnisha 2:d1d15e8c083f 65 int main()
avnisha 2:d1d15e8c083f 66 {
avnisha 2:d1d15e8c083f 67 while (1) {
avnisha 2:d1d15e8c083f 68 for (float i=2000.0; i<10000.0; i+=100) {
avnisha 2:d1d15e8c083f 69 spkr.period(1.0/i);
avnisha 2:d1d15e8c083f 70 spkr=0.5;
avnisha 2:d1d15e8c083f 71 wait(0.1);
avnisha 2:d1d15e8c083f 72 }
avnisha 2:d1d15e8c083f 73 spkr=0.0;
avnisha 2:d1d15e8c083f 74 while(!fire) {}
avnisha 2:d1d15e8c083f 75 }
avnisha 2:d1d15e8c083f 76 }
avnisha 2:d1d15e8c083f 77 #endif