Controlling hardware without widgets

Hello everyone!
I am currently facing a problem which looks trivial even to me, if not for the fact that i can’t solve it!
I have written a simple code that allows me to control several LEDs with Widgets. What i would like to do now is to have a push button turn off one of these LEDs when it is pressed. Simple enough. However, i can’t get it to work!
I’ve written a different code to read the button values and test if everything works as it should and it does indeed work as intended, so i know the hardware isn’t the problem, nor is it the overall code. But i can’t get to replicate the result in the blynk code!
I’ve tried reading the button from the loop function and from the BLYNK_WRITE() function, but it just… doesn’t. I get no readings and therefore no hardware control based on said readings.
Does anyone see what i’m doing wrong?
This is the code:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>

char ssid[] = "";
char pass[] = "";

BlynkTimer timer;

BLYNK_WRITE(V0){
  if(param.asInt()==1){
    digitalWrite(5,HIGH);
   } else {
    digitalWrite(5,LOW);
  }
}

BLYNK_WRITE(V1){
  if(param.asInt()==1){
    digitalWrite(21,HIGH);
   } else {
    digitalWrite(21,LOW);
  }
}

BLYNK_WRITE(V4){
  if(param.asInt()==1){
    digitalWrite(2,HIGH);
    Blynk.setProperty(V5,"isDisabled",true);
   } else {
    digitalWrite(2,LOW);
    Blynk.setProperty(V5,"isDisabled",false);
  }

}

BLYNK_WRITE(V5){
  if(param.asInt()==1){
    digitalWrite(4,HIGH);
    Blynk.setProperty(V4,"isDisabled",true);
   } else {
    digitalWrite(4,LOW);
     Blynk.setProperty(V4,"isDisabled",false);
  }
if(digitalRead(14)==LOW){
Serial.println("off") //this will become digitalWrite(something) as soon as i understand how to get the readings
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  pinMode(2,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(21,OUTPUT);
  pinMode(14,INPUT_PULLUP);
}

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

The code you’ve posted doesn’t contain any code to poll your button, or any indication which pin your button is attached to. There’s also no indication of what is attached to the various virtual pins, and what function they perform.

Pete.

You have to use an interrupt to read the digital input attached to the physical button

Oh, you’re right! I didn’t realize i posted the code without the (failed) attempts at printing! I’ll try out the suggestions in the comments and if those won’t work out i’ll edit the code!
Thanks

I’ll be trying that out right now, thanks!

1 Like

Solved! Thanks everyone!

1 Like