sounds
Dependencies: mbed
main.cpp@0:6418a1bc89fe, 2014-10-09 (annotated)
- Committer:
- christianjaunich
- Date:
- Thu Oct 09 14:49:11 2014 +0000
- Revision:
- 0:6418a1bc89fe
Sounds with switches
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
christianjaunich | 0:6418a1bc89fe | 1 | #include "mbed.h" |
christianjaunich | 0:6418a1bc89fe | 2 | // speaker sound effect demo using PWM hardware output |
christianjaunich | 0:6418a1bc89fe | 3 | PwmOut speaker(p26); |
christianjaunich | 0:6418a1bc89fe | 4 | DigitalIn switch2(p16); //Switch 2 |
christianjaunich | 0:6418a1bc89fe | 5 | DigitalIn switch3(p17); //Switch 3 |
christianjaunich | 0:6418a1bc89fe | 6 | int main() |
christianjaunich | 0:6418a1bc89fe | 7 | { |
christianjaunich | 0:6418a1bc89fe | 8 | float i=0.0; |
christianjaunich | 0:6418a1bc89fe | 9 | while(1) |
christianjaunich | 0:6418a1bc89fe | 10 | { |
christianjaunich | 0:6418a1bc89fe | 11 | if(switch2==0&&switch3==0) |
christianjaunich | 0:6418a1bc89fe | 12 | { |
christianjaunich | 0:6418a1bc89fe | 13 | speaker=0.0; |
christianjaunich | 0:6418a1bc89fe | 14 | } |
christianjaunich | 0:6418a1bc89fe | 15 | else if((switch2==1)&&(switch3==0)) |
christianjaunich | 0:6418a1bc89fe | 16 | { |
christianjaunich | 0:6418a1bc89fe | 17 | // generate a 500Hz tone using PWM hardware output |
christianjaunich | 0:6418a1bc89fe | 18 | speaker.period(1.0/500.0); // 500hz period |
christianjaunich | 0:6418a1bc89fe | 19 | speaker =0.5; //50% duty cycle - max volume |
christianjaunich | 0:6418a1bc89fe | 20 | wait(.5); |
christianjaunich | 0:6418a1bc89fe | 21 | } |
christianjaunich | 0:6418a1bc89fe | 22 | else if((switch3==1)&&(switch2==0)) |
christianjaunich | 0:6418a1bc89fe | 23 | { |
christianjaunich | 0:6418a1bc89fe | 24 | // two tone police siren effect - two periods or two frequencies |
christianjaunich | 0:6418a1bc89fe | 25 | speaker.period(1.0/969.0); |
christianjaunich | 0:6418a1bc89fe | 26 | speaker =0.25; |
christianjaunich | 0:6418a1bc89fe | 27 | wait(.5); |
christianjaunich | 0:6418a1bc89fe | 28 | speaker.period(1.0/500.0); |
christianjaunich | 0:6418a1bc89fe | 29 | wait(.5); |
christianjaunich | 0:6418a1bc89fe | 30 | |
christianjaunich | 0:6418a1bc89fe | 31 | } |
christianjaunich | 0:6418a1bc89fe | 32 | else if((switch2==1)&&(switch3==1)) |
christianjaunich | 0:6418a1bc89fe | 33 | { |
christianjaunich | 0:6418a1bc89fe | 34 | while(i<6000.0) |
christianjaunich | 0:6418a1bc89fe | 35 | { |
christianjaunich | 0:6418a1bc89fe | 36 | speaker.period(1.0/float(i)); |
christianjaunich | 0:6418a1bc89fe | 37 | speaker=0.25; |
christianjaunich | 0:6418a1bc89fe | 38 | wait(.1); |
christianjaunich | 0:6418a1bc89fe | 39 | i+=100; |
christianjaunich | 0:6418a1bc89fe | 40 | if(i==6000.0) |
christianjaunich | 0:6418a1bc89fe | 41 | { |
christianjaunich | 0:6418a1bc89fe | 42 | i=0.0; |
christianjaunich | 0:6418a1bc89fe | 43 | } |
christianjaunich | 0:6418a1bc89fe | 44 | if(switch2==0&&switch3==0) |
christianjaunich | 0:6418a1bc89fe | 45 | { |
christianjaunich | 0:6418a1bc89fe | 46 | break; |
christianjaunich | 0:6418a1bc89fe | 47 | } |
christianjaunich | 0:6418a1bc89fe | 48 | } |
christianjaunich | 0:6418a1bc89fe | 49 | } |
christianjaunich | 0:6418a1bc89fe | 50 | } |
christianjaunich | 0:6418a1bc89fe | 51 | } |