Make noise with a piezo buzzer. Use a digital out pin.

Fork of beep by Peter Drescher

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.

Committer:
joe4465
Date:
Sat Feb 22 13:11:02 2014 +0000
Revision:
5:372aa7360db7
Parent:
4:d8e14429a95f
commit before library update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:18e4a9c978ec 1 #include "beep.h"
dreschpe 0:18e4a9c978ec 2 #include "mbed.h"
dreschpe 0:18e4a9c978ec 3
dreschpe 2:a34405c20cf5 4 /** class to make sound with a buzzer, based on a PwmOut
dreschpe 3:5a8242af60ba 5 * The class use a timeout to switch off the sound - it is not blocking while making noise
dreschpe 3:5a8242af60ba 6 *
dreschpe 3:5a8242af60ba 7 * Example:
dreschpe 3:5a8242af60ba 8 * @code
dreschpe 3:5a8242af60ba 9 * // Beep with 1Khz for 0.5 seconds
dreschpe 3:5a8242af60ba 10 * #include "mbed.h"
dreschpe 3:5a8242af60ba 11 * #include "beep.h"
dreschpe 3:5a8242af60ba 12 *
dreschpe 3:5a8242af60ba 13 * Beep buzzer(p21);
dreschpe 3:5a8242af60ba 14 *
dreschpe 3:5a8242af60ba 15 * int main() {
dreschpe 3:5a8242af60ba 16 * ...
dreschpe 3:5a8242af60ba 17 * buzzer.beep(1000,0.5);
dreschpe 3:5a8242af60ba 18 * ...
dreschpe 3:5a8242af60ba 19 * }
dreschpe 3:5a8242af60ba 20 * @endcode
dreschpe 3:5a8242af60ba 21 */
dreschpe 2:a34405c20cf5 22
dreschpe 0:18e4a9c978ec 23 using namespace mbed;
dreschpe 3:5a8242af60ba 24 // constructor
dreschpe 2:a34405c20cf5 25 /** Create a Beep object connected to the specified PwmOut pin
dreschpe 3:5a8242af60ba 26 *
dreschpe 3:5a8242af60ba 27 * @param pin PwmOut pin to connect to
dreschpe 3:5a8242af60ba 28 */
dreschpe 2:a34405c20cf5 29
joe4465 5:372aa7360db7 30 Beep::Beep(PinName pin) : _digitalOut(pin) {
joe4465 5:372aa7360db7 31 _digitalOut.write(0); // after creating it have to be off
dreschpe 0:18e4a9c978ec 32 }
dreschpe 0:18e4a9c978ec 33
dreschpe 2:a34405c20cf5 34 /** stop the beep instantaneous
dreschpe 3:5a8242af60ba 35 * usually not used
dreschpe 3:5a8242af60ba 36 */
dreschpe 0:18e4a9c978ec 37 void Beep::nobeep() {
joe4465 5:372aa7360db7 38 _digitalOut.write(0);
dreschpe 0:18e4a9c978ec 39 }
dreschpe 0:18e4a9c978ec 40
dreschpe 2:a34405c20cf5 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 */
dreschpe 2:a34405c20cf5 46
joe4465 5:372aa7360db7 47 void Beep::beep(float time) {
dreschpe 0:18e4a9c978ec 48
joe4465 5:372aa7360db7 49 _digitalOut.write(1);
dreschpe 0:18e4a9c978ec 50 toff.attach(this,&Beep::nobeep, time); // time to off
dreschpe 0:18e4a9c978ec 51 }
dreschpe 0:18e4a9c978ec 52
dreschpe 0:18e4a9c978ec 53
dreschpe 0:18e4a9c978ec 54
dreschpe 0:18e4a9c978ec 55