Adding New Songs
Step 1: Add function declaration in pre_defined_songs.h
Step 2: Implement the song in pre_defined_songs.c
void play_my_song(void)
{
const uint16_t durations[] = {
Q,
Q,
Q,
Q,
Q,
Q,
Q,
H};
for (uint8_t i = 0; i < 8; i++)
}
const uint16_t notes[]
Array of musical notes mapped to keyboard keys.
void playNote(uint16_t half_us, uint16_t duration_ms)
Play a musical tone.
Step 3: Add command in main.c
else if (receivedChar == '3')
{
play_my_song();
}
void printString(const char String[])
Sends a null-terminated string over USART.
Note Frequency Calculation
Timer values are calculated as:
OCR =
F_CPU / (2 × frequency × prescaler)
Example
for C4 (261.63 Hz) with
F_CPU=16MHz, prescaler=1:
OCR = 16000000 / (2 × 261.63 × 1) = 30578
#define F_CPU
CPU clock frequency in Hertz.
Adding New Notes
Add to scale16.h:
#define NewNote OCR_VALUE
Performance Improvements
Implement Non-blocking Playback
ISR(TIMER1_COMPA_vect) {
}
Add Serial Buffer Queue
#define RX_BUFFER_SIZE 32
volatile char rx_buffer[RX_BUFFER_SIZE];
Troubleshooting
| Problem | Solution |
| No sound | Check buzzer connection and resistor |
| Garbled serial | Verify baud rate (9600) and F_CPU |
| Wrong notes | Check F_CPU in CPU.h |
| No serial echo | Verify RX/TX cross connection |