|
| 1 | +/****************************************************************************** |
| 2 | + Example_03_Buzz_Duration |
| 3 | +
|
| 4 | + This example shows how to control the buzzer using frequency and duration. |
| 5 | +
|
| 6 | + It turns the buzzer on and off. |
| 7 | + Much like the classic "blink LED sketch" this will buzz |
| 8 | + the buzzer once every second at a different frequency and duration. |
| 9 | +
|
| 10 | + By Pete Lewis @ SparkFun Electronics |
| 11 | + December 2023 |
| 12 | +
|
| 13 | + Based on code originally written by Fischer Moseley @ SparkFun Electronics |
| 14 | + Original Creation Date: June 28, 2019 |
| 15 | +
|
| 16 | + This code is Lemonadeware; if you see me (or any other SparkFun employee) at the |
| 17 | + local, and you've found our code helpful, please buy us a round! |
| 18 | +
|
| 19 | + Hardware Connections: |
| 20 | + Connect QWIIC cable from Arduino to Qwiic Buzzer |
| 21 | +
|
| 22 | + Distributed as-is; no warranty is given. |
| 23 | +******************************************************************************/ |
| 24 | + |
| 25 | +#include <SparkFun_Qwiic_Buzzer_Arduino_Library.h> |
| 26 | +QwiicBuzzer buzzer; |
| 27 | + |
| 28 | +void setup() { |
| 29 | + Serial.begin(115200); |
| 30 | + Serial.println("Qwiic Buzzer Example_03_Buzz_Duration"); |
| 31 | + Wire.begin(); //Join I2C bus |
| 32 | + |
| 33 | + //check if buzzer will acknowledge over I2C |
| 34 | + if (buzzer.begin() == false) { |
| 35 | + Serial.println("Device did not acknowledge! Freezing."); |
| 36 | + while (1); |
| 37 | + } |
| 38 | + Serial.println("Buzzer acknowledged."); |
| 39 | +} |
| 40 | + |
| 41 | +void loop() { |
| 42 | + buzzer.on(2730, 100); // frequency: 2.73KHz, duration: 100ms |
| 43 | + delay(1000); |
| 44 | + |
| 45 | + buzzer.on(1000, 500); // frequency: 1K, duration: 500ms |
| 46 | + delay(1000); |
| 47 | + |
| 48 | + // Note, we dont' have to call buzzer.off(), because it will automatically turn |
| 49 | + // off after the duration of each tone is completed. |
| 50 | +} |
0 commit comments