Serial Musical Organ 1.0
Buzzer controller and musical organ
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
333#include "includes/CPU.h"
334#include <avr/io.h>
335#include <util/delay.h>
336#include "includes/pinDefines.h"
337#include "includes/scale16.h"
338#include "includes/organ.h"
340#include "includes/USART.h"
341
357#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
358
372
406const uint16_t notes[] = {G4, Gx4, A4, Ax4, B4, C5, Cx5,
407 D5, Dx5, E5, F5, Fx5, G5, Gx5,
408 A5, Ax5, B5, C6};
409
455int main(void)
456{
457 /* Initialize buzzer pin as output */
458 BUZZER_DDR |= (1 << BUZZER_PIN);
459
460 /* Initialize USART serial communication (9600 baud, 8N1) */
461 initUSART();
462
463 /* Display welcome message and control instructions */
464 printString("---Serial Organ---\r\n");
465 printString("Controls: a-s-d-f-g-h-j-k-l-;-' for white keys\r\n");
466 printString(" w-e-t-y-i-o-p for black keys\r\n");
467 printString(" 1 to play Twinkle Twinkle Little Star\r\n");
468 printString(" 2 to play Imperial March from Star Wars\r\n");
469 printString(" '[' = short note (250ms), ']' = long note (500ms)\r\n");
470 printString("Ready!\r\n");
471
472 char receivedChar;
473 uint8_t isNote;
474 uint8_t i;
490 const uint8_t keys[] = {'a', 'w', 's', 'e', 'd', 'f', 't',
491 'g', 'y', 'h', 'j', 'i', 'k', 'o',
492 'l', 'p', ';', '\''};
493
494 /* Main program loop - runs forever */
495 while (1)
496 {
501 receivedChar = receiveByte();
502
508 transmitByte(receivedChar);
509
515 isNote = 0;
516 for (i = 0; i < ARRAY_SIZE(keys); i++)
517 {
518 if (receivedChar == keys[i])
519 {
520 /* Play the corresponding note with current duration */
522 isNote = 1;
523 break; /* Exit loop once match is found */
524 }
525 }
526
532 if (!isNote)
533 {
534 if (receivedChar == '[')
535 {
536 /* Set short note duration mode (250ms, faster tempo) */
538 printString("\r\nShort mode (250ms)\r\n");
539 }
540 else if (receivedChar == ']')
541 {
542 /* Set long note duration mode (500ms, slower tempo) */
544 printString("\r\nLong mode (500ms)\r\n");
545 }
546 else if (receivedChar == '1')
547 {
548 /* Play Twinkle Twinkle Little Star (blocks for ~10-15 seconds) */
550 printString("\r\nPlaying Twinkle Twinkle Little Star\r\n");
551 }
552 else if (receivedChar == '2')
553 {
554 /* Play Imperial March from Star Wars (blocks for ~30 seconds) */
555 printString("\r\nPlaying Imperial March from Star Wars\r\n");
557 printString("\r\nImperial March finished!\r\n");
558 }
559 else
560 {
567 if (receivedChar != '\r' && receivedChar != '\n')
568 {
569 /* Play silence for unassigned keys */
571 }
572 /* CR and LF are silently ignored (no rest, no echo effect) */
573 }
574 }
575
582 _delay_ms(10);
583 }
584
585 /* Never reached in embedded systems - infinite loop above */
586 return 0;
587}
588
CPU configuration and definitions for ATmega168.
USART serial communication interface.
void initUSART(void)
Initializes the USART peripheral for serial communication.
Definition USART.c:34
uint8_t receiveByte(void)
Receives a single byte from USART.
Definition USART.c:88
void printString(const char String[])
Sends a null-terminated string over USART.
void transmitByte(uint8_t data)
Transmits a single byte over USART.
Definition USART.c:71
#define Gx5
Definition scale16.h:84
#define Cx5
Definition scale16.h:77
#define B4
Definition scale16.h:61
#define G5
Definition scale16.h:83
#define D5
Definition scale16.h:78
#define F5
Definition scale16.h:81
#define C5
Definition scale16.h:76
#define C6
Definition scale16.h:87
#define Gx4
Definition scale16.h:70
#define A5
Definition scale16.h:73
#define G4
Definition scale16.h:69
#define Ax5
Definition scale16.h:74
#define A4
Definition scale16.h:59
#define Dx5
Definition scale16.h:79
#define Fx5
Definition scale16.h:82
#define E5
Definition scale16.h:80
#define B5
Definition scale16.h:75
#define H
Definition scale16.h:91
#define Q
Definition scale16.h:90
#define Ax4
Definition scale16.h:60
#define BUZZER_DDR
Data Direction Register for BUZZER pin.
Definition pinDefines.h:31
#define BUZZER_PIN
BUZZER pin number within its port.
Definition pinDefines.h:42
#define ARRAY_SIZE(x)
Calculate the number of elements in a static array at compile time.
Definition main.c:357
uint16_t currentNoteLength
Current note duration in milliseconds.
Definition main.c:371
int main(void)
Main program entry point.
Definition main.c:455
const uint16_t notes[]
Array of musical notes mapped to keyboard keys.
Definition main.c:406
Organ tone generator interface.
void playNote(uint16_t half_us, uint16_t duration_ms)
Play a musical tone.
Definition organ.c:66
void rest(uint16_t duration_ms)
Definition organ.c:92
Hardware pin mapping for BUZZER/buzzer output.
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.
Musical note frequency lookup table (16-bit timer values)