Skip to content

Instantly share code, notes, and snippets.

@reefwing
Last active November 14, 2020 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reefwing/b0b73851b9e72366dab6b405e4c4265d to your computer and use it in GitHub Desktop.
Save reefwing/b0b73851b9e72366dab6b405e4c4265d to your computer and use it in GitHub Desktop.
Play tones on a piezo buzzer using Mbed Bit Banging - Arduino Nano 33 BLE & Portenta H7
void playTone(int pin, int period_us, int duration_ms) {
mbed::DigitalOut buzzer(digitalPinToPinName( pin ));
static int time_on, time_off;
unsigned long start_time, end_time;
start_time = millis();
end_time = start_time;
while ((end_time - start_time) <= duration_ms) {
time_on = period_us / 2;
time_off = period_us - time_on;
buzzer = 1;
wait_us( time_on );
buzzer = 0;
wait_us( time_off );
end_time = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment