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

Implementation of pre-defined songs for musical organ. More...

#include "includes/CPU.h"
#include <avr/io.h>
#include <util/delay.h>
#include "includes/scale16.h"
#include "includes/organ.h"
#include "includes/pre_defined_songs.h"
Include dependency graph for pre_defined_songs.c:

Go to the source code of this file.

Macros

#define _num_notes(x)   sizeof(x) / sizeof(x[0])
 

Functions

void play_twinkle_little_star ()
 Plays "Twinkle Twinkle Little Star" melody.
 

Detailed Description

Implementation of pre-defined songs for musical organ.

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

This file contains the actual implementations of all pre-programmed songs. Each song is defined as arrays of notes (frequencies) and durations (milliseconds), then played sequentially using playNote().

Implementation Details

Memory Usage Analysis

Note
ATmega168 has 16KB flash, so memory usage is minimal
Warning
Modifying song arrays increases flash memory consumption
See also
pre_defined_songs.h Header file with function declarations
scale16.h Note definitions (C4, G4, Q, H, etc.)
organ.h playNote() function implementation

Definition in file pre_defined_songs.c.

Macro Definition Documentation

◆ _num_notes

#define _num_notes (   x)    sizeof(x) / sizeof(x[0])

Function Documentation

◆ play_twinkle_little_star()

void play_twinkle_little_star ( )

Plays "Twinkle Twinkle Little Star" melody.

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

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: