|
Serial Musical Organ 1.0
Buzzer controller and musical organ
|
Main program for serial-controlled musical organ. More...
#include "includes/CPU.h"#include <avr/io.h>#include <util/delay.h>#include "includes/pinDefines.h"#include "includes/scale16.h"#include "includes/organ.h"#include "includes/pre_defined_songs.h"#include "includes/USART.h"
Go to the source code of this file.
Macros | |
| #define | ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| Calculate the number of elements in a static array at compile time. | |
Functions | |
| int | main (void) |
| Main program entry point. | |
Variables | |
| uint16_t | currentNoteLength = Q |
| Current note duration in milliseconds. | |
| const uint16_t | notes [] |
| Array of musical notes mapped to keyboard keys. | |
Main program for serial-controlled musical organ.
This program implements a serial-controlled musical organ that plays notes received via USART on an ATmega168 microcontroller. It maps keyboard keys to musical notes and allows dynamic control of note duration through serial commands. The program also supports playing pre-programmed songs.
| Key | Action | Mode |
|---|---|---|
| a, s, d, f, g, h, j, k, l, ;, ' | White keys (natural notes) | Musical |
| w, e, t, y, i, o, p | Black keys (sharp notes) | Musical |
| [ | Set short note duration (250ms) | Control |
| ] | Set long note duration (500ms) | Control |
| 1 | Play "Twinkle Twinkle Little Star" | Song |
| 2 | Play "Imperial March" (Star Wars) | Song |
The following keyboard keys correspond to musical notes:
White Keys (Natural Notes):
| Key | Note | Frequency | Octave |
|---|---|---|---|
| a | G4 | 392 Hz | 4th |
| s | A4 | 440 Hz | 4th |
| d | B4 | 494 Hz | 4th |
| f | C5 | 523 Hz | 5th |
| g | D5 | 587 Hz | 5th |
| h | E5 | 659 Hz | 5th |
| j | F5 | 698 Hz | 5th |
| k | G5 | 784 Hz | 5th |
| l | A5 | 880 Hz | 5th |
| ; | A#5 | 932 Hz | 5th |
| ' | C6 | 1047 Hz | 6th |
Black Keys (Sharp Notes):
| Key | Note | Frequency | Octave |
|---|---|---|---|
| w | G#4 | 415 Hz | 4th |
| e | A#4 | 466 Hz | 4th |
| t | C#5 | 554 Hz | 5th |
| y | D#5 | 622 Hz | 5th |
| i | F#5 | 740 Hz | 5th |
| o | G#5 | 831 Hz | 5th |
| p | B5 | 988 Hz | 5th |
receiveByte() blocks until a character is receivedplayNote() blocks for the duration of the note (250-500ms)play_twinkle_little_star() blocks for ~10-15 secondsplay_imperial_march() blocks for ~30 secondsThis program is open source. Feel free to modify and distribute.
Definition in file main.c.
| #define ARRAY_SIZE | ( | x | ) | (sizeof(x) / sizeof((x)[0])) |
Calculate the number of elements in a static array at compile time.
| x | The array to calculate size for |
| int main | ( | void | ) |
Main program entry point.
Initializes the buzzer pin and USART communication, then enters an infinite loop that processes incoming serial commands and plays corresponding musical notes or songs.
The main loop continuously:
receiveByte() (blocking call)transmitByte()@performance Performance Characteristics
< Received character from USART
< Flag indicating if character was a valid note
< Loop counter for key mapping
Keyboard key mapping array
Maps physical keyboard keys to musical notes. Must maintain same order as notes[] array.
The array includes:
Total of 18 keys covering 2 octaves (G4 to C6)
Step 1: Receive character from USART
Step 2: Echo character back to sender
Useful for debugging and providing visual feedback
Step 3: Check if character is a valid musical note
Iterates through keys array and plays corresponding note if a match is found.
Step 4: Process non-note commands
Handles control characters, song triggers, and ignores unwanted characters.
Handle all other characters
Ignore carriage return and line feed (common from terminal) For all other characters, play a rest (silence) to indicate the key was received but invalid.
Step 5: Small delay to prevent CPU saturation
Allows other tasks (if any) to run and prevents the main loop from consuming 100% CPU when idle. A value of 10ms is sufficient for smooth operation.
Definition at line 455 of file main.c.
References ARRAY_SIZE, BUZZER_DDR, BUZZER_PIN, currentNoteLength, H, initUSART(), notes, play_imperial_march(), play_twinkle_little_star(), playNote(), printString(), Q, receiveByte(), rest(), and transmitByte().

| uint16_t currentNoteLength = Q |
Current note duration in milliseconds.
Global variable that stores the currently selected note duration. Initialized to Q (quarter note, typically 250ms) for shorter notes. Modified by '[' (short mode) and ']' (long mode) commands.
Definition at line 371 of file main.c.
Referenced by main().
| const uint16_t notes[] |
Array of musical notes mapped to keyboard keys.
This array contains frequency values (timer compare values) for notes corresponding to keys in the same position in the keys array.
Index Mapping:
| Index | Key | Note | Frequency |
|---|---|---|---|
| 0 | a | G4 | 392 Hz |
| 1 | w | G#4 | 415 Hz |
| 2 | s | A4 | 440 Hz |
| 3 | e | A#4 | 466 Hz |
| 4 | d | B4 | 494 Hz |
| 5 | f | C5 | 523 Hz |
| 6 | t | C#5 | 554 Hz |
| 7 | g | D5 | 587 Hz |
| 8 | y | D#5 | 622 Hz |
| 9 | h | E5 | 659 Hz |
| 10 | j | F5 | 698 Hz |
| 11 | i | F#5 | 740 Hz |
| 12 | k | G5 | 784 Hz |
| 13 | o | G#5 | 831 Hz |
| 14 | l | A5 | 880 Hz |
| 15 | p | B5 | 988 Hz |
| 16 | ; | A#5 | 932 Hz |
| 17 | ' | C6 | 1047 Hz |
Definition at line 406 of file main.c.
Referenced by main().