by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:d7c850cdbc39, 2013-05-24 (annotated)
- Committer:
- robt
- Date:
- Fri May 24 21:49:35 2013 +0000
- Revision:
- 0:d7c850cdbc39
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
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 |
robt | 0:d7c850cdbc39 | 2 | */ |
robt | 0:d7c850cdbc39 | 3 | #include "mbed.h" |
robt | 0:d7c850cdbc39 | 4 | #include <stdio.h> |
robt | 0:d7c850cdbc39 | 5 | Serial pc(USBTX, USBRX); |
robt | 0:d7c850cdbc39 | 6 | DigitalIn up_button(p5); |
robt | 0:d7c850cdbc39 | 7 | DigitalIn down_button(p6); |
robt | 0:d7c850cdbc39 | 8 | DigitalOut redled(p19); //displays the metronome beat |
robt | 0:d7c850cdbc39 | 9 | Ticker beat_rate; //define a Ticker, with name “beat_rate” |
robt | 0:d7c850cdbc39 | 10 | void beat(void); |
robt | 0:d7c850cdbc39 | 11 | float period (0.5); //metronome period in seconds, inital value 0.5 |
robt | 0:d7c850cdbc39 | 12 | int rate (120); //metronome rate, initial value 120 |
robt | 0:d7c850cdbc39 | 13 | |
robt | 0:d7c850cdbc39 | 14 | int main() { |
robt | 0:d7c850cdbc39 | 15 | pc.printf("\r\n"); |
robt | 0:d7c850cdbc39 | 16 | pc.printf("mbed metronome!\r\n"); |
robt | 0:d7c850cdbc39 | 17 | pc.printf("_______________\r\n"); |
robt | 0:d7c850cdbc39 | 18 | period = 1; |
robt | 0:d7c850cdbc39 | 19 | redled = 1; //diagnostic |
robt | 0:d7c850cdbc39 | 20 | wait(.1); |
robt | 0:d7c850cdbc39 | 21 | redled = 0; |
robt | 0:d7c850cdbc39 | 22 | beat_rate.attach(&beat, period); //initialises the beat rate |
robt | 0:d7c850cdbc39 | 23 | //main loop checks buttons, updates rates and displays |
robt | 0:d7c850cdbc39 | 24 | while(1){ |
robt | 0:d7c850cdbc39 | 25 | if (up_button ==1) //increase rate by 4 |
robt | 0:d7c850cdbc39 | 26 | rate = rate + 4; |
robt | 0:d7c850cdbc39 | 27 | if (down_button ==1) //decrease rate by 4 |
robt | 0:d7c850cdbc39 | 28 | rate = rate - 4; |
robt | 0:d7c850cdbc39 | 29 | if (rate > 208) //limit the maximum beat rate to 208 |
robt | 0:d7c850cdbc39 | 30 | rate = 208; |
robt | 0:d7c850cdbc39 | 31 | if (rate < 40) //limit the minimum beat rate to 40 |
robt | 0:d7c850cdbc39 | 32 | rate = 40; |
robt | 0:d7c850cdbc39 | 33 | period = 60/rate; //calculate the beat period |
robt | 0:d7c850cdbc39 | 34 | pc.printf("metronome rate is %i\r", rate); |
robt | 0:d7c850cdbc39 | 35 | //pc.printf("metronome period is %f\r\n", period); //optional check |
robt | 0:d7c850cdbc39 | 36 | wait (0.5); |
robt | 0:d7c850cdbc39 | 37 | } |
robt | 0:d7c850cdbc39 | 38 | } |
robt | 0:d7c850cdbc39 | 39 | |
robt | 0:d7c850cdbc39 | 40 | void beat() { //this is the metronome beat |
robt | 0:d7c850cdbc39 | 41 | beat_rate.attach(&beat, period); //update beat rate at this moment |
robt | 0:d7c850cdbc39 | 42 | redled = 1; |
robt | 0:d7c850cdbc39 | 43 | wait(.1); |
robt | 0:d7c850cdbc39 | 44 | redled = 0; |
robt | 0:d7c850cdbc39 | 45 | } |
robt | 0:d7c850cdbc39 | 46 |