6

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
  • 241
  • 1
  • 2
  • 7

3 Answers3

8

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
  • 105,851
  • 5
  • 82
  • 139
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,332
  • 3
  • 22
  • 34
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