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
    PORTD=0x00;
    
    DDRB=0xFF;   //PORTB as output
    PORTB=0x00;
    
    
    while(1){
        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: https://stackoverflow.com/questions/64418619/how-to-check-if-a-certain-pin-on-portx-is-pressed

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: