avnish aggarwal
/
Book-PE_09-10_Metronomex
ok
Fork of Book-PE_09-10_Metronome by
main.cpp@1:733a0de4d34c, 2013-08-01 (annotated)
- Committer:
- avnisha
- Date:
- Thu Aug 01 23:24:00 2013 +0000
- Revision:
- 1:733a0de4d34c
- Parent:
- 0:d7c850cdbc39
based on toulson
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 | //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 |
robt | 0:d7c850cdbc39 | 12 | Ticker beat_rate; //define a Ticker, with name “beat_rate” |
robt | 0:d7c850cdbc39 | 13 | void beat(void); |
avnisha | 1:733a0de4d34c | 14 | float period (1); //metronome period in seconds, inital value 0.5 |
avnisha | 1:733a0de4d34c | 15 | int rate (60); //metronome rate, initial value 120 |
robt | 0:d7c850cdbc39 | 16 | |
robt | 0:d7c850cdbc39 | 17 | int main() { |
robt | 0:d7c850cdbc39 | 18 | pc.printf("\r\n"); |
robt | 0:d7c850cdbc39 | 19 | pc.printf("mbed metronome!\r\n"); |
robt | 0:d7c850cdbc39 | 20 | pc.printf("_______________\r\n"); |
robt | 0:d7c850cdbc39 | 21 | period = 1; |
robt | 0:d7c850cdbc39 | 22 | redled = 1; //diagnostic |
robt | 0:d7c850cdbc39 | 23 | wait(.1); |
robt | 0:d7c850cdbc39 | 24 | redled = 0; |
robt | 0:d7c850cdbc39 | 25 | beat_rate.attach(&beat, period); //initialises the beat rate |
robt | 0:d7c850cdbc39 | 26 | //main loop checks buttons, updates rates and displays |
robt | 0:d7c850cdbc39 | 27 | while(1){ |
robt | 0:d7c850cdbc39 | 28 | if (up_button ==1) //increase rate by 4 |
robt | 0:d7c850cdbc39 | 29 | rate = rate + 4; |
robt | 0:d7c850cdbc39 | 30 | if (down_button ==1) //decrease rate by 4 |
robt | 0:d7c850cdbc39 | 31 | rate = rate - 4; |
robt | 0:d7c850cdbc39 | 32 | if (rate > 208) //limit the maximum beat rate to 208 |
robt | 0:d7c850cdbc39 | 33 | rate = 208; |
robt | 0:d7c850cdbc39 | 34 | if (rate < 40) //limit the minimum beat rate to 40 |
robt | 0:d7c850cdbc39 | 35 | rate = 40; |
robt | 0:d7c850cdbc39 | 36 | period = 60/rate; //calculate the beat period |
robt | 0:d7c850cdbc39 | 37 | pc.printf("metronome rate is %i\r", rate); |
robt | 0:d7c850cdbc39 | 38 | //pc.printf("metronome period is %f\r\n", period); //optional check |
robt | 0:d7c850cdbc39 | 39 | wait (0.5); |
robt | 0:d7c850cdbc39 | 40 | } |
robt | 0:d7c850cdbc39 | 41 | } |
robt | 0:d7c850cdbc39 | 42 | |
robt | 0:d7c850cdbc39 | 43 | void beat() { //this is the metronome beat |
robt | 0:d7c850cdbc39 | 44 | beat_rate.attach(&beat, period); //update beat rate at this moment |
robt | 0:d7c850cdbc39 | 45 | redled = 1; |
robt | 0:d7c850cdbc39 | 46 | wait(.1); |
robt | 0:d7c850cdbc39 | 47 | redled = 0; |
robt | 0:d7c850cdbc39 | 48 | } |
robt | 0:d7c850cdbc39 | 49 |