Metronomo
Dependencies: mbed
main.cpp@0:c9c3c570d34c, 2016-11-06 (annotated)
- Committer:
- Mattinico
- Date:
- Sun Nov 06 12:43:33 2016 +0000
- Revision:
- 0:c9c3c570d34c
k
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mattinico | 0:c9c3c570d34c | 1 | /*Program Example 9: Metronome. Uses Ticker to set beat rate |
Mattinico | 0:c9c3c570d34c | 2 | */ |
Mattinico | 0:c9c3c570d34c | 3 | #include "mbed.h" |
Mattinico | 0:c9c3c570d34c | 4 | #include <stdio.h> |
Mattinico | 0:c9c3c570d34c | 5 | Serial pc(USBTX, USBRX); |
Mattinico | 0:c9c3c570d34c | 6 | DigitalIn button(PC_13); |
Mattinico | 0:c9c3c570d34c | 7 | DigitalIn dir(D4,PullDown); |
Mattinico | 0:c9c3c570d34c | 8 | DigitalOut redled(D8); //displays the metronome beat |
Mattinico | 0:c9c3c570d34c | 9 | Ticker beat_rate; //define a Ticker, with name “beat_rate” |
Mattinico | 0:c9c3c570d34c | 10 | void beat(void); |
Mattinico | 0:c9c3c570d34c | 11 | float period (0.5); //metronome period in seconds, initial value 0.5 |
Mattinico | 0:c9c3c570d34c | 12 | int rate (120); //metronome rate, initial value 120 |
Mattinico | 0:c9c3c570d34c | 13 | int main() |
Mattinico | 0:c9c3c570d34c | 14 | { |
Mattinico | 0:c9c3c570d34c | 15 | pc.printf("\r\n"); |
Mattinico | 0:c9c3c570d34c | 16 | pc.printf("mbed metronome!\r\n"); |
Mattinico | 0:c9c3c570d34c | 17 | pc.printf("_______________\r\n"); |
Mattinico | 0:c9c3c570d34c | 18 | period = 1; |
Mattinico | 0:c9c3c570d34c | 19 | redled = 1; //diagnostic |
Mattinico | 0:c9c3c570d34c | 20 | wait(.1); |
Mattinico | 0:c9c3c570d34c | 21 | redled = 0; |
Mattinico | 0:c9c3c570d34c | 22 | beat_rate.attach(&beat, period); //initializes the beat rate |
Mattinico | 0:c9c3c570d34c | 23 | //main loop checks buttons, updates rates and displays |
Mattinico | 0:c9c3c570d34c | 24 | while(1) { |
Mattinico | 0:c9c3c570d34c | 25 | if (button ==0) { |
Mattinico | 0:c9c3c570d34c | 26 | if (dir) |
Mattinico | 0:c9c3c570d34c | 27 | //increase rate by 4 |
Mattinico | 0:c9c3c570d34c | 28 | rate = rate + 4; |
Mattinico | 0:c9c3c570d34c | 29 | else |
Mattinico | 0:c9c3c570d34c | 30 | //decrease rate by 4 |
Mattinico | 0:c9c3c570d34c | 31 | rate = rate - 4; |
Mattinico | 0:c9c3c570d34c | 32 | } |
Mattinico | 0:c9c3c570d34c | 33 | if (rate > 208) //limit the maximum beat rate to 208 |
Mattinico | 0:c9c3c570d34c | 34 | rate = 208; |
Mattinico | 0:c9c3c570d34c | 35 | if (rate < 40) //limit the minimum beat rate to 40 |
Mattinico | 0:c9c3c570d34c | 36 | rate = 40; |
Mattinico | 0:c9c3c570d34c | 37 | period = 60/rate; //calculate the beat period |
Mattinico | 0:c9c3c570d34c | 38 | pc.printf("metronome rate is %i\r", rate); |
Mattinico | 0:c9c3c570d34c | 39 | //pc.printf("metronome period is %f\r\n", period); //optional check |
Mattinico | 0:c9c3c570d34c | 40 | wait (0.5); |
Mattinico | 0:c9c3c570d34c | 41 | } |
Mattinico | 0:c9c3c570d34c | 42 | } |
Mattinico | 0:c9c3c570d34c | 43 | void beat() //this is the metronome beat |
Mattinico | 0:c9c3c570d34c | 44 | { |
Mattinico | 0:c9c3c570d34c | 45 | beat_rate.attach(&beat, period); //update beat rate at this moment |
Mattinico | 0:c9c3c570d34c | 46 | redled = 1; |
Mattinico | 0:c9c3c570d34c | 47 | wait(.1); |
Mattinico | 0:c9c3c570d34c | 48 | redled = 0; |
Mattinico | 0:c9c3c570d34c | 49 | } |