Go Down

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

jcarrr

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

PaulS

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

CrossRoads

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.

jcarrr

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

PaulS

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.

jcarrr

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