Skip to content

Commit ff22d61

Browse files
committed
ex8 sound effects working
1 parent afd54dd commit ff22d61

File tree

4 files changed

+344
-0
lines changed

4 files changed

+344
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/******************************************************************************
2+
Example_08_Sound_Effects
3+
4+
This example demos the sound effects included in this library.
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+
Sound effects based on the following work:
13+
Jan 21st, 2020
14+
Snake In A Can Controller
15+
Written by: Pete Lewis, with contributions from Jeff Haas
16+
A collaboration with Mario the Maker Magician
17+
https://www.mariothemagician.com/
18+
19+
January, 2021
20+
Cry, Laugh Functions were adapted from Adafruit animal sounds
21+
by Magician/hacker Jeff Haas. Thanks Jeff!!
22+
https://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds
23+
24+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
25+
local, and you've found our code helpful, please buy us a round!
26+
27+
Hardware Connections:
28+
Connect QWIIC cable from Arduino to Qwiic Buzzer
29+
30+
Distributed as-is; no warranty is given.
31+
******************************************************************************/
32+
33+
#include <SparkFun_Qwiic_Buzzer_Arduino_Library.h>
34+
QwiicBuzzer buzzer;
35+
36+
//#define BUZZER_VOLUME 4 // loudest!!
37+
#define BUZZER_VOLUME 3 // pretty good volume for most things
38+
39+
void setup() {
40+
Serial.begin(115200);
41+
Serial.println("Qwiic Buzzer Example_08_Sound_Effects");
42+
Wire.begin(); //Join I2C bus
43+
44+
//check if buzzer will acknowledge over I2C
45+
if (buzzer.begin() == false) {
46+
Serial.println("Device did not acknowledge! Freezing.");
47+
while (1);
48+
}
49+
Serial.println("Buzzer acknowledged.");
50+
51+
for(int i = 0 ; i <= 9 ; i++)
52+
{
53+
Serial.print("Sound Effect: ");
54+
Serial.println(i);
55+
buzzer.playSoundEffect(i, BUZZER_VOLUME);
56+
delay(2000);
57+
}
58+
}
59+
60+
void loop() {
61+
// do nothing
62+
// we play through all the available sound effects above in setup once.
63+
}

src/SparkFun_Qwiic_Buzzer_Arduino_Library.cpp

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ Based on original source code written by
99
Fischer Moseley @ SparkFun Electronics
1010
Original Creation Date: July 24, 2019
1111
12+
Sound effects based on the following work:
13+
Jan 21st, 2020
14+
Snake In A Can Controller
15+
Written by: Pete Lewis, with contributions from Jeff Haas
16+
A collaboration with Mario the Maker Magician
17+
https://www.mariothemagician.com/
18+
19+
January, 2021
20+
Cry, Laugh Functions were adapted from Adafruit animal sounds
21+
by Magician/hacker Jeff Haas. Thanks Jeff!!
22+
https://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds
23+
1224
This file implements the QwiicBuzzer class, prototyped in SparkFun_Qwiic_Buzzer_Arduino_Library.h
1325
1426
Development environment specifics:
@@ -256,4 +268,261 @@ uint16_t QwiicBuzzer::writeDoubleRegisterWithReadback(Qwiic_Buzzer_Register reg,
256268
if (readDoubleRegister(reg) != data)
257269
return 2;
258270
return 0;
271+
}
272+
273+
/*------------------------- Sound Effects ---------------- */
274+
275+
void QwiicBuzzer::playSoundEffect(uint8_t soundEffectNumber, uint8_t volume)
276+
{
277+
switch (soundEffectNumber) {
278+
case 0:
279+
soundEffect0(volume);
280+
break;
281+
case 1:
282+
soundEffect1(volume);
283+
break;
284+
case 2:
285+
soundEffect2(volume);
286+
break;
287+
case 3:
288+
soundEffect3(volume);
289+
break;
290+
case 4:
291+
soundEffect4(volume);
292+
break;
293+
case 5:
294+
soundEffect5(volume);
295+
break;
296+
case 6:
297+
soundEffect6(volume);
298+
break;
299+
case 7:
300+
soundEffect7(volume);
301+
break;
302+
case 8:
303+
soundEffect8(volume);
304+
break;
305+
case 9:
306+
soundEffect9(volume);
307+
break;
308+
}
309+
}
310+
311+
// SIREN SLOW X1
312+
void QwiicBuzzer::soundEffect0(uint8_t volume)
313+
{
314+
for (int note = 150 ; note < 4000 ; note += 150)
315+
{
316+
on(note, 10, volume);
317+
delay(10);
318+
}
319+
for (int note = 4000 ; note > 150 ; note -= 150)
320+
{
321+
on(note, 10, volume);
322+
delay(10);
323+
}
324+
off();
325+
}
326+
327+
// SIREN FAST X3
328+
void QwiicBuzzer::soundEffect1(uint8_t volume)
329+
{
330+
for (int i = 0 ; i <= 2 ; i++)
331+
{
332+
for (int note = 150 ; note < 4000 ; note += 150)
333+
{
334+
on(note, 2, volume);
335+
delay(2);
336+
}
337+
for (int note = 4000 ; note > 150 ; note -= 150)
338+
{
339+
on(note, 2, volume);
340+
delay(2);
341+
}
342+
}
343+
off();
344+
}
345+
346+
// YES SLOW
347+
void QwiicBuzzer::soundEffect2(uint8_t volume)
348+
{
349+
for (int note = 150 ; note < 4000 ; note += 150)
350+
{
351+
on(note, 50, volume);
352+
delay(40);
353+
}
354+
off();
355+
}
356+
357+
// YES FAST
358+
void QwiicBuzzer::soundEffect3(uint8_t volume)
359+
{
360+
for (int note = 150 ; note < 4000 ; note += 150)
361+
{
362+
on(note, 10, volume);
363+
delay(10);
364+
}
365+
off();
366+
}
367+
368+
// NO SLOW
369+
void QwiicBuzzer::soundEffect4(uint8_t volume)
370+
{
371+
for (int note = 4000 ; note > 150 ; note -= 150)
372+
{
373+
on(note, 50, volume);
374+
delay(40);
375+
}
376+
off();
377+
}
378+
379+
// NO FAST
380+
void QwiicBuzzer::soundEffect5(uint8_t volume)
381+
{
382+
for (int note = 4000 ; note > 150 ; note -= 150)
383+
{
384+
on(note, 10, volume);
385+
delay(10);
386+
}
387+
off();
388+
}
389+
390+
// LAUGH
391+
void QwiicBuzzer::soundEffect6(uint8_t volume)
392+
{
393+
int laughdelay = 400;
394+
int laughstep = 10;
395+
uint16_t i;
396+
397+
//for (i = 650; i > 525; i -= 3) // vary up //1538, 1905
398+
for (i = 1538; i < 1905; i += laughstep) // vary up //1538, 1905
399+
{
400+
on(i, 20, volume);
401+
delay(10);
402+
}
403+
404+
delay(laughdelay);
405+
406+
//for (i = 800; i > 660; i -= 3) // 1250, 1515
407+
for (i = 1250; i < 1515; i += laughstep) // 1250, 1515
408+
{
409+
on(i, 20, volume);
410+
delay(10);
411+
}
412+
delay(laughdelay);
413+
414+
//for (i = 900; i > 745; i -= 3) // 1111, 1342
415+
for (i = 1111; i < 1342; i += laughstep) // 1111, 1342
416+
{
417+
on(i, 20, volume);
418+
delay(10);
419+
}
420+
delay(laughdelay);
421+
422+
//for (i = 990; i > 850; i -= 3) // 1010, 1176
423+
for (i = 1010; i < 1176; i += laughstep) // 1010, 1176
424+
{
425+
on(i, 20, volume);
426+
delay(10);
427+
}
428+
off();
429+
}
430+
431+
// LAUGH FAST
432+
void QwiicBuzzer::soundEffect7(uint8_t volume)
433+
{
434+
int laughdelay = 200;
435+
int laughstep = 15;
436+
uint16_t i;
437+
438+
for (i = 1538; i < 1905; i += laughstep) // vary up //1538, 1905
439+
{
440+
on(i, 20, volume);
441+
delay(10);
442+
}
443+
444+
delay(laughdelay);
445+
446+
for (i = 1250; i < 1515; i += laughstep) // 1250, 1515
447+
{
448+
on(i, 20, volume);
449+
delay(10);
450+
}
451+
delay(laughdelay);
452+
453+
for (i = 1111; i < 1342; i += laughstep) // 1111, 1342
454+
{
455+
on(i, 20, volume);
456+
delay(10);
457+
}
458+
delay(laughdelay);
459+
460+
for (i = 1010; i < 1176; i += laughstep) // 1010, 1176
461+
{
462+
on(i, 20, volume);
463+
delay(10);
464+
}
465+
off();
466+
}
467+
468+
// CRY SLOW
469+
void QwiicBuzzer::soundEffect8(uint8_t volume)
470+
{
471+
int crydelay = 500;
472+
int step = 10;
473+
uint16_t i;
474+
475+
for (i = 2000; i > 1429; i -= step) // vary down //2000, 1429
476+
{
477+
on(i, 20, volume);
478+
delay(10);
479+
}
480+
481+
delay(crydelay);
482+
483+
for (i = 1667; i > 1250; i -= step) // 1667, 1250
484+
{
485+
on(i, 20, volume);
486+
delay(10);
487+
}
488+
delay(crydelay);
489+
490+
//for (i = 900; i > 745; i -= 3) // 1111, 1342
491+
for (i = 1429; i > 1053; i -= step) // 1429, 1053
492+
{
493+
on(i, 20, volume);
494+
delay(10);
495+
}
496+
off();
497+
}
498+
499+
// CRY FAST
500+
void QwiicBuzzer::soundEffect9(uint8_t volume)
501+
{
502+
int crydelay = 200;
503+
int step = 20;
504+
uint16_t i;
505+
506+
for (i = 2000; i > 1429; i -= step) // vary down //2000, 1429
507+
{
508+
on(i, 20, volume);
509+
delay(10);
510+
}
511+
512+
delay(crydelay);
513+
514+
for (i = 1667; i > 1250; i -= step) // 1667, 1250
515+
{
516+
on(i, 20, volume);
517+
delay(10);
518+
}
519+
delay(crydelay);
520+
521+
//for (i = 900; i > 745; i -= 3) // 1111, 1342
522+
for (i = 1429; i > 1053; i -= step) // 1429, 1053
523+
{
524+
on(i, 20, volume);
525+
delay(10);
526+
}
527+
off();
259528
}

src/SparkFun_Qwiic_Buzzer_Arduino_Library.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ class QwiicBuzzer
5959
bool setBuzzerActiveReg();
6060
bool clearBuzzerActiveReg();
6161

62+
void playSoundEffect(uint8_t soundEffectNumber, uint8_t volume);
63+
void soundEffect0(uint8_t volume);
64+
void soundEffect1(uint8_t volume);
65+
void soundEffect2(uint8_t volume);
66+
void soundEffect3(uint8_t volume);
67+
void soundEffect4(uint8_t volume);
68+
void soundEffect5(uint8_t volume);
69+
void soundEffect6(uint8_t volume);
70+
void soundEffect7(uint8_t volume);
71+
void soundEffect8(uint8_t volume);
72+
void soundEffect9(uint8_t volume);
73+
6274
//Internal I2C Abstraction
6375
uint8_t readSingleRegister(Qwiic_Buzzer_Register reg); //Reads a single 8-bit register.
6476
uint16_t readDoubleRegister(Qwiic_Buzzer_Register reg); //Reads a 16-bit register (little endian).

0 commit comments

Comments
 (0)