Pages: [1]   Go Down
Author Topic: Read a pin as a boolean value  (Read 575 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems like this has worked before but now I am uncertain about the current results:

boolean variable = digitalRead(pin);

as in:

 IsWater = digitalRead(ReadFloatPin);

Thanks
Logged

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

The digitalRead() function returns an int, containing one of two values - HIGH or LOW. LOW is defined as 0, while HIGH is defined as 1. Coincidentally, these are the same values assigned to false (LOW) and true (HIGH).
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26941
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I usually do it as
byte variable = digitalRead (pinName);

Then value is always 0 or 1, good for doing other operations if needed.
And only 1 byte of memory as far as I know.

With boolean, result is technically 0 or not 0, which is fine if just looking for a True/False answer.
Int? Byte? I don't know, will trust PaulS on that.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, the byte variable sounds like a good solution.  I think it was salvation once before when things seemed to become flaky, but I cannot find the code
Logged

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

Quote
Thanks, the byte variable sounds like a good solution.
That's probably the best choice, since a boolean is the same size as a byte.
Logged

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried both.  The results seem to be the same.  The underlying problem proved to be a misplaced  } that caused some code to be included in an   else .
Thanks again
Logged

Pages: [1]   Go Up
Jump to: