I have a very simple test sketch in which I\'m trying to set a pin to HIGH and then read its state with digitalRead. Here is my sketch.
<
Didn't like any of the previous answers, because they would not be cross-arduino-platform compatible. You need to access through the pin reference tables. The following expression does the trick.
bool value = (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));
Let me break that down for better understanding
digitalPinToPort(pin) look up you the gpio bank [port] that the pin is assigned to on your selected hardware
portOutputRegister(...) gives you a pointer to the port containing the value you are looking for. * dereferences the pointer and gives the full value of all 8 pins assigned to that port. It is not yet uniquely useful, but the bit you are looking for is in there.
&digitalPinToBitMask(pin) selects only the bit you are interested in for the pin, all other bits will be zero.
0!= tests to see if the resulting expression is zero, or something else. If it is zero, then your output is zero on that pin. Otherwise the output is one.