But the right hand side is not boolean, it is 0x1
Itâs 0x1 after translation.
But now weâre nitpicking on terms.
The main issue for the OP is that he is comparing an analog measurement to something Boolean, which will not produce the result he hopes for.
It is not nitpicking to say that HIGH is not a boolean. A boolean is a data type, not just a value of 0 or 1
Whilst I agree that the use of analogRead() to test for HIGH makes no sense, what is being tested is not a boolean
Before I posted reply #16, I did a little test to verify that what I was saying was correct.
I modified gonanf_2's code to print out the value of 'R', as well as a message to say ', R == HIGH' if the condition (R == HIGH) was met.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int R = analogRead(A0);
Serial.print(R);
Serial.print("");
if (R == HIGH) {
Serial.print(", R == HIGH");
}
Serial.println();
delay(10);
}
and here are the results:
The message ', R == HIGH' is only printed when R = 1.
I won't bother showing the other 970 or so values of R that don't print the message ', R == HIGH'.
@JohnLincoln THX for going to the trouble.
tl;dr not a rant
I can understand someone posting wrong, even dangerously or stupidly wrong information.
Something you "learned", something that has always worked for you, something you see other ppl doing alla time.
Perhaps that can be excused.
But there is no excuse for doubling down, digging deeper the whole you've been told you were in, in some cases adding more bogus reasoning and "facts" whenâŚ
...in (I estimate) about eleven minutes you could just get out a stupid UNO board, write a twenty line program and demonstrate the correctness of your POV or show yourself your own ignorance.
And even less of an excuse these days, when you can shave minjtes off such an effort by using the wokwi simulatorâŚ
There are things I am so very sure about I can just post on the matter at hand. If someone sez "nah huh, you wrong", I don't respond "am not am not" until I have googled and maybe also done a little tiny test, takes eleven minutes or less.
When I am wrong, I don't mind saying so, and I appreciate the opportunity to learn something, which I can freely say I do here in these fora with some good regularity. I don't think I'd participate if I wasn't also learning something. About C/C++, about coding, about what ppl get up to with these little toys, about myself.
This is the nice thing about engineering and science. Experiments can test theories, and participants agree to abide by the results.
a7
I stand corrected.
I ran a little test myself, and (R == HIGH) evaluates to 1 only when R is 1, and 0, 2, 3, ⌠1023 all produce a 0.
Apparently âHIGHâ is translated to 1 before the comparison, the compiler doesnât recognize it as Boolean.
Still, it doesn't produce the result the OP probably hoped for.
Because of how HIGH is #defined
#define HIGH 0x1
a 1 is inserted into the code even before it is compiled
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

