First Draft, serial print change based on distance
Embed:
(wiki syntax)
Show/hide line numbers
beep.cpp
00001 #include "beep.h" 00002 #include "mbed.h" 00003 00004 00005 /** class to make sound with a buzzer, based on a PwmOut 00006 * The class use a timeout to switch off the sound - it is not blocking while making noise 00007 * 00008 * Example: 00009 * @code 00010 * // Beep with 1Khz for 0.5 seconds 00011 * #include "mbed.h" 00012 * #include "beep.h" 00013 * 00014 * Beep buzzer(p21); 00015 * 00016 * int main() { 00017 * ... 00018 * buzzer.beep(1000,0.5); 00019 * ... 00020 * } 00021 * @endcode 00022 */ 00023 00024 using namespace mbed; 00025 // constructor 00026 /** Create a Beep object connected to the specified PwmOut pin 00027 * 00028 * @param pin PwmOut pin to connect to 00029 */ 00030 00031 Beep::Beep(PinName PTC10) : _pwm(PTC10) { 00032 _pwm.write(0.0); // after creating it have to be off 00033 } 00034 00035 /** stop the beep instantaneous 00036 * usually not used 00037 */ 00038 void Beep::nobeep() { 00039 _pwm.write(0.0); 00040 } 00041 00042 /** Beep with given frequency and duration. 00043 * 00044 * @param frequency - the frequency of the tone in Hz 00045 * @param time - the duration of the tone in seconds 00046 */ 00047 00048 void Beep::beep(float frequency, float time) { 00049 00050 _pwm.period(1.0/frequency); 00051 _pwm.write(0.5); // 50% duty cycle - beep on 00052 // toff.attach(&Beep::nobeep, time); // time to off 00053 } 00054 00055 00056 00057
Generated on Mon Aug 22 2022 16:53:08 by
1.7.2