Serial Musical Organ 1.0
Buzzer controller and musical organ
Loading...
Searching...
No Matches
Functions
pre_defined_songs.h File Reference

Pre-defined songs for musical organ. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void play_twinkle_little_star ()
 Plays "Twinkle Twinkle Little Star" melody.
 
void play_imperial_march ()
 Plays "Imperial March" (Darth Vader's Theme) from Star Wars.
 

Detailed Description

Pre-defined songs for musical organ.

Author
Roybel Carbonell Camejo
Date
2026-04-29
Version
1.1

Declares functions to play pre-programmed songs via USART commands. Songs are triggered by numeric keys (e.g., '1' for Twinkle Star, '2' for Imperial March).

Available Songs

Key Function Song Title Duration
'1' play_twinkle_little_star() Twinkle Twinkle Little Star ~10-15 sec
'2' play_imperial_march() Imperial March (Star Wars) ~30 sec

Usage

// In main.c serial command handling
switch(receivedChar) {
case '1':
printString("\r\nPlaying Twinkle Twinkle Little Star\r\n");
break;
case '2':
printString("\r\nPlaying Imperial March\r\n");
break;
}
void printString(const char String[])
Sends a null-terminated string over USART.
Pre-defined songs for musical organ.
void play_imperial_march()
Plays "Imperial March" (Darth Vader's Theme) from Star Wars.
void play_twinkle_little_star()
Plays "Twinkle Twinkle Little Star" melody.

Definition in file pre_defined_songs.h.

Function Documentation

◆ play_imperial_march()

void play_imperial_march ( )

Plays "Imperial March" (Darth Vader's Theme) from Star Wars.

Blocking function that plays the iconic Imperial March theme composed by John Williams for Star Wars: The Empire Strikes Back (1980). Associated with Darth Vader and the Galactic Empire.

Composition Details

  • Composer: John Williams
  • Movie: Star Wars: Episode V - The Empire Strikes Back (1980)
  • Key: E minor (Em)
  • Time Signature: 4/4 (march tempo)
  • Original Tempo: ~104 BPM
  • Duration: ~30 seconds (complete main theme)

Musical Structure

The piece follows this structure:

* Intro (4 bars) | Main Theme A (4 bars) | Main Theme B (4 bars) |
* Bridge (4 bars) | Main Theme A (4 bars) | Build-up (3 bars) |
* Climax (5 bars) | Resolution (4 bars) | Coda (6 bars)
* 

Melody (C major scale)

* G3 - G3 - G3 - (G3 D#3) G3 | G3 G3 G3 D#3 G3 |
* C4 C4 C4 G3 C4 | A#3 A#3 A#3 F3 A#3 |
* [Pattern repeats with variations]
* 

Trivia & Cultural Impact

  • One of the most recognizable film themes in cinema history
  • Often used in popular culture to denote villainy or impending doom
  • Has been performed by numerous orchestras worldwide
  • Frequently appears in video games, memes, and parodies

Usage

// Play the Imperial March
// Trigger with '2' in main.c
if (receivedChar == '2') {
printString("\r\n*** Imperial March ***\r\n");
}
Note
Blocking function - no serial input accepted while playing
Triggered by pressing '2' in the main program (recommended)
The theme uses notes from G3 to F4 (range: G3, G#3, A#3, C4, D4, D#4, E4, F4)
Warning
This is a blocking function - the microcontroller will not respond to any serial commands until the song finishes (~30 seconds)
Note durations are affected by global currentNoteLength variable
For authentic tempo, ensure currentNoteLength matches intended duration
  • Use Q (250ms) for faster, more dramatic tempo
  • Use H (500ms) for slower, more ominous tempo
Bug:
Current blocking implementation prevents interrupt during playback
Todo:

Implement non-blocking version using timer interrupts

Add ability to stop playback mid-song (emergency stop)

See also
play_twinkle_little_star() Another pre-defined song
playNote() Core function for note playback
scale16.h Contains note definitions (G3, C4, F4, etc.)
Examples
/home/luka/WORK/Programming/ARDUINO/AVR-Square-Wave-Organ/src/main.c, and /home/luka/WORK/Programming/ARDUINO/AVR-Square-Wave-Organ/src/pre_defined_songs.c.

Referenced by main().

Here is the caller graph for this function:

◆ play_twinkle_little_star()

void play_twinkle_little_star ( )

Plays "Twinkle Twinkle Little Star" melody.

Blocking function that plays the classic children's song "Twinkle Twinkle Little Star" through the buzzer.

Melody (C major scale)

* CC GG AA G - FF EE DD C - GG FF EE D - GG FF EE D -
* CC GG AA G - FF EE DD C
* 

Song Information

  • Composer: Traditional/French folk melody (1761)
  • Lyrics by: Jane Taylor (1806)
  • Key: C major
  • Time Signature: 4/4
  • Tempo: Andante (~120 BPM with Q=250ms)
  • Duration: ~10-15 seconds (depends on note duration mode)
Note
Blocking function - no serial input accepted while playing
Triggered by pressing '1' in the main program
Warning
Note durations are affected by global currentNoteLength variable
If currentNoteLength = H (500ms), song plays slower If currentNoteLength = Q (250ms), song plays faster
See also
playNote() from organ.h for individual note playback
currentNoteLength in main.c for duration control
play_imperial_march() Another pre-defined song

Implements the complete Twinkle Twinkle Little Star song using fixed note durations (Q=250ms for eighth notes, H=500ms for quarter notes). The song has 42 notes organized in 6 phrases of 7 notes each.

Song Structure (42 notes, 6 phrases)

* Phrase 1: C4  C4  G4  G4  A4  A4  G4  (Twinkle twinkle little star)
* Phrase 2: F4  F4  E4  E4  D4  D4  C4  (How I wonder what you are)
* Phrase 3: G4  G4  F4  F4  E4  E4  D4  (Up above the world so high)
* Phrase 4: G4  G4  F4  F4  E4  E4  D4  (Like a diamond in the sky)
* Phrase 5: C4  C4  G4  G4  A4  A4  G4  (Twinkle twinkle little star)
* Phrase 6: F4  F4  E4  E4  D4  D4  C4  (How I wonder what you are)
* 

Rhythm Pattern

  • First 6 notes of each phrase: Q (250ms) - eighth notes
  • Last note of each phrase: H (500ms) - quarter note (held longer)
  • Creates a "swing" feel typical of children's songs

Note Frequency Analysis

Note Frequency (Hz) Period (μs) Timer Value
C4 261.63 3822 1911
D4 293.66 3405 1702
E4 329.63 3033 1516
F4 349.23 2863 1431
G4 392.00 2551 1275
A4 440.00 2272 1136

@complexity O(n) where n = 42 notes @performance Takes ~10-15 seconds to complete (Q=250ms, H=500ms)

Bug:
None known for this implementation
See also
play_imperial_march() Another pre-defined song
playNote() Core function that generates tones
Note
This function is blocking - no other code executes during playback
Warning
Do not modify the arrays while the song is playing (not possible anyway)

Array of notes for Twinkle Twinkle Little Star

Contains 42 note frequency values in sequence. Values correspond to C4, G4, A4, F4, E4, D4 from scale16.h.

Note
Stored in flash memory (const qualifier in global scope)
See also
scale16.h for note definitions (C4, G4, A4, F4, E4, D4)

Array of durations for Twinkle Twinkle Little Star

Each duration corresponds to the note at the same index in estrellita_notas[]. Uses Q (250ms) for short notes and H (500ms) for notes that should be held longer.

Note
Pattern: 6 short notes + 1 long note repeated 6 times
See also
scale16.h for duration definitions (Q=250, H=500)

Play each note in sequence

Iterates through all 42 notes and plays each with its corresponding duration. The loop is blocking.

Invariant
i always ranges from 0 to 41 @ensures All 42 notes have been played
Examples
/home/luka/WORK/Programming/ARDUINO/AVR-Square-Wave-Organ/src/main.c, and /home/luka/WORK/Programming/ARDUINO/AVR-Square-Wave-Organ/src/pre_defined_songs.c.

Definition at line 82 of file pre_defined_songs.c.

References A4, C4, D4, E4, F4, G4, H, playNote(), and Q.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: