Play a melody on the Arduino and blink the LED to the beat
Simple Arduino project to play a melody using notes and blink an LED to the tune of the melody.
Could be used to play melody and blink the Christmas lights.
Turn on the LED
For each note in the melody do:
Get the note name
Get beat for the note
Calculate note playing duration = number of beats multiply by Melody.tempo
(duration of one beat, defined for each melody)
Get note frequency by its name from the music_structures.h
Play the tone async, playNote()
Blink by the LED, blink()
:
Turn off (or fade down),
Delay milis of BLINK_DURATION
Turn on
Delay until playing note is finished
Delay for a tiny gap between notes to make melody more intence, PAUSE_BETWEEN_NOTES
Note
If you want to fade out and fade in the light using the analogWrite
function, do not use pin 13 (LED_BUILTIN
) because it is digital-only.
To load multiple melodies with defined notes, their definitions are placed in PROGMEM structures. This approach stores data in Arduino flash memory, preventing excessive RAM usage.
To retrieve data from flash memory:
Note notes[melody.length];
memcpy_P(¬es, melody.notes, melody.length * sizeof(Note));
create h-file for the melody
include it into arduino-play-melody.ino
Define notes in the Letter notation :
const Note NEW_MELODY_NOTES[] PROGMEM = {
{"C4", 1},
};
where
C4
- name of the note is C in the 4th octave.
1
- is float number of beats for the note.
Define melody data:
const Melody NEW_MELODY PROGMEM = {
tempo: 300,
length: lengthOfNotes(NEW_MELODY_NOTES),
octaveShift: 0,
notes: NEW_MELODY_NOTES
};
where:
tempo
- duration of one beat in ms
length
- number of notes in the notes array
octaveShift
- octave shift of played notes:
-1 - one octave down
0 - no shift
1 - one octave up
Add melody into the MELODIES
array in the arduino-play-melody.ino
const Melody *MELODIES[] = {
...,
&NEW_MELODY,
};
Notes in the music_structures.h are defined in Letter notation .
Tones of the notes are defined by the A440 standard :
A440 (also known as Stuttgart pitch) is the musical pitch corresponding to an audio frequency of 440 Hz, which serves as a tuning standard for the musical note of A above middle C, or A4 in scientific pitch notation.
Frequencies for equal-tempered scale, A4 = 440 Hz:
The frequency of any note can be calculated based on the reference note.
Algorythm is implemented in the note_freq_calculator.cpp .
For example, according to standard A4 = 440:
base note, base
= A4
base note frequency, f(base)
= 440
f(n) = f(base) * 2 ^ (delta / 12);
C C# D D# E F F# G G# A A# B
1 2 3 4 5 6 7 8 9 10 11 12
delta = n - base = (O * 12 + n) - (4 * 12 + 10);
O
- octave number;
n
- tone number in the octave;
4
- base octave number;
12
- number of tones in the octave;
10
- base tone number in its octave;
C4 - A4 = (4 * 12 + 1) - (4 * 12 + 10) = -9;
f(C4) = 440 * 2 ^ (-9 / 12) = 440 * 2 ^ -0.75 = 440 * 0.5946 = 461.63;
B4 - A4 = (4 * 12 + 12) - (4 * 12 + 10) = 2;
f(B4) = 440 * 2 ^ (2 / 12) = 493.88;
A3 - A4 = (3 * 12 + 10) - (3 * 12 + 10) = -12;
f(A3) = 440 * 2 ^ (-12 / 12) = 220;
A5 - A4 = (5 * 12 + 10) - (3 * 12 + 10) = 12;
f(A3) = 440 * 2 ^ (12 / 12) = 880;
Melody is defined in the jingle_bells.h .
How does this melody sound on the piano:
E5 C#6 B5 A5 E5
E5 C#6 B5 A5 F#5
F#5 D6 C#6 B5 G#5
E6 F#6 E6 D6 B5 C#6
E5 C#6 B5 A5 E5
E5 C#6 B5 A5 F#5
F#5 D6 C#6 B5 E6 E6 E6
F#6 E6 D6 B5 A5
C#6 C#6 C#6
C#6 C#6 C#6
C#6 E6 A5 B5 C#6
D6 D6 D6 D6
D6 C#6 C#6
C#6 B5 B5 C#6 B5 E6
C#6 C#6 C#6
C#6 C#6 C#6
C#6 E6 A5 B5 C#6
D6 D6 D6
D6 C#6 C#6 C#6
E6 E6 D6 B5 A5
How does this melody looks on the Christmas Lights:
We wish you a Merry Christmas
Melody is defined in the merry_christmas.h .
C5 F5 F5 G5 F5 E5 D5 D5 D5
G5 G5 A5 G5 F5 E5 E5 E5
A5 A5 B5 A5 G5 F5 D5 C5 C5 D5 G5 E5 F5
C5 F5 F5 F5 E5
E5 F5 E5 D5 C5
G5 A5 G5 G5 F5 F5 C6 C5
C5 C5 D5 G5 E5 F5
How does this melody looks on the Christmas Lights:
Melody is defined in the elise.h .
E5
D#5
E5
D#5
0.5
0.5
0.5
0.5
E5
B4
D5
C5
A4
0.5
0.5
0.5
0.5
1.5
C4
E4
A4
B4
0.5
0.5
0.5
1.5
E4
G#4
B4
C5
0.5
0.5
0.5
1.5
E5
D#5
E5
D#5
0.5
0.5
0.5
0.5
E5
B4
D5
C5
A4
0.5
0.5
0.5
0.5
1.5
C4
E4
A4
B4
0.5
0.5
0.5
1.5
E4
C5
B4
A4
0.5
0.5
0.5
2
Notes: silent_night.h