Button Press Detection Issues

Hello! Below I posted some c code. I am currently using WinAVR and writing my Arduino code in the C programming language. I have a question that is probably a basic answer that I seem to be missing or not understanding. Basically if I deploy my code with one line it works but with an alternative line it doesnt work and it would seem both should work so I am curious as to what is wrong with this line of code for detecting a low on the input:

!(PORTB & (1<<PINB0))

This line however for detecting a low on the input works fine:


Below is my complete project:

#include <avr/io.h>
#include <avr/delay.h>

int main(void)
	DDRB = (1<<PINB5);
	PORTB = (1<<PINB0);
			PORTB |= (1<<PINB5);
			PORTB &= ~(1<<PINB5);

You’re mixing your "PORT"s with your "PIN"s pretty liberally here :slight_smile:

PORTB = output register
PINB = input register

So you want to check “!(PINB & (1<<PB0))”

To see the state of a pin you look in one of the PINx registers, not the PORTx registers.
The PORTx register is basically the value of the last state written to that pin, nothing to do
with the actual physical state of the pin. The PINx registers are pseudo-registers that
reflect the actual voltage on the pins.

Ok, that makes sense. Thank you both for explaining this to me very much!