Make noise with a piezo buzzer. Use a digital out pin.
Fork of beep by
This is a simple non blocking library to turn on the pin passed into the constructor for the amount of time specified when calling the beep(float time) function.
I use it to turn on a buzzer but it could be used for anything you need to turn on for a set time.
beep.h@5:372aa7360db7, 2014-02-22 (annotated)
- Committer:
- joe4465
- Date:
- Sat Feb 22 13:11:02 2014 +0000
- Revision:
- 5:372aa7360db7
- Parent:
- 3:5a8242af60ba
commit before library update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:18e4a9c978ec | 1 | #ifndef MBED_BEEP_H |
dreschpe | 0:18e4a9c978ec | 2 | #define MBED_BEEP_H |
dreschpe | 0:18e4a9c978ec | 3 | |
dreschpe | 0:18e4a9c978ec | 4 | #include "mbed.h" |
dreschpe | 0:18e4a9c978ec | 5 | |
dreschpe | 1:ddccf0a4a414 | 6 | /** class to make sound with a buzzer, based on a PwmOut |
dreschpe | 3:5a8242af60ba | 7 | * The class use a timeout to switch off the sound - it is not blocking while making noise |
dreschpe | 3:5a8242af60ba | 8 | * |
dreschpe | 3:5a8242af60ba | 9 | * Example: |
dreschpe | 3:5a8242af60ba | 10 | * @code |
dreschpe | 3:5a8242af60ba | 11 | * // Beep with 1Khz for 0.5 seconds |
dreschpe | 3:5a8242af60ba | 12 | * #include "mbed.h" |
dreschpe | 3:5a8242af60ba | 13 | * #include "beep.h" |
dreschpe | 3:5a8242af60ba | 14 | * |
dreschpe | 3:5a8242af60ba | 15 | * Beep buzzer(p21); |
dreschpe | 3:5a8242af60ba | 16 | * |
dreschpe | 3:5a8242af60ba | 17 | * int main() { |
dreschpe | 3:5a8242af60ba | 18 | * ... |
dreschpe | 3:5a8242af60ba | 19 | * buzzer.beep(1000,0.5); |
dreschpe | 3:5a8242af60ba | 20 | * ... |
dreschpe | 3:5a8242af60ba | 21 | * } |
dreschpe | 3:5a8242af60ba | 22 | * @endcode |
dreschpe | 3:5a8242af60ba | 23 | */ |
dreschpe | 1:ddccf0a4a414 | 24 | |
dreschpe | 1:ddccf0a4a414 | 25 | |
dreschpe | 0:18e4a9c978ec | 26 | namespace mbed { |
dreschpe | 0:18e4a9c978ec | 27 | |
dreschpe | 0:18e4a9c978ec | 28 | /* Class: Beep |
dreschpe | 0:18e4a9c978ec | 29 | * A class witch uses pwm to controle a beeper to generate sounds. |
dreschpe | 0:18e4a9c978ec | 30 | */ |
dreschpe | 0:18e4a9c978ec | 31 | class Beep { |
dreschpe | 0:18e4a9c978ec | 32 | |
dreschpe | 0:18e4a9c978ec | 33 | public: |
dreschpe | 0:18e4a9c978ec | 34 | |
dreschpe | 3:5a8242af60ba | 35 | /** Create a Beep object connected to the specified PwmOut pin |
dreschpe | 3:5a8242af60ba | 36 | * |
dreschpe | 3:5a8242af60ba | 37 | * @param pin PwmOut pin to connect to |
dreschpe | 3:5a8242af60ba | 38 | */ |
dreschpe | 0:18e4a9c978ec | 39 | Beep (PinName pin); |
dreschpe | 0:18e4a9c978ec | 40 | |
dreschpe | 3:5a8242af60ba | 41 | /** Beep with given frequency and duration. |
dreschpe | 3:5a8242af60ba | 42 | * |
dreschpe | 3:5a8242af60ba | 43 | * @param frequency - the frequency of the tone in Hz |
dreschpe | 3:5a8242af60ba | 44 | * @param time - the duration of the tone in seconds |
dreschpe | 3:5a8242af60ba | 45 | */ |
joe4465 | 5:372aa7360db7 | 46 | void beep (float time); |
dreschpe | 0:18e4a9c978ec | 47 | |
dreschpe | 3:5a8242af60ba | 48 | /** stop the beep instantaneous |
dreschpe | 3:5a8242af60ba | 49 | * usually not used |
dreschpe | 3:5a8242af60ba | 50 | */ |
dreschpe | 0:18e4a9c978ec | 51 | void nobeep(); |
dreschpe | 0:18e4a9c978ec | 52 | |
dreschpe | 0:18e4a9c978ec | 53 | private : |
joe4465 | 5:372aa7360db7 | 54 | DigitalOut _digitalOut; |
dreschpe | 0:18e4a9c978ec | 55 | Timeout toff; |
dreschpe | 0:18e4a9c978ec | 56 | }; |
dreschpe | 0:18e4a9c978ec | 57 | |
dreschpe | 0:18e4a9c978ec | 58 | } |
dreschpe | 0:18e4a9c978ec | 59 | #endif |