How to check if a certain PIN on PORTx is pressed

So I have an assignment that wants me to perform certain code when a button on PD2 is presed.

The problem i'am having right now is i don't really know what to check for neither do i understand the underlying logic.

So this is the code i've come up with thus far.

 DDRD=0x00;   //PORTD pin 0 as input
    DDRB=0xFF;   //PORTB as output
        if (PIND & (1<<PD2)==1) // check if PD2 is pressed on PIND2
            // modify bits on PORTB here

I'm using Atmega328 and running this on AtmelStudio 7

Read more here:

Content Attribution

This content was originally published by azemi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: