PIND - Can't find the simple command

Hello
As usual, run out of flash memory >:(

Changed my PinMode statements to DDRB, DDRC, and DDRD statements. All fine there.

I have Googled for an hour, trying to find the correct syntax to read an individual pin using PIND.

Rather than using all my digitalRead statements, I wanted to read the state of the INDIVIDUAL pins using the port manipulation command.

edit… OK

if (PIND & (1<<PD5)) {Serial.println(“LOW”);} //LOW state

This detects low state on pin D5.

How do I change this statement to detect a high state?

Thanks

Never used it. This seems to do the trick though:

void setup()
{
Serial.begin(115200);
Serial.print("Pin 3: ");
Serial.println((PIND & 8)>1);  
}

void loop()
{
}

Are you sure that 1<<PD5 means LOW??

if (PIND & (1<<PD5)) {Serial.println("LOW");}  //LOW stateThat will print LOW when pin D5 (digital pin 5) is HIGH, probably not what you want.

To detect LOW, use

if (PIND & (1<<PD5) == 0) {Serial.println("LOW");}   //LOW state

or

if (!(PIND & (1<<PD5))) {Serial.println("LOW");}   //LOW state

Or:

Serial.println(bitRead(PIND,PD5) ? "HIGH" : "LOW");

jremington:
To detect LOW, use

if (PIND & (1<<PD5) == 0) {Serial.println("LOW");}   //LOW state

This will not work, == has higher precedence than &.
Needs mode ().

if ((PIND & (1<<PD5)) == 0) {Serial.println("LOW");}   //LOW state

jremington:
To detect LOW, use

if (PIND & (1<<PD5) == 0) {Serial.println("LOW");}   //LOW state

No, no, you clearly haven’t used C much! ‘&’ binds less tightly than ‘==’, you must use
parantheses:

if ((PIND & (1<<PD5)) == 0) {Serial.println("LOW");}   //LOW state

[/quote]

Its one of the many quirks (ie mistakes) in the design of C that ‘&’, ‘|’, ‘^’ have completely
dumb precedences.

:o :o :o
Eh...

OK, I will have to look into this further. I thought it was a simple command.
That is the trouble now with Google, you find all kinds of right and wrong answers

Eh...

OK, I will have to look into this further. I thought it was a simple command.
That is the trouble now with Google, you find all kinds of right and wrong answers

Steve-SFX:
Eh…

OK, I will have to look into this further. I thought it was a simple command.
That is the trouble now with Google, you find all kinds of right and wrong answers

if (PIND & (1<<PD5) == 0) {Serial.println(“LOW”);} //LOW state
This doesn’t appear to work.

See comment in reply #5. Extra set of parenthesis should do

if ((PIND & (1<<PD5)) == 0) {Serial.println("LOW");}   //LOW state[/i]

Rather have too many than too little :slight_smile:

if (!(PIND & (1<<PD5))) {Serial.println(“LOW”);} //LOW state

This works

if (PIND & (1<<PD5) == 0) {Serial.println(“LOW”);} //LOW state
This doesn’t

Sorry for my moment of inattention!

As you noticed, my second suggestion does work, and is the construction that I actually use:

if (!(PIND & (1<<PD5))) {Serial.println("LOW");}   //LOW state