5

Is it ok to use

digitalWrite(pin, 1) instead of digitalWrite(pin, HIGH)

or

digitalWrite(pin, 0) instead of digitalWrite(pin, LOW)

I want it that way to make the coding lesser because I save values on EEPROM with 1's and 0's. So if I want to use EEPROM value, I can call it this way

digitalWrite(pin, EEPROM.read(1))
Sam San
  • 231
  • 1
  • 2
  • 5
  • 1
    I have no arduino to test now, but my bet is it's possible. HIGH and LOW are just constants for 1 and 0. – Pandemonium Aug 12 '15 at 16:33
  • As a continuation to all the answers, the result of HIGH and LOW being #defined is that there is no change in compiled binary size -- only more descriptive names in source files (which is why they are recommended). The preprocessor converts `HIGH` into `0x1` and `LOW` into `0x0` for you. – cortices Aug 15 '15 at 01:23

3 Answers3

7

Yes, that is fine. LOW is 0 and HIGH is 1. digitalWrite() sets the output to off if it receives a 0 and on if it receives anything of 1 or more.

That means that these are all equivalent:

digitalWrite(pin, HIGH);
digitalWrite(pin, 1);
digitalWrite(pin, 69);

It's especially useful when you are examining a variable for, say, a certain bit being set:

digitalWrite(pin, bytevar & 0x80);

That will set the pin to high on any value from 128 to 255 in the byte variable, and low for anything below 128.

Majenko
  • 103,876
  • 5
  • 75
  • 133
2

From the arduino source code:

#define HIGH 0x1
#define LOW  0x0

So HIGH is exactly the same as 1. And LOW exactly the same as 0.

Gerben
  • 11,156
  • 3
  • 19
  • 33
  • Exactly, and since `HIGH == 1`, then you could save HIGH to the EEPROM. – Nick Gammon Aug 12 '15 at 21:23
  • Also `HIGH - HIGH == LOW` (-; – Gerben Aug 13 '15 at 08:12
  • 1
    This misses the point. The question is: is `LOW` *guaranteed* to always be `0` and is `HIGH` *guaranteed* to always be `1`? Looking at the current source code provides no answer to that question. – AnT stands with Russia Apr 26 '19 at 18:32
  • @AnT HIGH and LOW are Arduino specific. Arduino doesn't have a specification like for example C, C++, or Forth. So looking at the Arduino source code is currently the best way to determine this. Can they change their code tomorrow? yes. Is this likely? Not in the slightest. – Gerben Apr 26 '19 at 18:42
  • it doesn't have a type? As in if I want to declare a variable `state` and assign it a value HIGH or LOW later on, should i declare state as an int or something else? – rfii Jan 28 '21 at 23:07
  • 1
    @rfii #define is a macro. So the preprocessor replaces all the occurrences of HIGH and LOW with 1 and 0, before the result goes to the compiler. So, to the compiler, it looks as if you would have written `1` and `0`. Or did a find&replace yourself. So it doesn't have a type, but can be seen as a string. – Gerben Jan 29 '21 at 13:03
0

When you look at the source code of Arduino, HIGH is defined as 1 and LOW is defined as 0 using #define.

You may heard about Preprocessor, Compiler, and Linker. When you build the project, preprocessor do its job before the code is compiled. What the preprocessor does is basically converting defined words into corresponding value.

For example when you write a syntax like:

digitalWrite(1, HIGH);

When you build your project, the preprocessor converts the above like into this:

digitalWrite(1, 1);

Then the code is compiled. Therefore, there is no difference between HIGH and 1. So, if the result of EEPROM.read(1) is either 0 or 1, there should be no problem.

Bumsik Kim
  • 216
  • 1
  • 3