2

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))
  • 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. – Pie 'Oh' Pah 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 1:23
3

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.

1

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.

  • 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 8:12
  • 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 Apr 26 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 at 18:42
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.