The objection is that we have an OUTPUT pin. It is ONLY and ALWAYS an OUTPUT pin. It should be under the code's constant and supreme control.
THIS is the exact point you are being obtuse on. You want to shout "BAD PROGRAMMING!" so loud, you are not looking at the bigger picture. In fact, you are suggesting a potentially bad programming practice at the same time.
You SHOULD use a proper variable to track each parameter of what the code is dealing with.
THERE ALREADY IS ONE. Every PORT has an I/O Register! When you issue a digitalRead(), the PINx register is what is read. In the case of INPUT pins, this value will depend on the last value clocked into the register from the physical pin. In the case of OUTPUT pins, the value is whatever you last wrote to it.
What if it is accidentally or temporarily shorted to ground, or to +5V? (As from plugging in a connector, etc.)
Since you are not reading the STATE of the PIN, but the register which determines it, it does not matter what the Pin is connected to.
This behavior is no different than if you have a variable tracking the state of the pin. Except that you are using a wasted variable to track something already built in the Micro-controller's hardware. Your opinion is that this is a Bad programming practice. Sorry, but I and most of the RealWorld(TM) will disagree with you.
From ATmel's Own Documentation:
13.2.4 Reading the Pin Value
"Independent of the setting of Data Direction bit DDxn, the port pin can be read through the PINxn Register bit.."
I point this out because the documentation clearly refers to the PINx Register, which is also illustrated below.
From wiring_digital.c:
int digitalRead(uint8_t pin)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
if (port == NOT_A_PIN) return LOW;
// If the pin that support PWM output, we need to turn it off
// before getting a digital reading.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
if (*portInputRegister(port) & bit) return HIGH; // CMIYC is referring to this line below.
return LOW;
}
Now before you say "But there! See! It says 'portInputRegister'" you have to go and look to see what that is defined as, which is:
in pins.arduino.h:
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
in pins_arduino.c:
const uint16_t PROGMEM port_to_input_PGM[] = {
NOT_A_PIN,
&PINA,
&PINB,
&PINC,
&PIND,
&PINE,
&PINF,
&PING,
&PINH,
NOT_A_PIN,
&PINJ,
&PINK,
&PINL,
};
The PINx register keeps track of the value of the PIN, regardless of its state. When you issue a digitalRead(), the register PINx is read and its value returned. Perhaps the Arduino language would benefit from pinRegisterRead() or something similar.
However, as I pointed out in my previous examples the Arduino language does not always have the most logical syntax for all aspects of the hardware's features. (e.g. enabling the Pull-Up resistors.)