KPE Buzzer

mail@pastecode.io avatar
unknown
c_cpp
a year ago
742 B
15
Indexable
#include "mbed.h"
#define C 261.63
#define C_S 277.18
#define D 293.66
#define D_S 311.13
#define E 329.63
#define F 349.23
#define F_S 369.99
#define G 392
#define G_S 415.3
#define A 440
#define A_S 466.16
#define B 493.88
class Buzzer {
public:
Buzzer(PinName pin) : _pin(pin) {}

void beep(int freq, float time) {
    _pin = freq? 0.5:0;
    _pin.period(freq? 1.0/freq :1);
    wait(time);
}
private:
    PwmOut _pin;
};
Buzzer buzzer(PA_4);
int main() {

    short freq[] = {G, G, E, 0,G,G,E,0,G,G,A,G,G,F,0}; //frekvence
    double time[] = {0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,0}; //cas

    for(int i = 0; i < sizeof(freq), i++)
    {
        buzzer.beep(freq[i], time[i]);
    }

}