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:
dreschpe
Date:
Tue Feb 01 18:44:45 2011 +0000
Revision:
0:18e4a9c978ec
Child:
2:a34405c20cf5

        

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 0:18e4a9c978ec 4 using namespace mbed;
dreschpe 0:18e4a9c978ec 5 // constructor
dreschpe 0:18e4a9c978ec 6 Beep::Beep(PinName pin) : _pwm(pin) {
dreschpe 0:18e4a9c978ec 7 _pwm.write(0.0); // after creating it have to be off
dreschpe 0:18e4a9c978ec 8 }
dreschpe 0:18e4a9c978ec 9
dreschpe 0:18e4a9c978ec 10 // switch off
dreschpe 0:18e4a9c978ec 11 void Beep::nobeep() {
dreschpe 0:18e4a9c978ec 12 _pwm.write(0.0);
dreschpe 0:18e4a9c978ec 13 }
dreschpe 0:18e4a9c978ec 14
dreschpe 0:18e4a9c978ec 15
dreschpe 0:18e4a9c978ec 16 // beep
dreschpe 0:18e4a9c978ec 17 void Beep::beep(float freq, float time) {
dreschpe 0:18e4a9c978ec 18
dreschpe 0:18e4a9c978ec 19 _pwm.period(1.0/freq);
dreschpe 0:18e4a9c978ec 20 _pwm.write(0.5); // 50% duty cycle - beep on
dreschpe 0:18e4a9c978ec 21 toff.attach(this,&Beep::nobeep, time); // time to off
dreschpe 0:18e4a9c978ec 22 }
dreschpe 0:18e4a9c978ec 23
dreschpe 0:18e4a9c978ec 24
dreschpe 0:18e4a9c978ec 25
dreschpe 0:18e4a9c978ec 26