Stuck with Button Code

Hardware: NodeMCU (ESP-12E)
Platform : Android

Working on a water level indicator with a button to switch on the pump, The button can only be turned on if the PERCENTAGE is higher than 4 percent, and while the button is on and the water level reaches below 4 percent, the button should turn off the relay and also update the off position in the app.

The button reads the on value and turns on the relay but it seems like it’s stuck in the on position and the relay also stays on…any help is appreciated.

And I’m not a programmer, just started with arduino and IOT, the attached code is an extraction of codes from different projects that people have posted on the web, I just compiled them.

#define trigPin 12
#define echoPin 13
#define relay 5

long duration, distance; // Duration used to calculate distance

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <math.h>
#include <SimpleTimer.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxxxxxxxxxxx";

SimpleTimer timer;
unsigned int notified = 0;
int percentage;

void setup() 

  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relay, OUTPUT);
  timer.setInterval(2000, RepeatTask);

void RepeatTask()
  /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 

 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;


percentage = map(distance, 138, 7, 0, 100); //Convert distance into percentage for the guage.

Blynk.virtualWrite(6,percentage);// virtualpin 6 write percentage value

if (percentage <= 5 && notified == 0)
  notified = 1; 
  //"","PLEASE FILL THE TANK", "now");
} else if(percentage > 7)
  notified = 0;

BLYNK_WRITE(V1) // Run this function when V1 button pressed.
     int pinValue = param.asInt();  // Get status of V1.
    if (pinValue == 1 && percentage > 4) {  // If status of V1 is 1 and percentage is greater than 4
      digitalWrite(relay, HIGH); // Turn on the relay
    }else {
   int pinValue = 0; 
   Blynk.virtualWrite(V1, pinValue); //Update button value to OFF

void loop() {;; // Initiates SimpleTimer

I do recommend you try to learn… as there is many little things in your code that need to be understood in-order to properly mix and match them. We do not teach programming here, just try to teach how to use Blynk. But you can find programming courses and site on the web.

You have a mixture of BLYNK_READ() and BLYNK WRITE() functions. You should use timers in your code to call void myFunction() functions instead of BLYNK_READ()

Since this function gets called at every button’s (V1) state change (press and release) you do not need to tell the same V1 that it is tuned OFF, when it is… well… turned off :stuck_out_tongue: as that is already handled with… int pinValue = param.asInt(); // Get status of V1.

Instead you could remove that entire else() process and have the re-setting of the button (presumably in the switch state) handled in the determination function…

like this…

} else if(percentage > 7)
  notified = 0;
  Blynk.virtualWrite(V1, 0); //Update button value to OFF
  Blynk.syncVirtual(V1);  // Follow up by calling the BLYNK_WRITE(V1) function with new state value.


A lot of guys do this… copy, paste, copy, paste, copy, paste and turn on the blender… after that “HELP HELP this code does not work”

I’m not speaking specifically of Moshin00 but it’s a recurring situation that we find every day.

oh, that works !!



Thanks for the instant response, in my previous thread you suggested to use SimpleTimer and functions in my code, I followed your steps and that took me in the right direction, really appreciate that.

The solution you suggested for my code worked out like a charm, and yes, it was stupid of me to call int = pinValue = 0; in the V1 :stuck_out_tongue:


Programmer and a PRO at Photoshop:P That’s rare dude!

1 Like

I apologize, my approach made you mad, I can’t say about the “GUYS” but I’m working on my programming skills. Hopefully I will get there!


sounds good

Remember… :wink: