Skip to content

Commit 750d40a

Browse files
committed
samples: Add tone melody
- Use `tone` and `noTone` functions to play a melody on piezo buzzer. - Taken from https://github.com/arduino/arduino-examples/blob/main/examples/02.Digital/toneMelody/toneMelody.ino - I have only tested on LED since I do not have a buzzer. Signed-off-by: Ayush Singh <[email protected]>
1 parent 616549e commit 750d40a

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

samples/tone_melody/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
6+
set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(tone_melody)
10+
11+
target_sources(app PRIVATE src/main.cpp)
12+
13+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/tone_melody/Kconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright (c) 2024 Ayush Singh <[email protected]>
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
mainmenu "Tone melody sample application"
8+
9+
config TONE_OUT_DIGITAL_PIN_NUMBER
10+
int "Buzzer Pin Number"
11+
default 8
12+
13+
source "Kconfig.zephyr"

samples/tone_melody/README.rst

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. _blinky-tone-sample:
2+
3+
Tone Melody
4+
############
5+
6+
Overview
7+
********
8+
9+
This Arduino sample Plays a melody.
10+
11+
Requirements
12+
************
13+
14+
Your board must:
15+
16+
#. Have piezo buzzer connected via a GPIO pin.
17+
#. GPIO pin can be selected using CONFIG_TONE_OUT_DIGITAL_PIN_NUMBER.
18+
19+
Building and Running
20+
********************
21+
22+
Build and flash Blinky as follows,
23+
24+
```sh
25+
$> west build -p -b arduino_nano_33_ble samples/basic/tone_melody/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
26+
27+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
28+
```
29+
30+
After flashing, the buzzer starts to play a melody.
31+
32+
Adding board support
33+
********************
34+
35+
To add support for your board, set CONFIG_TONE_OUT_DIGITAL_PIN_NUMBER
36+
according to your connections.

samples/tone_melody/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_GPIO=y
2+
CONFIG_CPLUSPLUS=y
3+
CONFIG_ARDUINO_API=y

samples/tone_melody/src/main.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Melody
3+
*
4+
* Plays a melody
5+
*
6+
* circuit:
7+
* - 8 ohm speaker on digital pin 8
8+
*
9+
* created 21 Jan 2010
10+
* modified 30 Aug 2011
11+
* by Tom Igoe
12+
*
13+
* This example code is in the public domain.
14+
*
15+
* https://docs.arduino.cc/built-in-examples/digital/toneMelody/
16+
*
17+
* SPDX-License-Identifier: CC-BY-1.0
18+
*/
19+
20+
#include "pitches.h"
21+
#include <Arduino.h>
22+
23+
// notes in the melody:
24+
int melody[] = {
25+
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
26+
};
27+
28+
// note durations: 4 = quarter note, 8 = eighth note, etc.:
29+
int noteDurations[] = {
30+
4, 8, 8, 4, 4, 4, 4, 4
31+
};
32+
33+
void setup() {
34+
// iterate over the notes of the melody:
35+
for (int thisNote = 0; thisNote < 8; thisNote++) {
36+
37+
// to calculate the note duration, take one second divided by the note type.
38+
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
39+
int noteDuration = 1000 / noteDurations[thisNote];
40+
tone(CONFIG_TONE_OUT_DIGITAL_PIN_NUMBER, melody[thisNote], noteDuration);
41+
42+
// to distinguish the notes, set a minimum time between them.
43+
// the note's duration + 30% seems to work well:
44+
int pauseBetweenNotes = noteDuration * 1.30;
45+
delay(pauseBetweenNotes);
46+
// stop the tone playing:
47+
noTone(CONFIG_TONE_OUT_DIGITAL_PIN_NUMBER);
48+
}
49+
}
50+
51+
void loop() {
52+
// no need to repeat the melody.
53+
}

samples/tone_melody/src/pitches.h

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

0 commit comments

Comments
 (0)