Interesting question, I've never thought about that.
First
if(randomPin == HIGH)
will not work, it's about the same as "if (5 == 1)" which always will be false.
And we're talking about OUTPUT-pins here?
My short guess is it will probably work with something like
digitalWrite(conditionalPin, digitalRead(randomPin))
My longer spekulation/explanation is this.
digitalWrite will set a bit in the output data register (ODR) (by writing a bit to bit-reset or bit-set-reset register) and digitalRead will return HIGH if the bit in the input data register (IDR) is set, LOW if it's unset.
So you want to read the ODR-register, but that's no way to do that without going to lower levels.
But what happens with IDR for an output pin? The reference says (8.1.8 Output configuration)
● A read access to the Input Data Register gets the I/O state in open drain mode
● A read access to the Output Data register gets the last written value in Push-Pull mode
I'm not sure how to interpret that exactly.
I think it's like this
- If the pin is set to OUTPUT (then it will be set to push-pull mode) or OUTPUT_PUSH_PULL, the IDR and ODR will contain the same information. If that's the case you can use digitalRead() to get the value of the last digitalWrite().
- If the pin is configured as OUTPUT_OPEN_DRAIN, the relevant bit in the registers will be same if a pin is low. If you set the pin high, ODR will be high but IDR will be high if you have external pullup, low if you have external pulldown or have a random value if it's floating. But if you choose open-drain I guess you have some kind of external pullup. In that case ODR and IDR will have the same value.
And
while(digitalRead(randomPin))
will work for input pins and, if I'm correct above, for OUTPUT pins too.
Everything untested. Time to sleep.