Fastest wait until pin changes from LOW to HIGH

Hi,

Can somebody tell me if this is the right way to FASTLY let your code halt until a certain pin (here pin D5) changes from LOW to HIGH ?

while (PIND & ( 1 << 5 )==false){
}

Kind regards,

Bart

Its kind of strange (or I just don't understand how to do it), but I tried the following

In my setup code I set

pinMode(5, INPUT); // set pin to input
digitalWrite(5, HIGH); // turn on pullup resistors

Then in my loop I have

while (PIND & ( 1 << 5 )){
}
blablabla....

I thought that would make my code halt until I connect port D pin 5 (digital out 5 on arduino) to ground and only when I connect the pin to ground it will execute blablabla... ?

Or am I totally wrong?

Mea culpa

I was checking the wrong bit

it had to be

while (PIND & 0b00100000 ){
}

Two code snaps should be identical, could be compliler optimize "while" out, as there is nothing usefull in it. Checck in assembly, if there is any difference.

Magician:
Two code snaps should be identical, could be compliler optimize "while" out, as there is nothing usefull in it. Checck in assembly, if there is any difference.

If PIND is properly declared volatile, the compiler should not optimize the while out. If PIND is not declared volatile, then yes the compiler likely will optimize away the while.

then yes the compiler likely will optimize away the while.

I find it doesn't, I use that construct all the time.