What is the TypeDef for HIGH and LOW?

I want to pass in a variable to a routine for digitalWrite(). In the help, I do not see a standard typedef definition for digitalWrite().

What is HIGH and LOW? int? boolean? unsigned byte?...

"int"

james$ grep HIGH *
Arduino.h:#define HIGH 0x1

Thanks. I guess I was looking for the correct prototype for digitalWrite()

Which is am guessing to be:

 void digitalWrite(int pin, int value);

All of this is in the code included with the IDE.

Depends on your OS, but the eventual directory structure is: “hardware/arduino/cores/arduino”

james$ grep digitalWrite *
Arduino.h:void digitalWrite(uint8_t, uint8_t);
...
wiring_digital.c:void digitalWrite(uint8_t pin, uint8_t val)

HIGH = 1 LOW = 0.

But, you can pass any number to digitalWrite(). If it is not 0, then the output is a logic 1, if the number is 0, the output is a logic 0.

[quote author=Tom Carpenter link=topic=182057.msg1349156#msg1349156 date=1376148422] HIGH = 1 LOW = 0.

But, you can pass any number to digitalWrite(). If it is not 0, then the output is a logic 1, if the number is 0, the output is a logic 0. [/quote] Technically, that is isn't quite correct. And while it works with todays Arduino team supplied AVR core code, it is taking advantage of undocumented internal behavior of the code and is bad programming practice.

The current digitalWrite(pin, value) code in the Arduino team supplied AVR core code, will set the output of the pin to a logic 1 if the value parameter is not "LOW". It uses the define LOW, not a value of zero.

So while LOW is defined as 0 and given the way the code is currently written, using 0 and non zero values will work as Tom mentioned above when using today's Arduino Team supplied AVR core code.

However, that is not how the API is defined and any time you step outside the definition of an API and start taking advantage of internal implementation knowledge, you run the risk of getting burned at some point in the future. The best thing is to always use an API the way it is defined. http://arduino.cc/en/Reference/digitalWrite

Which defines the value parameter as being either HIGH or LOW. The behavior of any other value is undefined. The API does not state that 0 and non zero values can be used. It says to use HIGH and LOW to get the desired results.

As an example consider the exact code inside the digitalWrite() function of the Arduino team supplied AVR core code:

    if (val == LOW) {
        *out &= ~bit;
    } else {
        *out |= bit;
    }

In this case if the value is not LOW, you get a digital 1 on the output. So when LOW is 0, then any non zero value will create a 1 on the output.

However, consider if this or some other core had written the code like this instead:

    if (val == HIGH) {
        *out |= bit;
    } else {
        *out &= ~bit;
    }

Which is perfectly valid, and still conforms to the digitalWrite() API, but in this case, any value other than HIGH will create a digital 0 on the output pin. So if HIGH is 1 and LOW is 0, HIGH or 1 will create a digital 1 on the output and LOW or 0 will create a 0 in the digital output, but if you used other undocumented values as the value parameter i.e. (in 2, 3, 42, etc..) you would get a digital 0 on the output pin.

When using an API it is best to always use it the way it is documented and for digitalWrite() than means using LOW and HIGH and not 0, 1, or non zero values.

--- bill

it is taking advantage of undocumented internal behavior of the code and is bad programming practice.

It's perfectly in tune with the ethos of C programming - zero is false, non-zero is true. It's been that way since the first edition of K&R

AWOL:

it is taking advantage of undocumented internal behavior of the code and is bad programming practice.

It’s perfectly in tune with the ethos of C programming - zero is false, non-zero is true.
It’s been that way since the first edition of K&R

While I agree with you that historically zero is false, and non-zero is true,
it is irrelevant with respect to an API definition.

An API definition defines the API any way it wants to.
It defines how to use to use the API to get the desired results.
If you step outside outside the API definition and use parameter values that
are undefined with respect to the API definition
by trying to take advantage of knowledge of undocumented internal behavior of code in the underlying
API code implementation then that is bad programming practice.

In this case the digitalWrite() API documentation is very clear.
value can be HIGH or LOW
HIGH means you get a digital 1 on the output.
LOW means you get a digital 0 on the output.
The output for any other value is undefined.
i.e. the digitalWrite() documentation does not state that you can use values other
than HIGH or LOW or that you can use any non zero value to
get a digital 1 output.
Therefore using parameter values other than HIGH or LOW is making an expectation
of behavior that the API definition simply does not commit to.

The only way to really know that using undocumented value parameters
such as all non zero values sets the digital output to digital 1
is to look at code implementation of digitalWrite().
Any time you are using an API with undocumented parameters
and depending on a particular code implementation of the underlying API code
to make your code work, that is a bad programming practice.

Again, it is best to use an API the way it is defined.

— bill

Maybe someone should have a word with the Arduino team

https://www.arduino.cc/reference/en/language/variables/data-types/bool/

In the example code they pass a boolean to digitalWrite().