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.
Revision 0:18e4a9c978ec, committed 2011-02-01
- Comitter:
- dreschpe
- Date:
- Tue Feb 01 18:44:45 2011 +0000
- Child:
- 1:ddccf0a4a414
- Commit message:
Changed in this revision
beep.c | Show annotated file Show diff for this revision Revisions of this file |
beep.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/beep.c Tue Feb 01 18:44:45 2011 +0000 @@ -0,0 +1,26 @@ +#include "beep.h" +#include "mbed.h" + +using namespace mbed; +// constructor +Beep::Beep(PinName pin) : _pwm(pin) { + _pwm.write(0.0); // after creating it have to be off +} + +// switch off +void Beep::nobeep() { + _pwm.write(0.0); +} + + +// beep +void Beep::beep(float freq, float time) { + + _pwm.period(1.0/freq); + _pwm.write(0.5); // 50% duty cycle - beep on + toff.attach(this,&Beep::nobeep, time); // time to off +} + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/beep.h Tue Feb 01 18:44:45 2011 +0000 @@ -0,0 +1,40 @@ +#ifndef MBED_BEEP_H +#define MBED_BEEP_H + +#include "mbed.h" + +namespace mbed { + +/* Class: Beep + * A class witch uses pwm to controle a beeper to generate sounds. + */ +class Beep { + +public: + + /* Constructor: Beep + * Creates a new beeper object. + * + * Variables: + * pin - The pin which is connected to the beeper. + */ + Beep (PinName pin); + + /* Function: beep + * Beep with given frequency and duration. + * + * Variables: + * frequency - The frequency to use. + * time - The turation to beep. + */ + void beep (float frequency, float time); + + void nobeep(); + +private : + PwmOut _pwm; + Timeout toff; +}; + +} +#endif