Puzzled about a code line

Hi,

The code shown below is taken from the Blynk examples (LED status of button).

I’m, however, puzzled on this line...

 boolean isPressed = (digitalRead(btnPin) == LOW);

The variable isPressed will ever change? Surely I’m missing something here!
TIA

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Select your pin with physical button
const int btnPin = 7;

WidgetLED led3(V3);

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);

  // Setup physical button pin (active low)
  pinMode(btnPin, INPUT_PULLUP);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  timer.setInterval(500L, buttonLedWidget);
}

// V3 LED Widget represents the physical button state
boolean btnState = false;

void buttonLedWidget()
{
  // Read button
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led3.on();
    } else {
      led3.off();
    }
    btnState = isPressed;
  }
}

void loop()
{
  Blynk.run();
  timer.run();
}

Surely I'm missing something here!

Possibly. What do you think it does?
Maybe you're not seeing the second "=" in "=="

 boolean isPressed = (digitalRead(btnPin) == LOW);

A boolean value will be either false (0) or true (non-zero). What happens here is that a boolean variable, isPressed is created and assigned the value of the result of a comparison. The comparison will return either zero, for false, or non-zero for true. It's kind of a slick way to store the result of a digital read, which returns an int type, in a smaller boolean type (saves a byte of RAM). Every time that line is executed the switch will be read.

ebolisa:
Hi,

The code shown below is taken from the Blynk examples (LED status of button).

I’m, however, puzzled on this line...

 boolean isPressed = (digitalRead(btnPin) == LOW);

The variable isPressed will ever change? Surely I’m missing something here!
TIA

The long version, and maybe easier for an untrained eye, looks like this.

 boolean isPressed;
if  (digitalRead(btnPin) == LOW) {
   isPressed = true;
   } 
   else {
   isPressed = false;
   }

Got it, thank you all!!

Delta_G:
You're just a little off there. Your variables are scoped to the if block so they cease to exist right after they're created. Kind of useless like that.

You are right. I updated my code.