Last active
November 14, 2020 04:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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