Go Down

Topic: Read a pin as a boolean value (Read 746 times) previous topic - next topic


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);



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).


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.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


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


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.


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

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131