Skip to content

Commit afd54dd

Browse files
committed
ex7 play melody working
1 parent cb6d488 commit afd54dd

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/******************************************************************************
2+
Example_07_Buzz_Melody
3+
4+
This example shows how to buzz a melody on the Qwiic Buzzer
5+
6+
By Pete Lewis @ SparkFun Electronics
7+
December 2023
8+
9+
Based on code originally written by Fischer Moseley @ SparkFun Electronics
10+
Original Creation Date: June 28, 2019
11+
12+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
13+
local, and you've found our code helpful, please buy us a round!
14+
15+
Hardware Connections:
16+
Connect QWIIC cable from Arduino to Qwiic Buzzer
17+
18+
Distributed as-is; no warranty is given.
19+
******************************************************************************/
20+
21+
#include <SparkFun_Qwiic_Buzzer_Arduino_Library.h>
22+
QwiicBuzzer buzzer;
23+
24+
// notes in the melody:
25+
int melody[] = {
26+
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
27+
};
28+
29+
// note durations: 4 = quarter note, 8 = eighth note, etc.:
30+
int noteDurations[] = {
31+
4, 8, 8, 4, 4, 4, 4, 4
32+
};
33+
34+
#define BUZZER_VOLUME 4 // loudest!!
35+
//#define BUZZER_VOLUME 3 // pretty good volume for most things
36+
37+
void setup() {
38+
Serial.begin(115200);
39+
Serial.println("Qwiic Buzzer Example_07_Buzz_Melody");
40+
Wire.begin(); //Join I2C bus
41+
42+
//check if buzzer will acknowledge over I2C
43+
if (buzzer.begin() == false) {
44+
Serial.println("Device did not acknowledge! Freezing.");
45+
while (1);
46+
}
47+
Serial.println("Buzzer acknowledged.");
48+
49+
Serial.println("Buzzing Melody now...");
50+
play_melody();
51+
}
52+
53+
void loop() {
54+
// do nothing here
55+
// we just play the melody once during setup above.
56+
}
57+
58+
void play_melody()
59+
{
60+
// iterate over the notes of the melody:
61+
for (int thisNote = 0; thisNote < 8; thisNote++) {
62+
63+
// to calculate the note duration, take one second divided by the note type.
64+
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
65+
int noteDuration = 1000 / noteDurations[thisNote];
66+
buzzer.on(melody[thisNote], noteDuration, BUZZER_VOLUME);
67+
68+
// to distinguish the notes, set a minimum time between them.
69+
// the note's duration + 30% seems to work well:
70+
int pauseBetweenNotes = noteDuration * 1.30;
71+
delay(pauseBetweenNotes);
72+
// stop the tone playing:
73+
buzzer.off();
74+
}
75+
}

src/SparkFun_Qwiic_Buzzer_Arduino_Library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Distributed as-is; no warranty is given.
2828
#include <Wire.h>
2929
#include <Arduino.h>
3030
#include "registers.h"
31+
#include "buzzer_pitches.h"
3132

3233
#define SFE_QWIIC_BUZZER_DEFAULT_ADDRESS 0x34 //default I2C address of the buzzer
3334
#define SFE_QWIIC_BUZZER_DEV_ID 0x5D //device ID of the Qwiic Buzzer

src/buzzer_pitches.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*************************************************
2+
Public Constants
3+
*************************************************/
4+
5+
#define NOTE_B0 31
6+
#define NOTE_C1 33
7+
#define NOTE_CS1 35
8+
#define NOTE_D1 37
9+
#define NOTE_DS1 39
10+
#define NOTE_E1 41
11+
#define NOTE_F1 44
12+
#define NOTE_FS1 46
13+
#define NOTE_G1 49
14+
#define NOTE_GS1 52
15+
#define NOTE_A1 55
16+
#define NOTE_AS1 58
17+
#define NOTE_B1 62
18+
#define NOTE_C2 65
19+
#define NOTE_CS2 69
20+
#define NOTE_D2 73
21+
#define NOTE_DS2 78
22+
#define NOTE_E2 82
23+
#define NOTE_F2 87
24+
#define NOTE_FS2 93
25+
#define NOTE_G2 98
26+
#define NOTE_GS2 104
27+
#define NOTE_A2 110
28+
#define NOTE_AS2 117
29+
#define NOTE_B2 123
30+
#define NOTE_C3 131
31+
#define NOTE_CS3 139
32+
#define NOTE_D3 147
33+
#define NOTE_DS3 156
34+
#define NOTE_E3 165
35+
#define NOTE_F3 175
36+
#define NOTE_FS3 185
37+
#define NOTE_G3 196
38+
#define NOTE_GS3 208
39+
#define NOTE_A3 220
40+
#define NOTE_AS3 233
41+
#define NOTE_B3 247
42+
#define NOTE_C4 262
43+
#define NOTE_CS4 277
44+
#define NOTE_D4 294
45+
#define NOTE_DS4 311
46+
#define NOTE_E4 330
47+
#define NOTE_F4 349
48+
#define NOTE_FS4 370
49+
#define NOTE_G4 392
50+
#define NOTE_GS4 415
51+
#define NOTE_A4 440
52+
#define NOTE_AS4 466
53+
#define NOTE_B4 494
54+
#define NOTE_C5 523
55+
#define NOTE_CS5 554
56+
#define NOTE_D5 587
57+
#define NOTE_DS5 622
58+
#define NOTE_E5 659
59+
#define NOTE_F5 698
60+
#define NOTE_FS5 740
61+
#define NOTE_G5 784
62+
#define NOTE_GS5 831
63+
#define NOTE_A5 880
64+
#define NOTE_AS5 932
65+
#define NOTE_B5 988
66+
#define NOTE_C6 1047
67+
#define NOTE_CS6 1109
68+
#define NOTE_D6 1175
69+
#define NOTE_DS6 1245
70+
#define NOTE_E6 1319
71+
#define NOTE_F6 1397
72+
#define NOTE_FS6 1480
73+
#define NOTE_G6 1568
74+
#define NOTE_GS6 1661
75+
#define NOTE_A6 1760
76+
#define NOTE_AS6 1865
77+
#define NOTE_B6 1976
78+
#define NOTE_C7 2093
79+
#define NOTE_CS7 2217
80+
#define NOTE_D7 2349
81+
#define NOTE_DS7 2489
82+
#define NOTE_E7 2637
83+
#define NOTE_F7 2794
84+
#define NOTE_FS7 2960
85+
#define NOTE_G7 3136
86+
#define NOTE_GS7 3322
87+
#define NOTE_A7 3520
88+
#define NOTE_AS7 3729
89+
#define NOTE_B7 3951
90+
#define NOTE_C8 4186
91+
#define NOTE_CS8 4435
92+
#define NOTE_D8 4699
93+
#define NOTE_DS8 4978

0 commit comments

Comments
 (0)