Pages: [1]   Go Down
Author Topic: Interchanging HIGH/LOW with true/false  (Read 855 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I notice that both of these lines work:

Code:
boolean pressed = (digitalRead(3)==HIGH);

boolean pressed = digitalRead(3);

I would assume that the first is more correct unless HIGH and LOW were interchangeable with true and false in which case the second line would be a better way to write it.

Which is correct? or more correct? Can I always trust that second one to work?

Thanks
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 109
Posts: 4074
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Boolean FALSE is zero, anything else is TRUE, so you can use any value that resolves to one or the other in a boolean test.  The answers from your examples depend more on how the switch is wired than the program statements used.  For instance, a floating switch input is not guaranteed to be in one state or another for any length of time if the switch is open.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A boolean is simply a byte sized variable. True is non-zero. False is zero.

HIGH and LOW are defined as 1 and 0 which match the definitions of true and false.

So, either f your statements will work, under some circumstances, although I prefer the first one. It explicitly says that you want to compare the reading of the pin to HIGH.

Think about what the second statement would mean if the internal (or an external) pullup resistor is used. When pullups are used, LOW (false) mean pressed.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Definitely the first one is better as it is the most explicit.

Bob Martin (from the book Clean code) would say "use descriptive names where possible" and add spaces around the ==

boolean isButtonXPressed = (digitalRead(ButtonX) == HIGH);  

This makes the code self documenting
« Last Edit: February 21, 2013, 04:36:23 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
boolean pressed = (digitalRead(3)==HIGH);

You don't need the brackets, so:

Code:
boolean pressed = digitalRead(3) == HIGH;

I prefer that, because for one thing, in a lot of cases, where you use the pull-up resistor, pressed would be:

Code:
boolean pressed = digitalRead(3) == LOW;

Second, semantically, it is "true" you did a digital read, but did it return HIGH or LOW?
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some people advocate writing code like   
Code:
boolean state = ( HIGH == digitalRead(3) )

The reason to do this,  is that you will get a compiler error if you type the wrong number of equals signs.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Some people advocate writing code like   
Code:
boolean state = ( HIGH == digitalRead(3) )

The reason to do this,  is that you will get a compiler error if you type the wrong number of equals signs.
Personally I err on the side of extra brackets.  Yes, it may look a little more crowded, but you can then be 100% certain what order things are going to happen in.  Some of the order of precedences can be a little grey - if you have two operators that have the same order of precedence, which one will happen first?  And can you remember the order of precedence of ALL the operators off the top of your head?  Thought not.  I have a good memory, and I can't - only that * and / happen before - and + (as taught in maths lessons when you're 6).  So, add brackets to make it explicit.  Assembly doesn't use brackets, so it shouldn't make the slightest bit of difference to the quality of the resultant code, but it might make all the difference to your calculations.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Some of the order of precedences can be a little grey
I disagree. The precedence rules are quite well defined.

Quote
And can you remember the order of precedence of ALL the operators off the top of your head?
THIS is the real problem. I can't. I can remember that the order is left to right for operators of the same precedence, that multiplication and division are higher than addition and subtraction, that multiplication and division have the same precedence, and that addition and subtraction have the same precedence.

Where ==, >=, >, <=, <, comma, dot, ->, the various shift operators, and the other bit operators fit I have no clue, without looking them up.

But, I do know that parentheses let ME control the order the way I want. So, I use them.

I think OPs use of parentheses around the comparison (as in (digitalRead(n) == HIGH)) are a good idea. It clearly indicates, to me, that it is the result of that comparison that is being assigned to the variable.
Logged

Germany
Offline Offline
Newbie
*
Karma: 4
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Definitely the first one is better as it is the most explicit.

Bob Martin (from the book Clean code) would say "use descriptive names where possible" and add spaces around the ==

boolean isButtonXPressed = (digitalRead(ButtonX) == HIGH);  

This makes the code self documenting

I go one step further with "descriptive names" and define

Code:
const boolean ButtonDown = false; // button connects to GND
...
if(digitalRead(pinButton) == ButtonDown){...}

If I need to change the button to high-side switching, there is only one line of code to change
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2107
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I go one step further with "descriptive names" and define

Code:
const boolean ButtonDown = false; // button connects to GND
...
if(digitalRead(pinButton) == ButtonDown){...}

If I need to change the button to high-side switching, there is only one line of code to change

Thats what enums are great for:

Code:
enum BUTTON_STATE{
  ButtonDown ,
  ButtonUp
};
...
if(digitalRead(pinButton) == ButtonDown){...}
Logged


UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12554
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const boolean ButtonDown = false; // button connects to GND

I like the approach, but I'd argue that the type and value should match the return type and value of digitalRead():

Code:
const int ButtonDown = LOW; // button connects to GND
Logged

I only provide help via the forum - please do not contact me for private consultancy.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const boolean ButtonDown = false; // button connects to GND

I like the approach, but I'd argue that the type and value should match the return type and value of digitalRead():

Code:
const int ButtonDown = LOW; // button connects to GND


... why is digitalRead() even an int?!  And on an 8 bit core too.  It really should be either (unsigned) char, or boolean.

"Arduino.  Helping to encourage bad practices since 2005."
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: