When your program set state on an OUTPUT pin, it can be sometimes useful to know the state of this OUTPUT for some reasons (sending the output state over internet, etc…)
So here is the trick to know the output state of a pin:
//exemple for the state of the pin 3 Serial.println(bitRead(PORTD,3));
Actually the micro-controller keeps in some register the value set for output pin.
PORTB and PORTD are registers for digital pin from 8 to 13 and digital pin from 0 to 7.
A list of interesting register
- PORTD maps to Arduino digital pins 0 to 7
DDRD – The Port D Data Direction Register – read/writePORTD – The Port D Data Register – read/writePIND – The Port D Input Pins Register – read only
- PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable
DDRB – The Port B Data Direction Register – read/writePORTB – The Port B Data Register – read/writePINB – The Port B Input Pins Register – read only
- PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini
DDRC – The Port C Data Direction Register – read/writePORTC – The Port C Data Register – read/writePINC – The Port C Input Pins Register – read only