Arduino Buzzer Sound Non Blocking

by AfricanCNC in Circuits > Arduino

65 Views, 0 Favorites, 0 Comments

Arduino Buzzer Sound Non Blocking

SDXL_09_Can_you_create_a_picture_from_an_ESP32_playing_a_sound_0.jpg
20240929_114755.jpg

In my current controller project, which required continuous sensor monitoring and adjustments without delays or blocking sequences in the Arduino code running on an ESP32,

I encountered challenges with various libraries offering non-blocking functionality, especially concerning Piezo sound.

However, upon discovering Albert van Dalen's VirtualDelay library on his website 10 , the solution to my requirements materialized. Leveraging this library alongside the straightforward Arduino tone command, I successfully integrated sound effects such as playing tones when a button is pressed or issuing warnings seamlessly within my system's operational flow without impeding the critical monitoring processes.

Supplies

maxresdefault.jpg

TOOLS USED IN THIS PROJECT


To successfully implement the code for playing sound without blocking your Arduino code using any Arduino board and a Piezo speaker (not a buzzer), you will require the following supplies:


1. **Arduino Board**: Any Arduino board compatible with the Arduino IDE will work for this project. Popular options include Arduino Uno, Arduino Nano, or ESP32.


2. **Piezo Speaker**: You will need a piezo speaker instead of a buzzer. A piezo speaker requires an oscillating signal to produce sound, making it ideal for non-blocking sound generation in your project.


3. **Jumper Wires**: You will need jumper wires of appropriate lengths to establish connections between the Arduino board and the piezo speaker.


4. **Resistor (optional)**: Depending on the piezo speaker's power rating and the output pin of your Arduino board, you may need a suitable resistor to protect the components and ensure proper functioning.


5. **VirtualDelay Library by Albert van Dalen**: Download the VirtualDelay library from Albert van Dalen's website [link to website]. This library will enable you to implement non-blocking delay functions in your Arduino code, which is crucial for maintaining the continuous supervision and adjustments required in your project.


With these supplies in hand and the VirtualDelay library integrated into your Arduino project, you can effectively play sound through a piezo speaker without blocking your code, ensuring seamless operation of your controller system.

The Non Blocking Sketch.

_R0jWVA9Ez5.blob.jpeg

1. **Setup Function**:

The `setup()` function is a fundamental part of any Arduino sketch and is called once when the Arduino board is powered up or reset. In this project:

The serial communication is initialized with a baud rate of 112500, enabling communication with the Serial Monitor for debugging purposes.

The pins for the buttons and the Piezo speaker are configured in the `INPUT_PULLUP` and `OUTPUT` modes, respectively. The `INPUT_PULLUP` mode ensures the button pins are kept at a HIGH state unless activated.


2. **Loop Function**:

The `loop()` function is a standard Arduino function that runs continuously after the `setup()` function completes. In this project:

The `loop()` function calls two essential tasks: `PlaySound()` and `ButtonPress()`.

`PlaySound()` plays different melodies on the piezo speaker with non-blocking delays, ensuring the Arduino can perform.


3.**Customizing**:

To personalize the project to your own needs, consider the following customization options:

  1. Changing Melodies and Tones: You can modify the melodies and tones played by the piezo speaker in the PlaySound() function. Adjust the frequency and timing of tones to create specific musical sequences or sound effects. To do this, update the tone() and delay.start() commands within the PlaySound() function with your desired frequencies and timing.


  1. Integrating Additional Buttons: If you wish to incorporate more buttons to trigger different sounds or actions, you can expand the ButtonPress() function to include additional button checks and corresponding actions.


  1. Adapting the Delay Durations: The delay.start() commands in the PlaySound() function control the timing between different tones. You can customize these durations to create unique rhythm patterns or synchronize with your project's other aspects.


  1. Expanding Functionality: Beyond playing sounds, you can integrate additional actions based on user input, sensor readings, or system states. This could include controlling external devices, displaying information on a display, or triggering specific events.


  1. Utilizing Serial Communication: Leveraging the initialized Serial communication in the setup() function, you can add debugging messages, interact with a computer or other device, or receive user input through the Serial Monitor.


Snip of the Arduino .ino File

/*This code is created with the Virtual Library of Albert van Dalen

Check his Website for more options with this Library

https://avdweb.nl/arduino/timing/virtualdelay

Download the Library from GitHub

https://github.com/avdwebLibraries/avdweb_VirtualDelay

Implemented as a nonblocking sound player with Piezo Speaker

with Arduino Tone function. by C. van Hal

*/


#include "avdweb_VirtualDelay.h" // Include the VirtualDelay library


#define ButtonPin1 8 // Define the pin for the first button

#define ButtonPin2 7 // Define the pin for the second button

#define PIEZO_PIN 13 // Define the pin for the piezo speaker


// Define VirtualDelay objects for each sound and delay

VirtualDelay VariedTone, delay2, delay3, delay4, // Melody 1

VariedToneReverse, delay6, delay7, delay8, // Melody 2

ErrorWarning, delay10, delay11, delay12, // Melody 3

ClickSound, delay14, // Melody 4

BeepEnter, delay16, delay17, // Melody 5

BeepLeave, delay19, delay20; // Melody 6


// Regular setup

void setup() {

Serial.begin(112500); // Initialize serial communication

Serial.println("\ntestSequence"); // Print a message to the Serial Monitor

pinMode(ButtonPin1, INPUT_PULLUP); // Set the first button pin to input with internal pull-up resistor

pinMode(ButtonPin2, INPUT_PULLUP); // Set the second button pin to input with internal pull-up resistor

pinMode(PIEZO_PIN, OUTPUT); // Set the piezo pin to output

}


// Main loop

void loop() {

PlaySound(); // Call the function to play sound

ButtonPress(); // Call the function to handle button presses

}


// Function to handle button presses

void ButtonPress() {

if (digitalRead(ButtonPin1) == LOW) { // Check if the first button is pressed

delay(500); // Add a brief delay

BeepEnter.start(1); // Start playing a sound using the VirtualDelay object

}


if (digitalRead(ButtonPin2) == LOW) { // Check if the second button is pressed

delay(500); // Add a brief delay

BeepLeave.start(1); // Start playing a sound using the VirtualDelay object

}

}


// Function to play sound using non-blocking delays

void PlaySound() {

// Each section plays a different melody using non-blocking delays

//============================

if (VariedTone.elapsed()) {

tone(PIEZO_PIN, 1000);

delay2.start(100);

}

if (delay2.elapsed()) {

tone(PIEZO_PIN, 1200);

delay3.start(100);

}

//============================

// ... (additional code for other melodies and delays)

}