Error in my AVR BUTTON code
You code is fine regarding the Data Direction Registers, you set the GPIO directions correctly.

If you set a bit (to 1) in the Data Direction Resgister, the corresponding pin will be output. If you clear it (to 0) it will be an input.

Your problem seems to be with your hardware, lack of knowledge of your hardware. A push button can be active low or active high.




In the first case, the button is active low. So when you push it the pin will read 0, as it will be connected to GND. When it is not pushed it will read 1 as the pin is pulled up to VCC by a resistor.

In this case your code won't work because it checks if the pin is 1, which won't be if the button is pushed.

if(PIND &amp; (1&lt;&lt;BUTTON) &amp;&amp; flag == 0)


Probably this is your button's configuration so you should check if it is 0.

if(!(PIND &amp; (1&lt;&lt;BUTTON)) &amp;&amp; flag == 0)  // note the '!' operator

In the second case, the button is active high. When pushed the pin will read 1, as it will be tied to VCC. If unpushed it will read 0 because of the pull-down resistor to GND. In this case your code should work.