/arduino-play-melody

Simple Arduino project to play a melody using notes and blink an LED to the tune of the melody.

Primary LanguageCMIT LicenseMIT

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.

Algorythm:

  1. Turn on the LED
  2. For each note in the melody do:
    1. Get the note name
    2. Get beat for the note
    3. Calculate note playing duration = number of beats multiply by Melody.tempo (duration of one beat, defined for each melody)
    4. Get note frequency by its name from the music_structures.h
    5. Play the tone async, playNote()
    6. Blink by the LED, blink():
      • Turn off (or fade down),
      • Delay milis of BLINK_DURATION
      • Turn on
    7. Delay until playing note is finished
    8. 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.

Memory optimization

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(&notes, melody.notes, melody.length * sizeof(Note));

Define new melody

  1. create h-file for the melody
  2. include it into arduino-play-melody.ino
  3. 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.
  1. 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
  1. Add melody into the MELODIES array in the arduino-play-melody.ino
const Melody *MELODIES[] = {
  ...,
  &NEW_MELODY,
};

Notes Scale

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:

Notes calculation

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;

External load connection

Jingle Bells

Melody is defined in the jingle_bells.h.

How does this melody sound on the piano:

Notes:

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.

Notes:

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:

Für Elise

Melody is defined in the elise.h.

Notes:

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
E4
0.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

Silent Night

Notes: silent_night.h

C4 D4 C4 A3
1.5 0.5 1 3
C4 D4 C4 A3
1.5 0.5 1 3
G4 G4 E4
2 1 3
F4 F4 C4
2 1 3
D4 D4 F4
2 1 1.5
E4 D4 C4
0.5 1 1.5
D4 C4 A3
0.5 1 3
D4 D4 F4
2 1 1.5
E4 D4 C4
0.5 1 1.5
D4 C4 A3
0.5 1 3
G4 G4 A#4
2 1 1.5
G4 E4 F4
0.5 1 3
A4
3
F4 C4 A3 C4
1 1 1 1.5
A#3 G3 F3
0.5 1 3
A3
3

Links

Author

License