LED Piano: A3BU PWM Code

The A3BU code interacts with TeraTerm to receive a serial input from the user. Once the user types in the key word of a song, the A3BU will be notified and will transmit a PWM signal from the SCL on header J1 to the Arduino. Once the Arduino receives its PWM signal, a pre-programmed song will play on our instrument.
#define USART_SERIAL_EXAMPLE &USARTC0
#define USART_SERIAL_EXAMPLE_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH USART_CHSIZE_8BIT_gc
#define USART_SERIAL_PARITY USART_PMODE_DISABLED_gc
#define USART_SERIAL_STOP_BIT false

#include <conf_board.h>
#include <string.h>
#include <board.h>
#include <sysclk.h>
#include <st7565r.h>
#include <asf.h>

int main(void)
{
struct pwm_config mypwm[1];
board_init();
sysclk_init();
//initializes the graphics mono
gfx_mono_init();
//prepares the LCD back light to receive an input
PORTE_DIR = 0x10;
//turns the LCD back light to ON
PORTE_OUT = 0x10;
//prints the designated string value
gfx_mono_draw_string("Enter the song you wish to play!",18,10,&sysfont);

pwm_init(&mypwm[0], PWM_TCC0, PWM_CH_B, 500);//SCL on J1

// Set each of the 4 PWM channel duty cycles independently, and start them going pwm_start(&mypwm[0], 10);
pwm_start(&mypwm[0],0);

while (true) 
{
uint8_t tx_buf[] = "\n\rHello\n\rEnter song:";
uint8_t tx_length = 22;
uint8_t received_byte;
uint8_t i;
uint8_t j = 0;
uint8_t start_line_address = 0;
uint8_t x = 0;
char *song1 = "star";
char message[30];
char letter;

/* Initialize the board.
* The board-specific conf_board.h file contains the configuration of
* the board initialization.
*/
gfx_mono_init();
st7565r_init();

st7565r_set_column_address(0);
st7565r_set_page_address(0);

// USART options.
static usart_rs232_options_t USART_SERIAL_OPTIONS = {
.baudrate = USART_SERIAL_EXAMPLE_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};

// Initialize usart driver in RS232 mode
usart_init_rs232(USART_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS);

// Send "message header"
for (i = 0; i < tx_length; i++) 
{
usart_putchar(USART_SERIAL_EXAMPLE, tx_buf[i]);
}
// Get and echo a character forever, specific '\r' processing.
while (true) 
{
//received_byte = usart_getchar(USART_SERIAL_EXAMPLE);
PORTE_OUT = 0xff;
letter = usart_getchar(USART_SERIAL_EXAMPLE);
PORTE_OUT = 0x00;
//strcat(message, letter);
if(j < 30)
{
if(letter != '\r')
{
message[j] = letter;
j++;
}
}

if (letter == '\r') 
{
if(strcmp(message, song1) == 0)
{
gfx_mono_draw_string(" ", 0,0, &sysfont);
gfx_mono_draw_string("Ready to Play!", 44, 0, &sysfont);
pwm_start(&mypwm[0],80);
delay_ms(2000);
pwm_start(&mypwm[0],0);

}
else
{
for (i = 0; i < tx_length; i++) 
{
while(true)
{
PORTE_OUT = 0xff;
st7565r_set_display_start_line_address(start_line_address = start_line_address + 2);
for(i = 0; i < j; i++)
{
gfx_mono_draw_char(message[i], i*8, 0, &sysfont);
delay_ms(50);
}
/*for(i = 0; i < j; i++)
{ 
gfx_mono_draw_char(message[i], (x*8) + (i*8), 0, &sysfont);
}
delay_ms(200);
gfx_mono_draw_string(" ", 0,0, &sysfont);
x++;
if(((x*8) + (i*8)) >= 128)
{
x = 0;
}*/
}
usart_putchar(USART_SERIAL_EXAMPLE, tx_buf[i]);
}

}
} 
else
{
usart_putchar(USART_SERIAL_EXAMPLE, letter);
for(i = 0; i < j; i++)
{
gfx_mono_draw_char(message[i], i*8, 0, &sysfont);
}
}
}
}
}