Will the Arduino IDE interpret digitalWrite(13,x), where x>0, as digitalWrite(13,HIGH)? And will it interpret digitalWrite(13,x), where x<=0, as digitalWrite(13,LOW)?
1 Answer
Every other value than 0
or LOW
will result in setting the pin to HIGH
. In the following I explain why:
The function digitalWrite()
can be seen in the file hardware/arduino/avr/cores/arduino/wiring_digital.c
. The important lines here are:
if (val == LOW) {
*out &= ~bit;
} else {
*out |= bit;
}
As you can see, it checks, if the given value (from the parameter) is equal to LOW
and if so will set the corresponding pin to 0
. In every other case it will set the pin to 1
. The definition of LOW
can be found in arduino/hardware/arduino/avr/cores/arduino/Arduino.h
:
#define HIGH 0x1
#define LOW 0x0
So the code checks, if the value is equal to 0
and sets the pin also to 0
. Every other value will result in a 1
(meaning HIGH
). Also have a look at the definition of digitalWrite()
:
void digitalWrite(uint8_t pin, uint8_t val)
The value is an unsigned type, so every signed type will be casted to an unsigned type. A negative number would result in a positive number inside the function.
-
Just nitpicking: Also
False
and anything other, which evaluates as 0, works here :) (And it is common to use boolean variables/expression as the val)– gilhadJun 30, 2018 at 22:12
if (val)
and return the state of the register bit