Victor Szultka
/
2_4_Speaker
Simulated fire alarm response, with increasing tempo and lowering tone on each repetition
main.cpp@0:0651c91d96ca, 2020-07-31 (annotated)
- Committer:
- saltire78
- Date:
- Fri Jul 31 13:05:58 2020 +0000
- Revision:
- 0:0651c91d96ca
online posting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saltire78 | 0:0651c91d96ca | 1 | #include "mbed.h" |
saltire78 | 0:0651c91d96ca | 2 | |
saltire78 | 0:0651c91d96ca | 3 | DigitalIn fire(p14); |
saltire78 | 0:0651c91d96ca | 4 | PwmOut spkr(p26); |
saltire78 | 0:0651c91d96ca | 5 | AnalogIn pot1(p19); |
saltire78 | 0:0651c91d96ca | 6 | |
saltire78 | 0:0651c91d96ca | 7 | float freqmin = 2000.0; // initialising the low frequency range |
saltire78 | 0:0651c91d96ca | 8 | float freqmax = 10000.0; // initialising the high frequency range |
saltire78 | 0:0651c91d96ca | 9 | float freqchange = 100; // initialising the frequency change per tone |
saltire78 | 0:0651c91d96ca | 10 | float freqperiod = 1.0; // initialising the output tone |
saltire78 | 0:0651c91d96ca | 11 | float speakervol = 0.5; // initialising the PWM output |
saltire78 | 0:0651c91d96ca | 12 | float tonelength = 0.3; // initialising the time each tone will hold |
saltire78 | 0:0651c91d96ca | 13 | |
saltire78 | 0:0651c91d96ca | 14 | int main() |
saltire78 | 0:0651c91d96ca | 15 | { |
saltire78 | 0:0651c91d96ca | 16 | while (1) { |
saltire78 | 0:0651c91d96ca | 17 | for (float i=freqmin; i<freqmax; i+=freqchange) { |
saltire78 | 0:0651c91d96ca | 18 | spkr.period(freqperiod/i); // PWM output frequency |
saltire78 | 0:0651c91d96ca | 19 | spkr=speakervol; // output volume |
saltire78 | 0:0651c91d96ca | 20 | wait(tonelength); // length of each tone |
saltire78 | 0:0651c91d96ca | 21 | } |
saltire78 | 0:0651c91d96ca | 22 | spkr=0.0; |
saltire78 | 0:0651c91d96ca | 23 | freqperiod += 1.0; // tone sound will deepen with each repetition |
saltire78 | 0:0651c91d96ca | 24 | tonelength -= 0.05; // tone sound will shorten with each repetition |
saltire78 | 0:0651c91d96ca | 25 | |
saltire78 | 0:0651c91d96ca | 26 | while(pot1.read() < 0.5) {} // this uses the pot to control the program |
saltire78 | 0:0651c91d96ca | 27 | } |
saltire78 | 0:0651c91d96ca | 28 | } |