buzzer

marcoaita/malibrary/buzzer

A wrapper around the tone command for Arduino. It allows to use the buzzer on the Ks0183 keyestudio Multi-purpose Shield V1
buzzer
@/buzzer
A wrapper around the tone command for Arduino. It allows to use the buzzer on the Ks0183 keyestudio Multi-purpose Shield V1
PORTport
ENboolean
FREQnumber
buzzer
PORT
EN
FREQ
DONE
DONEpulse
To use the node in your project you should have the marcoaita/malibrary library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
    bool enableState=false;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    State* state = getState(ctx);
    auto port = getValue<input_PORT>(ctx);
    auto freq = getValue<input_FREQ>(ctx);
    auto enable = getValue<input_EN>(ctx);
    
    if (enable) {
        state->enableState=true;  // we enabled the buzzer
        pinMode(port, OUTPUT);
        tone(port, freq);
    } else {
        noTone(port);
        if (state->enableState==true) { //buzzer was on 
        emitValue<output_DONE>(ctx, true);  //emits a pulse when done producing sound
        pinMode(port, INPUT);                 // put the output into a tri-state to silence even stubborn buzzers (this works whatever the driving transistor is, PNP or NPN)
        state->enableState=false;  //reset state
        }
    }
}