buzzer
Dependencies: mbed
main.cpp@0:72ee6ba483f8, 2016-06-09 (annotated)
- Committer:
- xiaohonglv
- Date:
- Thu Jun 09 10:12:11 2016 +0000
- Revision:
- 0:72ee6ba483f8
- Child:
- 1:f6f3187e44c5
buzzer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xiaohonglv | 0:72ee6ba483f8 | 1 | #include "mbed.h" |
xiaohonglv | 0:72ee6ba483f8 | 2 | PwmOut buzzer(p23); |
xiaohonglv | 0:72ee6ba483f8 | 3 | |
xiaohonglv | 0:72ee6ba483f8 | 4 | // red light |
xiaohonglv | 0:72ee6ba483f8 | 5 | while (flag = 1); |
xiaohonglv | 0:72ee6ba483f8 | 6 | { |
xiaohonglv | 0:72ee6ba483f8 | 7 | |
xiaohonglv | 0:72ee6ba483f8 | 8 | int main() { |
xiaohonglv | 0:72ee6ba483f8 | 9 | |
xiaohonglv | 0:72ee6ba483f8 | 10 | |
xiaohonglv | 0:72ee6ba483f8 | 11 | |
xiaohonglv | 0:72ee6ba483f8 | 12 | speaker.period_ms(1); //1ms period 1k Hz // |
xiaohonglv | 0:72ee6ba483f8 | 13 | |
xiaohonglv | 0:72ee6ba483f8 | 14 | speaker = 0.5; //50% duty cycle // |
xiaohonglv | 0:72ee6ba483f8 | 15 | } |
xiaohonglv | 0:72ee6ba483f8 | 16 | |
xiaohonglv | 0:72ee6ba483f8 | 17 | } |
xiaohonglv | 0:72ee6ba483f8 | 18 | |
xiaohonglv | 0:72ee6ba483f8 | 19 | |
xiaohonglv | 0:72ee6ba483f8 | 20 | // yellow light |
xiaohonglv | 0:72ee6ba483f8 | 21 | |
xiaohonglv | 0:72ee6ba483f8 | 22 | while (flag = 2); |
xiaohonglv | 0:72ee6ba483f8 | 23 | |
xiaohonglv | 0:72ee6ba483f8 | 24 | { |
xiaohonglv | 0:72ee6ba483f8 | 25 | float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; //music frequency array |
xiaohonglv | 0:72ee6ba483f8 | 26 | |
xiaohonglv | 0:72ee6ba483f8 | 27 | |
xiaohonglv | 0:72ee6ba483f8 | 28 | float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array |
xiaohonglv | 0:72ee6ba483f8 | 29 | int |
xiaohonglv | 0:72ee6ba483f8 | 30 | main() |
xiaohonglv | 0:72ee6ba483f8 | 31 | { |
xiaohonglv | 0:72ee6ba483f8 | 32 | while (1) { |
xiaohonglv | 0:72ee6ba483f8 | 33 | for ( |
xiaohonglv | 0:72ee6ba483f8 | 34 | int |
xiaohonglv | 0:72ee6ba483f8 | 35 | i=0; |
xiaohonglv | 0:72ee6ba483f8 | 36 | i<=11; |
xiaohonglv | 0:72ee6ba483f8 | 37 | i++) {buzzer.period(1/(frequency[i])); // PMW period 1/frequency |
xiaohonglv | 0:72ee6ba483f8 | 38 | buzzer=0.5; //50% duty cycle |
xiaohonglv | 0:72ee6ba483f8 | 39 | wait(0.5*beat[i]); //hold for beat period |
xiaohonglv | 0:72ee6ba483f8 | 40 | } |
xiaohonglv | 0:72ee6ba483f8 | 41 | } |
xiaohonglv | 0:72ee6ba483f8 | 42 | } |
xiaohonglv | 0:72ee6ba483f8 | 43 | } |
xiaohonglv | 0:72ee6ba483f8 | 44 | |
xiaohonglv | 0:72ee6ba483f8 | 45 | |
xiaohonglv | 0:72ee6ba483f8 | 46 | |
xiaohonglv | 0:72ee6ba483f8 | 47 | |
xiaohonglv | 0:72ee6ba483f8 | 48 | //green light |
xiaohonglv | 0:72ee6ba483f8 | 49 | |
xiaohonglv | 0:72ee6ba483f8 | 50 | while (flag = 3); |
xiaohonglv | 0:72ee6ba483f8 | 51 | |
xiaohonglv | 0:72ee6ba483f8 | 52 | { |
xiaohonglv | 0:72ee6ba483f8 | 53 | |
xiaohonglv | 0:72ee6ba483f8 | 54 | int main() { |
xiaohonglv | 0:72ee6ba483f8 | 55 | |
xiaohonglv | 0:72ee6ba483f8 | 56 | speaker.period_ms(1); //1ms period 1k Hz // |
xiaohonglv | 0:72ee6ba483f8 | 57 | |
xiaohonglv | 0:72ee6ba483f8 | 58 | speaker = 0; //0% duty cycle // |
xiaohonglv | 0:72ee6ba483f8 | 59 | |
xiaohonglv | 0:72ee6ba483f8 | 60 | } |
xiaohonglv | 0:72ee6ba483f8 | 61 | |
xiaohonglv | 0:72ee6ba483f8 | 62 | |
xiaohonglv | 0:72ee6ba483f8 | 63 | |
xiaohonglv | 0:72ee6ba483f8 | 64 | |
xiaohonglv | 0:72ee6ba483f8 | 65 | |
xiaohonglv | 0:72ee6ba483f8 | 66 | |
xiaohonglv | 0:72ee6ba483f8 | 67 | |
xiaohonglv | 0:72ee6ba483f8 | 68 | |
xiaohonglv | 0:72ee6ba483f8 | 69 | |
xiaohonglv | 0:72ee6ba483f8 | 70 | |
xiaohonglv | 0:72ee6ba483f8 | 71 | |
xiaohonglv | 0:72ee6ba483f8 | 72 | |
xiaohonglv | 0:72ee6ba483f8 | 73 | |
xiaohonglv | 0:72ee6ba483f8 | 74 | |
xiaohonglv | 0:72ee6ba483f8 | 75 | |
xiaohonglv | 0:72ee6ba483f8 | 76 | |
xiaohonglv | 0:72ee6ba483f8 | 77 | |
xiaohonglv | 0:72ee6ba483f8 | 78 | |
xiaohonglv | 0:72ee6ba483f8 | 79 | |
xiaohonglv | 0:72ee6ba483f8 | 80 |