PeterH:
I agree with PaulS that it's bad practice in general to rely on the actual value of constants, or any other aspect of an component that is not specified as part of the external interface, even though we know that in the specific case of Arduino the HIGH and LOW constants are unlikely ever to change. I mean, it would be daft to change them, wouldn't it?
Almost as daft as Arduino changing the name of their top level include files and making every previous sketch and library invalid. So yeah, obviously HIGH and LOW are never going to change.
I take your point, but... Changing name of top level includes is an environment organizational change, where as HIGH = TRUE and LOW = FALSE are industry standards for TTL and CMOS logic.
Even so, it's not a good idea to assume that digital inputs are active high - arguably, they're more likely to be active low since the Arduino has pull up resistors - it is better to show that relationship explicitly within the sketch.
But the relationship exists explicitly in the schematic. For example, see my attached schematic.
In1 and In4 are both active high, and In2 and In3 are both active low (indicated by the line over the label). In1 and ~In2 are both switches to ground, so I need to provide a pull up in the Arduino. In3 and In4 are both driven outputs from an IC, so I don't need any pull ups or pull downs. Thus, to create a sketch for this circuit that would light the LED on the corresponding output (i.e. In1 => Out1, In2 => Out2, etc) based on whether the signal is "active", I would probably use something like this (the comments are as I would write them for myself...):
const int In1 = 2;
const int In2 = 3;
const int In3 = 4;
const int In4 = 5;
const int Out1 = 8;
const int Out2 = 9;
const int Out3 = 10;
const int Out4 = 11;
void setup() {
// Input pins
pinMode(In1, INPUT_PULLUP); // Switch to ground, no external pull up
pinMode(In2, INPUT_PULLUP); // Switch to ground, no external pull up
pinMode(In3, INPUT); // IC driven signal
pinMode(In4, INPUT); // IC driven signal
// Output pins
pinMode(Out1, OUTPUT);
pinMode(Out2, OUTPUT);
pinMode(Out3, OUTPUT);
pinMode(Out4, OUTPUT);
}
void loop() {
if(digitalRead(In1)) { // Active high input
digitalWrite(Out1, HIGH);
}
else {
digitalWrite(Out1, LOW);
}
if(~digitalRead(In2)) { // Active low input
digitalWrite(Out2, HIGH);
}
else {
digitalWrite(Out2, LOW);
}
if(~digitalRead(In3)) { // Active low input
digitalWrite(Out3, HIGH);
}
else {
digitalWrite(Out3, LOW);
}
if(digitalRead(In4)) { // Active high input
digitalWrite(Out4, HIGH);
}
else {
digitalWrite(Out4, LOW);
}
}
Depending on my mood (and the complexity of the sketch) I may add a H or L to the end of variable names to remind myself of the pin's active state, and only have the comment about active high or low at the variable declaration part.
Again, the method you seem to be promoting (i.e. using a comparison to create a boolean value to check state instead of taking the state directly as a boolean value) is by no means incorrect. It's just extraneous to my thought processes. For example, when looking at a red light (for those of us not colorblind...): when you see the light do you first think "that light is a color" and then "is that color red?", or do you just think "that light is red"? I just think there is space for both of us to be correct.