ATmega1284 behaving erratically
First off, your statement


  I've also tried change the value of F_CPU several times


makes me suspect you are misunderstanding F_CPU. To ensure sane operation, F_CPU must be set equal to your MCU clock frequency, i.e. you can't say #define F_CPU 6000000UL to make the MCU operate at 6MHz, you need a 6MHz clock source. Are you sure that your board uses a 6MHz clock? It's not the most common clock frequency (unless you're a USB keyboard, but I digress).

To isolate the issue, reduce your code a bit. A simple blinking program with no input would be appropriate. Also, there is no need to change the port direction constantly when blinking an LED, you can simply set it to output and leave it (in fact, this may be causing some weirdness). Oh, and #include "avr/io.h" doesn't seem quite right, it should be in your standard library headers. I'd suggest the following code:

#define F_CPU 6000000UL  // 6 MHz (is it really 6MHz)

#include &lt;avr/io.h&gt; // AVR IO library
#include &lt;util/delay.h&gt; // AVR busy wait delay loops

int main(void) {
    DDRD |= (1 &lt;&lt; PIND6); // set D6 (LED) to output
    while (1) {
        PORTD ^= (1 &lt;&lt; PIND6); // toggle D6 (LED)
        _delay_ms(250); // wait 250ms
    }
    return 0; // never reached
}


This is just about the simplest debugging "blink" code you can write, short of assembly. Try to get this working correctly, and then build on it from there.