Piezo buzzers are one of many easiest issues so as to add to any venture, simply supplying you with a non-visual alert system. At its most simple, they are often so simple as blinking an LED – simply flip a digital pin on and off in your microcontroller, exchange the LED with a buzzer, and you’ve got an audible warning alarm. For those who’ve labored with the Arduino “tone” library, then you recognize that by controlling the frequency despatched to the buzzer, you possibly can change the pitch and truly play tunes. In fact, one of many shortcomings is the truth that you possibly can solely play one word at a time. You could possibly join a number of buzzers to a number of pins, but it surely’s nonetheless solely doable to play one sound, from one buzzer, at a time.
The brand new SparkFun Qwiic Buzzer addresses these points. By including a bit of circuitry to the board, together with an ATtiny84, and making it controllable through I2C, we’ve created what one Funion has referred to as “essentially the most over-engineered buzzer on this planet.” Nevertheless, for somebody like me who may wish to do extra than simply single quantity, monophonic buzzer alerts, this new buzzer that is able to daisy-chaining and enjoying as much as 128 at a time is a dream come true.
Setup with a number of Qwiic Buzzers is quick and straightforward!
HOW IT WORKS
For those who’ve labored with the tone library, then working with our Qwiic buzzer will really feel very acquainted. Whereas there are a couple of other ways to create sounds, the best is to create an integer array for each the notes and their length. Then it’s only a matter of making a “for” loop, matching the variety of loops to the variety of notes in your tune.
Writer’s warning: If utilizing this methodology for polyphonic buzzer songs, creating completely different rhythms, or notes of various durations, is difficult. On the finish of the “for” loop, there’s a delay, which we’ve discovered works greatest at about 1.3 instances the size of the word being performed. This delay applies to all notes, so it forces all notes to begin and cease on the similar time. This may be a problem in, say, the “B” part of the Tremendous Mario Brothers theme, the place the bass line modifications, and differs from the highest two strains. It takes some inventive re-articulation, altering word length values and including rests to make every thing line up. As soon as that was performed, nevertheless, every thing ran fairly easily.
Music notation software program will be extraordinarily useful for adjusting word values to verify all of them align.
AN EXAMPLE TO GET YOU STARTED
Once I began engaged on the demo for the Qwiic Buzzer Video, I assumed, what ought to be “Howdy World!” of buzzers? The “Blink” sketch? The one logical answer gave the impression to be the Tremendous Mario Brothers theme.
I used three voices for this demo, so the very first thing we have to do is change the I2C addresses on two of the boards. That is simply performed utilizing instance 5. Then it’s on to the principle sketch. After importing the Qwiic Buzzer library, I created cases for every of the three buzzers. You’ll be able to title every one no matter works for you – voice1, voice2, voice3; excessive, medium, low; no matter works to your mind. For this instance I went with melodyBuzzer, harmonyBuzzer, and bassBuzzer. Subsequent, I created arrays for the notes of every of the three voices, and the durations of every word. Relying on the size of your music, this may wind up taking on most of your reminiscence. Whereas this instance will for on a SparkFun Redboard or Arduino Uno, if you wish to broaden on this, or have a number of music choices like I did within the video, you’ll undoubtedly wish to use a board with extra reminiscence, like a Redboard Artemis or one among our ESP32 boards. The setup loop is simply what you’d count on – provoke (.start) every buzzer occasion, ensure they’re acknowledged, and also you’re good to go.
You’ll discover that the majority of the enjoying directions occur in a separate perform, referred to as void play_melody(). As soon as we have established the notes and their durations, that each one simply will get fed into this perform, and the music performs!
WHAT THE CODE LOOKS LIKE
/*
* SparkFun Qwiic Buzzer Polyphony Demo
*
* This instance exhibits off the Qwiic Buzzer's capacity
* to comtrol a number of buzzers concurrently by
* enjoying a 3-part association of a bit of little bit of
* the Tremendous Mario Bros Theme.
*
* By Rob Reynolds @ SparkFun Electronics
* February 2024
*
* {Hardware} connections:
* Join three (3) SparkFun Qwiic Buzzers to you
* microcontroller through the Qwiic connectors.
*
* SparkFun code, firmware, and software program is launched underneath the MIT License.
* Please see LICENSE.md for additional particulars.
*
* This code is launched underneath the Beerware License. For those who discover this code
* helpful, and see me or every other Funion on the native, purchase us a beer!
*
* Distributed as-is, with no guarantee *
*
*/
#embrace <SparkFun_Qwiic_Buzzer_Arduino_Library.h> // Import the library for the Qwiic Buzzer
QwiicBuzzer melodyBuzzer; //0x34 (default)
QwiicBuzzer harmonyBuzzer; //0x35 (beforehand modified)
QwiicBuzzer bassBuzzer; //0x36 (beforehand modified)
// notes within the melody:
int melody[] = {
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_REST, // silence (aka "relaxation")
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_REST, // silence (aka "relaxation")
// "A" part begins right here***********************************
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST, // silence (aka "relaxation")
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_REST, // silence (aka "relaxation")
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_REST, // silence (aka "relaxation")
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_AS4,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_G4, //TRIPLETS START
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_A5,
SFE_QWIIC_BUZZER_NOTE_F5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_REST,
// "A" part repeat begins right here ***********************
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_AS4,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_G4, //TRIPLETS START
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_A5,
SFE_QWIIC_BUZZER_NOTE_F5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_REST,
//"B" Part begins right here**************************
SFE_QWIIC_BUZZER_NOTE_REST, //measure 7
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_FS5,
SFE_QWIIC_BUZZER_NOTE_F5,
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 8
SFE_QWIIC_BUZZER_NOTE_GS4,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 9
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_FS5,
SFE_QWIIC_BUZZER_NOTE_F5,
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 10
SFE_QWIIC_BUZZER_NOTE_C6,
SFE_QWIIC_BUZZER_NOTE_C6,
SFE_QWIIC_BUZZER_NOTE_C6,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 11
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_FS5,
SFE_QWIIC_BUZZER_NOTE_F5,
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 12
SFE_QWIIC_BUZZER_NOTE_GS4,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 13
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C5, //measure 14
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST,
};
// Notes within the concord
int concord[] = {
SFE_QWIIC_BUZZER_NOTE_FS4,
SFE_QWIIC_BUZZER_NOTE_FS4,
SFE_QWIIC_BUZZER_NOTE_FS4,
SFE_QWIIC_BUZZER_NOTE_FS4,
SFE_QWIIC_BUZZER_NOTE_FS4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
// "A" part begins right here*********************************
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_D4,
SFE_QWIIC_BUZZER_NOTE_CS4,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_C4, //TRIPLETS START
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_D4,
SFE_QWIIC_BUZZER_NOTE_REST,
// "A" part repeat begins right here*********************************
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_D4,
SFE_QWIIC_BUZZER_NOTE_CS4,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_C4, //TRIPLETS START
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_A4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_D4,
SFE_QWIIC_BUZZER_NOTE_REST,
//"B" Part begins right here**************************
SFE_QWIIC_BUZZER_NOTE_REST, //measure 7
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 8
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 9
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 10
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_G5,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 11
SFE_QWIIC_BUZZER_NOTE_E5,
SFE_QWIIC_BUZZER_NOTE_DS5,
SFE_QWIIC_BUZZER_NOTE_D5,
SFE_QWIIC_BUZZER_NOTE_B4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C5,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 12
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_G4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_REST, //measure 13
SFE_QWIIC_BUZZER_NOTE_GS4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E4, //measure 14
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_REST,
};
// notes within the bass:
int bass[] = {
SFE_QWIIC_BUZZER_NOTE_D3,
SFE_QWIIC_BUZZER_NOTE_D3,
SFE_QWIIC_BUZZER_NOTE_D3,
SFE_QWIIC_BUZZER_NOTE_D3,
SFE_QWIIC_BUZZER_NOTE_D3,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_G2,
SFE_QWIIC_BUZZER_NOTE_REST,
// "A" part begins right here**********************************
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_F3,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_FS3,
SFE_QWIIC_BUZZER_NOTE_F3,
SFE_QWIIC_BUZZER_NOTE_E3, //TRIPLETS START
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_D4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_A3,
SFE_QWIIC_BUZZER_NOTE_B3,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
// "A" part begins right here**********************************
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_E3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_C3,
SFE_QWIIC_BUZZER_NOTE_REST,
SFE_QWIIC_BUZZER_NOTE_F3,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_FS3,
SFE_QWIIC_BUZZER_NOTE_F3,
SFE_QWIIC_BUZZER_NOTE_E3, //TRIPLETS START
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_F4,
SFE_QWIIC_BUZZER_NOTE_D4,
SFE_QWIIC_BUZZER_NOTE_E4,
SFE_QWIIC_BUZZER_NOTE_C4,
SFE_QWIIC_BUZZER_NOTE_A3,
SFE_QWIIC_BUZZER_NOTE_B3,
SFE_QWIIC_BUZZER_NOTE_G3,
SFE_QWIIC_BUZZER_NOTE_REST,
// "B" part begins right here ***************************************
// Numbers point out word durations, only for my reference when writing
SFE_QWIIC_BUZZER_NOTE_C3, //4 measure 7
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_G3, //8
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_C4, //4
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_F3, //4 measure 8
SFE_QWIIC_BUZZER_NOTE_REST, //16
SFE_QWIIC_BUZZER_NOTE_REST, //16
SFE_QWIIC_BUZZER_NOTE_C4, //8
SFE_QWIIC_BUZZER_NOTE_C4, //8
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_F3, //8
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_C3, //4 measure 9
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_G3, //8
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_G3, //8
SFE_QWIIC_BUZZER_NOTE_C4, //8
SFE_QWIIC_BUZZER_NOTE_REST, //2 measure 10
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_G3, //4
SFE_QWIIC_BUZZER_NOTE_C3, //4 measure 11
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_G3, //8
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_C4, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_F3, //4 measure 12
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_C4, //8
SFE_QWIIC_BUZZER_NOTE_C4, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_F3, //4
SFE_QWIIC_BUZZER_NOTE_REST, //4
SFE_QWIIC_BUZZER_NOTE_C3, //4 measure 13
SFE_QWIIC_BUZZER_NOTE_GS3, //4
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_AS3, //4
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_C4, //4 measure 14
SFE_QWIIC_BUZZER_NOTE_REST, //8
SFE_QWIIC_BUZZER_NOTE_G3, //8
SFE_QWIIC_BUZZER_NOTE_G3, //4
SFE_QWIIC_BUZZER_NOTE_C3, //4
};
// word durations: 4 = quarter word, 8 = eighth word, and so on.:
int marioNoteDurations[] = {
8, 4, 4, 8, 4, 4, 4, 4, 4,
4, 8, 4, 8, 4, 8, 4, 4, 8, 4, 6, 6, 6, 4, 8, 4, 4, 8, 8, 4, 8,
4, 8, 4, 8, 4, 8, 4, 4, 8, 4, 6, 6, 6, 4, 8, 4, 4, 8, 8, 4, 8, //begin "B" part subsequent line
4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8, 4, 8, 4, 4,
4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 8, 4, 8, 4, 8, 8, 4, 4
// The prolonged areas above aren't obligatory, I take advantage of them to separate measures
// merely to assist me hold the music extra clear. Very useful for debugging!
};
void setup() {
Serial.start(115200);
Serial.println("Qwiic Buzzer Tremendous Mario Bros Pattern");
Wire.start(); //Be a part of I2C bus
melodyBuzzer.start(0x34);
harmonyBuzzer.start(0x35);
bassBuzzer.start(0x36);
/*
// These are good for testing, I commented them out once I moved the venture to stand-alone
//test if buzzer will join over I2C
if (buzzer1.start() == false) {
Serial.println("Buzzer 1 didn't join! Freezing.");
whereas (1);
}
else if (buzzer2.start() == false) {
Serial.println("Buzzer 2 didn't join! Freezing.");
whereas (1);
}
else if (buzzer3.start() == false) {
Serial.println("Buzzer 3 didn't join! Freezing.");
whereas (1);
}
*/
Serial.println("Buzzers linked.");
Serial.println("Buzzing Melody now...");
play_melody();
}
void loop() {
// do nothing right here
// we simply play the melody as soon as throughout setup above.
}
void play_melody()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 103; thisNote++) { // 51 for A piece solely (repeated), 97 for complete demo for whole melody/concord notes, add 8 rests to bass
// to calculate the word length, take one second divided by the word sort.
//e.g. quarter word = 1000 / 4, eighth word = 1000/8, and so on.
int melodyNoteDuration = 1000 / marioNoteDurations[thisNote]; // change quantity to alter tempo. <1000 = sooner; >1000 = slower
melodyBuzzer.configureBuzzer(melody[thisNote], melodyNoteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX);
melodyBuzzer.on();
harmonyBuzzer.configureBuzzer(concord[thisNote], melodyNoteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX);
harmonyBuzzer.on();
bassBuzzer.configureBuzzer(bass[thisNote], melodyNoteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX);
bassBuzzer.on();
// to tell apart the notes, set a minimal time between them.
// the word's length + 30% appears to work effectively:
int pauseBetweenNotes = melodyNoteDuration * 1.30;
delay(pauseBetweenNotes);
}
}
For musicians beginning out in coding, this will appear a bit overwhelming. For those who’re a coder with out a lot of a background in music, this will appear equally daunting. Beneath is a graphic displaying the notes and their octaves. You’ll be able to lengthen beneath and above the notes proven, however that ought to offer you place to begin. Be aware additionally that we do not use F#, G#, and so on, quite we use FS, GS, and so on, due to the #’s place in C++.
Notes and their octaves. Since we will not use # as an unintended, we is S to point sharps.
NOW IT’S YOUR TURN TO CREATE
This sketch is a good place to begin. From right here, all you could do to create a brand new tune is change the values of the notes and their durations. And whereas which will seem to be quite a bit – every word requires a line that appears like
SFE_QWIIC_BUZZER_NOTE_C4,
- all you could do is change the word worth itself, ie C4 turns into E5. Then arrow down, a few backspaces, repeat, and you may really get by way of it fairly rapidly.
A few issues that I discovered extraordinarily useful through the course of embrace commenting measure numbers when creating the “notes” integers, or at the least commenting very clear sections. You will discover within the above sketch that I remark when every new part begins, and when the triplets begin within the “A” part. In defining the durations, I’ve put further areas between values to point measure separations. Once more, this is not in any respect obligatory, I simply discover it extraordinarily useful when debugging your code, determining the place you’ve flawed notes or flawed word values, issues like that.
I will be including a pull request in our Github repo to make it possible for everybody who needs it has entry to this code. For those who’ve provide you with some wonderful buzzer tunes, I encourage you to do the identical. I ended at three Qwiic Buzzers for my demo, however that does not imply that you could. If anybody needs to deal with, say, Bernstein’s Overture to Candide, or Mussorgsky’s Footage at an Exhibition, I might be each excited and terrified to listen to that!
Cowl picture: SparkFun Qwiic Buzzer debuting on the Joan and Sanford I. Weill Recital Corridor, Carnagie Corridor, NYC. Recital corridor picture courtesy of Carnagie Corridor Group.