Hi avrdude,

The short answer is, this is essentially what digitalRead() is doing already. But I'm guessing you want lower overhead/higher performance.

Another thing you can try is using a library like digitalWriteFast or this one called digitalIOPerformance that I put together last year (not Freetronics-related):

https://github.com/projectgus/digitalIOPerformance
However, to answer your actual question - you need to use bitwise operators to see the actual value.

On Eleven/Uno, we can see that digital pin 3 is PD3 (Port D bit 3.) There are lots of ways to figure this out, I looked at the Eleven PDF

https://github.com/freetronics/Eleven/b ... evenR3.pdf but there are other diagrams out there.

So if we read PIND:

We get all 8 input values from port D as a byte. We want to look to see whether bit 3 is set. Bit 3 is value 8 (We count bits from zero and they double each time - so values are 1,2,4,8.) You can also show this as "1 left shifted 3 times", or 1 << 3 in C, which is convenient to read because that "3" matches exactly to the 3 in "PD3".

Some explanation can be found here.

So we can do a

bitwise AND to expose just that value:

or

Suppose the whole PIND value (all 8 pins) looks like this in binary

The bit we want is bit 3, the value of 8:

(Just to confuse you here we're counting bits from the right hand end here not the left hand end, because the right-most digit in a number has the lowest value! So rightmost is 1, next left is 2, next left is 4, next left is 8.)

So if we bitwise AND the pin value with the bit for the pin we can are about, we get this in binary

Which equals 00001000 (8) if the pin is high and the bit is set as shown above, or it would equal 00000000 if the pin was low and the bit was not set. Generally you can test for x != 0 to determine if the pin was high or not.

Hopefully that makes some sense. If you're looking for more insight into how to understand this, "bitwise operations" are the thing to read up on.

- Angus