buzzer

Dependencies:   mbed

Committer:
xiaohonglv
Date:
Thu Jun 09 14:15:51 2016 +0000
Revision:
1:f6f3187e44c5
Parent:
0:72ee6ba483f8
buzzer version 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xiaohonglv 0:72ee6ba483f8 1 #include "mbed.h"
xiaohonglv 0:72ee6ba483f8 2 PwmOut buzzer(p23);
xiaohonglv 0:72ee6ba483f8 3
xiaohonglv 1:f6f3187e44c5 4
xiaohonglv 0:72ee6ba483f8 5 // red light
xiaohonglv 1:f6f3187e44c5 6 float frequency[]= {500,0,500,0,500,0,500,0,500,0} ;//alarm frequency array
xiaohonglv 0:72ee6ba483f8 7
xiaohonglv 0:72ee6ba483f8 8
xiaohonglv 1:f6f3187e44c5 9 float beat[]= {1,1,1,1,1,1,1,1,1,1}; //beat array
xiaohonglv 1:f6f3187e44c5 10 int
xiaohonglv 1:f6f3187e44c5 11 main()
xiaohonglv 1:f6f3187e44c5 12 {
xiaohonglv 1:f6f3187e44c5 13 while (1) {
xiaohonglv 1:f6f3187e44c5 14 for (
xiaohonglv 1:f6f3187e44c5 15 int
xiaohonglv 1:f6f3187e44c5 16 i=0;
xiaohonglv 1:f6f3187e44c5 17 i<=9 ;
xiaohonglv 1:f6f3187e44c5 18 i++) {buzzer.period(1/(frequency[i])); // PMW period 1/frequency
xiaohonglv 1:f6f3187e44c5 19 buzzer=0.5; //50% duty cycle
xiaohonglv 1:f6f3187e44c5 20 wait(0.5*beat[i]); //hold for beat period
xiaohonglv 1:f6f3187e44c5 21 }
xiaohonglv 1:f6f3187e44c5 22 }
xiaohonglv 1:f6f3187e44c5 23 }
xiaohonglv 0:72ee6ba483f8 24
xiaohonglv 0:72ee6ba483f8 25
xiaohonglv 0:72ee6ba483f8 26
xiaohonglv 0:72ee6ba483f8 27 // yellow light
xiaohonglv 0:72ee6ba483f8 28
xiaohonglv 0:72ee6ba483f8 29 while (flag = 2);
xiaohonglv 0:72ee6ba483f8 30
xiaohonglv 0:72ee6ba483f8 31 {
xiaohonglv 0:72ee6ba483f8 32 float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; //music frequency array
xiaohonglv 0:72ee6ba483f8 33
xiaohonglv 0:72ee6ba483f8 34
xiaohonglv 0:72ee6ba483f8 35 float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
xiaohonglv 0:72ee6ba483f8 36 int
xiaohonglv 0:72ee6ba483f8 37 main()
xiaohonglv 0:72ee6ba483f8 38 {
xiaohonglv 0:72ee6ba483f8 39 while (1) {
xiaohonglv 0:72ee6ba483f8 40 for (
xiaohonglv 0:72ee6ba483f8 41 int
xiaohonglv 0:72ee6ba483f8 42 i=0;
xiaohonglv 0:72ee6ba483f8 43 i<=11;
xiaohonglv 0:72ee6ba483f8 44 i++) {buzzer.period(1/(frequency[i])); // PMW period 1/frequency
xiaohonglv 0:72ee6ba483f8 45 buzzer=0.5; //50% duty cycle
xiaohonglv 0:72ee6ba483f8 46 wait(0.5*beat[i]); //hold for beat period
xiaohonglv 0:72ee6ba483f8 47 }
xiaohonglv 0:72ee6ba483f8 48 }
xiaohonglv 0:72ee6ba483f8 49 }
xiaohonglv 0:72ee6ba483f8 50 }
xiaohonglv 0:72ee6ba483f8 51
xiaohonglv 0:72ee6ba483f8 52
xiaohonglv 0:72ee6ba483f8 53
xiaohonglv 0:72ee6ba483f8 54
xiaohonglv 0:72ee6ba483f8 55 //green light
xiaohonglv 0:72ee6ba483f8 56
xiaohonglv 0:72ee6ba483f8 57 while (flag = 3);
xiaohonglv 0:72ee6ba483f8 58
xiaohonglv 0:72ee6ba483f8 59 {
xiaohonglv 0:72ee6ba483f8 60
xiaohonglv 0:72ee6ba483f8 61 int main() {
xiaohonglv 0:72ee6ba483f8 62
xiaohonglv 1:f6f3187e44c5 63 buzzer.period_ms(1); //1ms period 1k Hz //
xiaohonglv 0:72ee6ba483f8 64
xiaohonglv 1:f6f3187e44c5 65 buzzer = 0; //0% duty cycle //
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