ebolisa
September 13, 2016, 6:52pm
1
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();
}
system
September 13, 2016, 6:54pm
2
Surely I'm missing something here!
Possibly. What do you think it does?
Maybe you're not seeing the second "=" in "=="
Jimmy60
September 13, 2016, 7:01pm
4
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;
}
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.