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