==HIGH ?

I am playing around with an HC-SR501 PIR motion detector unit, and looking at related websites, and I am not understanding a line of code I keep seeing.

Here is the code:-

// Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
DEBUG_PRINT("Got tripped: ");

boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;

Can someone explain the line to me. Why ==HIGH?



it's an evaluated expression.
if the sensor reading is equal to HIGH, it yields true, if not, false

You could think of it as

boolean tripped;
if(digitalRead(DIGITAL_INPUT_SENSOR) == HIGH))
  tripped = true;
  tripped = false;

== is a comparison. Is the left side equal to the right?

= is an assignment. The value calculated on the right (in this case true or false) is stored in the variable named on the left.

To add to Morgan’ response, in my opinion this is where some brackets would help with the human interpretation of this code even if they don’t make any difference to syntax for the compiler:

boolean tripped = (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH);

The form is just an initialised local variable

boolean tripped = <boolean expression>;

where is the result of evaluating


Thanks for all the explanations guys.
That is making perfect sense to me now.
I must admit, I much prefer things to be laid out clearly over a few lines like Metallors structure above. When I look at that format, it instantly makes sense and I can follow it. I'm still writing x=x+1 instead of x++ !.
I guess I just need another year before it becomes second nature.

Anyway, I will shower you all with Karma.

Thanks again