avnish aggarwal
/
Book-PE_09-10_Metronomexx
ok
Fork of Book-PE_09-10_Metronomex by
main.cpp@3:0de69a4b8aac, 2013-10-28 (annotated)
- Committer:
- avnisha
- Date:
- Mon Oct 28 08:44:02 2013 +0000
- Revision:
- 3:0de69a4b8aac
- Parent:
- 2:d1d15e8c083f
bug relating to int/float
Who changed what in which revision?
User | Revision | Line number | New 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 | */ |
robt | 0:d7c850cdbc39 | 5 | #include "mbed.h" |
robt | 0:d7c850cdbc39 | 6 | #include <stdio.h> |
robt | 0:d7c850cdbc39 | 7 | Serial pc(USBTX, USBRX); |
avnisha | 1:733a0de4d34c | 8 | DigitalIn up_button(p15); |
avnisha | 1:733a0de4d34c | 9 | DigitalIn down_button(p16); |
avnisha | 1:733a0de4d34c | 10 | DigitalOut redled(LED1); //displays the metronome beat |
avnisha | 2:d1d15e8c083f | 11 | Ticker beat_rate; |
avnisha | 2:d1d15e8c083f | 12 | PwmOut spkr(p26); |
avnisha | 2:d1d15e8c083f | 13 | |
robt | 0:d7c850cdbc39 | 14 | void beat(void); |
avnisha | 3:0de69a4b8aac | 15 | float period (1); |
avnisha | 3:0de69a4b8aac | 16 | float rate (60); |
robt | 0:d7c850cdbc39 | 17 | |
robt | 0:d7c850cdbc39 | 18 | int main() { |
robt | 0:d7c850cdbc39 | 19 | pc.printf("\r\n"); |
robt | 0:d7c850cdbc39 | 20 | pc.printf("mbed metronome!\r\n"); |
robt | 0:d7c850cdbc39 | 21 | pc.printf("_______________\r\n"); |
robt | 0:d7c850cdbc39 | 22 | period = 1; |
robt | 0:d7c850cdbc39 | 23 | redled = 1; //diagnostic |
robt | 0:d7c850cdbc39 | 24 | wait(.1); |
robt | 0:d7c850cdbc39 | 25 | redled = 0; |
robt | 0:d7c850cdbc39 | 26 | beat_rate.attach(&beat, period); //initialises the beat rate |
robt | 0:d7c850cdbc39 | 27 | //main loop checks buttons, updates rates and displays |
robt | 0:d7c850cdbc39 | 28 | while(1){ |
robt | 0:d7c850cdbc39 | 29 | if (up_button ==1) //increase rate by 4 |
robt | 0:d7c850cdbc39 | 30 | rate = rate + 4; |
robt | 0:d7c850cdbc39 | 31 | if (down_button ==1) //decrease rate by 4 |
robt | 0:d7c850cdbc39 | 32 | rate = rate - 4; |
robt | 0:d7c850cdbc39 | 33 | if (rate > 208) //limit the maximum beat rate to 208 |
robt | 0:d7c850cdbc39 | 34 | rate = 208; |
robt | 0:d7c850cdbc39 | 35 | if (rate < 40) //limit the minimum beat rate to 40 |
robt | 0:d7c850cdbc39 | 36 | rate = 40; |
robt | 0:d7c850cdbc39 | 37 | period = 60/rate; //calculate the beat period |
avnisha | 3:0de69a4b8aac | 38 | pc.printf("metronome rate is %f\r\n", rate); |
avnisha | 3:0de69a4b8aac | 39 | pc.printf("metronome period is %f\r\n", period); |
robt | 0:d7c850cdbc39 | 40 | wait (0.5); |
robt | 0:d7c850cdbc39 | 41 | } |
robt | 0:d7c850cdbc39 | 42 | } |
robt | 0:d7c850cdbc39 | 43 | |
robt | 0:d7c850cdbc39 | 44 | void beat() { //this is the metronome beat |
robt | 0:d7c850cdbc39 | 45 | beat_rate.attach(&beat, period); //update beat rate at this moment |
avnisha | 2:d1d15e8c083f | 46 | |
avnisha | 2:d1d15e8c083f | 47 | // speaker |
avnisha | 2:d1d15e8c083f | 48 | spkr.period(1.0/5000); |
avnisha | 2:d1d15e8c083f | 49 | spkr=0.5; |
avnisha | 2:d1d15e8c083f | 50 | |
avnisha | 2:d1d15e8c083f | 51 | // led |
robt | 0:d7c850cdbc39 | 52 | redled = 1; |
robt | 0:d7c850cdbc39 | 53 | wait(.1); |
avnisha | 2:d1d15e8c083f | 54 | |
avnisha | 2:d1d15e8c083f | 55 | // turn off speaker and led |
robt | 0:d7c850cdbc39 | 56 | redled = 0; |
avnisha | 2:d1d15e8c083f | 57 | spkr = 0.0; |
robt | 0:d7c850cdbc39 | 58 | } |
robt | 0:d7c850cdbc39 | 59 |