Start timer when there is no water

Hello. I need help with my project. I using water level guage with next levels of water(0%, 25%, 50%, 75%, 100%). I need timer, which turned on, when the level is at 0% and turned off when the level is higher than 25%. When the water level is lower than 25%, than start timer again(not from zero, timer must start from the last displayed time). So i need start/stop function and also i need reset button. Whenever I want to reset timer, then I press reset button and set the timer to 0. Timer must show h:m:s.
Basic program is here:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "123";

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

#define INzero 2    //     GPIO2,  D4   //ZERO WATER
#define INqut 5    //     GPIO5,  D1 
#define INhlf 4    //     GPIO4,  D2
#define INthf 12   //     GPIO12,  D6
#define INful 13   //     GPIO13,  D7

BlynkTimer timer; 

void sendSensorReadings() {

 if(digitalRead(INzero) == HIGH) {
   Serial.println("Level"); 
   Blynk.virtualWrite(V1, 0);
 }

 if(digitalRead(INqut) == HIGH) {
   Serial.println("Level"); 
   Blynk.virtualWrite(V1, 25);
 }

 if(digitalRead(INhlf) == HIGH) {
   Serial.println("Level"); 
   Blynk.virtualWrite(V1, 50);
 }

 if(digitalRead(INthf) == HIGH) {
   Serial.println("Level"); 
   Blynk.virtualWrite(V1, 75);
 }

 if(digitalRead(INful) == HIGH) {
   Serial.println("Level"); 
   Blynk.virtualWrite(V1, 100);
 }
}


void setup() {

pinMode(2, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);

timer.setInterval(1000, sendSensorReadings);
delay(10);

Blynk.begin(auth, ssid, pass);
delay(20);
}

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

Thanks a lot :slight_smile:

@Javal1 please edit your post and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Hello, still no idea how to do and i dont find any code which will be that what I need

I don’t think you’ll find community members who will write your code for you, but some of will help steer you in the right direction if you make an effort to figure things out for yourself.

You’ll need a variable to hold the overall elapsed time (presumably in seconds) and one to hold the start time of the current timed period.
You can use the millis() function to time these events, or the Blynk RTC widget.

You’ll also add a widget button to reset the overall elapsed time.

Pete.

Thanks a lot, ive made small progress: Please help me with next problem: Time didnt stop(please watch video), time must stop when the water level is on 25% and when level goes back to 0% must continue…
video: https://drive.google.com/open?id=1qPXR52bppFSG4PcCI8Shh-Yx32VLpnhp
code:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>

char auth[] = "123";

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

int latchButton;
int runHours;
int runMinutes;
int runSeconds;
int secsRemaining;
char buf[21];
unsigned long startMillis;
unsigned long endMillis;
unsigned long allSeconds;

#define INzero 2    //     GPIO2,  D4
#define INqut 5    //     GPIO5,  D1 
#define INhlf 4    //     GPIO4,  D2
#define INthf 12   //     GPIO12,  D6
#define INful 13   //     GPIO13,  D7


SimpleTimer timer;


void sendSensorReadings() {
 if(digitalRead(INzero) == HIGH) {
   Blynk.virtualWrite(V0, 100);
 }
 if(digitalRead(INzero) == LOW) {
 }

if((digitalRead(INzero) == HIGH) && (digitalRead(INqut) == LOW)) {
ElapsedTime();
}
 if(digitalRead(INqut) == HIGH) {
   Blynk.virtualWrite(V1, 100);
 }
 if(digitalRead(INqut) == LOW) {
   Blynk.virtualWrite(V1, 0);
 }

 if(digitalRead(INhlf) == HIGH) {
   Blynk.virtualWrite(V2, 100);
 }
 if(digitalRead(INhlf) == LOW) {
   Blynk.virtualWrite(V2, 0);
 }

 if(digitalRead(INthf) == HIGH) {
   Blynk.virtualWrite(V3, 100);
 }
 if(digitalRead(INthf) == LOW) {
   Blynk.virtualWrite(V3, 0);
 }

 if(digitalRead(INful) == HIGH) {
   Blynk.virtualWrite(V4, 100);
 }
 if(digitalRead(INful) == LOW) {
   Blynk.virtualWrite(V4, 0);
 }
}



BLYNK_WRITE(V10) {  // Button Widget
  latchButton = param.asInt();
  if (latchButton == 1) {
      digitalWrite(0,   HIGH);
      Serial.println("off");
  }
}


void ElapsedTime(){
    if (latchButton == 1) {
    startMillis = millis();  // Stop the counting
    
   }
    endMillis = millis(); //Start the counting from 0
    allSeconds = (endMillis - startMillis) / 1000;
    secsRemaining = allSeconds % 86400;
    runHours = secsRemaining / 3600;
    secsRemaining = allSeconds % 3600;
    runMinutes = secsRemaining / 60;
    runSeconds = secsRemaining % 60;
    sprintf(buf, "%02d:%02d:%02d", runHours, runMinutes, runSeconds);
    Blynk.virtualWrite(V15, buf);
  }


void setup() {

pinMode(2, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);

timer.setInterval(1000, sendSensorReadings);
delay(10);


Blynk.begin(auth, ssid, pass);
delay(20);
}

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

I’m not able to watch the video on the device I’m using.
You need to add code to stop the counting process when the 25% level is reached.

I’m not clear what the f7nctiin of the “latchbutton” is, and it would be much easier if you usd variable names that were more meaningful, and added comments to the code to explain the logic.

It would also help if the comments you already have were accurate…

Pete.

Correct, but hwere to add stop code? In which part of code? Thanks a lot for your help :slight_smile:

Here is the code with comments:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>

char auth[] = "123";

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

int ResetButton;
int runHours;
int runMinutes;
int runSeconds;
int secsRemaining;
char buf[21];
unsigned long startMillis;
unsigned long endMillis;
unsigned long allSeconds;

#define INzero 2    //     GPIO2,  D4   INzero=INPUT for zero%
#define INqut 5    //     GPIO5,  D1    INqut=INPUT for 1/4%(25)
#define INhlf 4    //     GPIO4,  D2    INhlf=INPUT for half%(50)
#define INthf 12   //     GPIO12,  D6   ...
#define INful 13   //     GPIO13,  D7


SimpleTimer timer;


void sendSensorReadings() {
 if(digitalRead(INzero) == HIGH) {    //Input for 0% is 1-turn ON the led diode on V0
   Blynk.virtualWrite(V0, 100);   //Time loop can start, becose there is no water
 }

if((digitalRead(INzero) == HIGH) && (digitalRead(INqut) == LOW)) {    //turn ON time loop
ElapsedTime();
}
 if(digitalRead(INqut) == HIGH) {   //Input for 25% is 1-turn ON second led diode on V1, time loop must stop
   Blynk.virtualWrite(V1, 100);
 }
 if(digitalRead(INqut) == LOW) {    //Time loop can start now, becose there is no water
   Blynk.virtualWrite(V1, 0);
 }

 if(digitalRead(INhlf) == HIGH) {   //Input for 50% is 1-turn ON third led diode on V2, time loop must stop
   Blynk.virtualWrite(V2, 100);
 }
 if(digitalRead(INhlf) == LOW) {    //Time loop must stop, becose 25% is still 1
   Blynk.virtualWrite(V2, 0);
 }

 if(digitalRead(INthf) == HIGH) {   //Input for 75% is 1-turn ON fourth led diode on V3, time loop must stop
   Blynk.virtualWrite(V3, 100);
 }
 if(digitalRead(INthf) == LOW) {    //Time loop must stop, becose 50% is still 1
   Blynk.virtualWrite(V3, 0);
 }

 if(digitalRead(INful) == HIGH) {   //Input for 100% is 1-turn ON fifth led diode on V4, time loop must stop
   Blynk.virtualWrite(V4, 100);
 }
 if(digitalRead(INful) == LOW) {    //Time loop must stop, becose 75% is still 1
   Blynk.virtualWrite(V4, 0);
 }
}



BLYNK_WRITE(V10) {  // Reset Button Widget, when I want to reset the timer
  ResetButton = param.asInt();
  if (ResetButton == 1) {
      digitalWrite(0,   HIGH);
      Serial.println("off");
  }
}


void ElapsedTime(){   //Time loop
    if (ResetButton == 1) {   //Reset
    startMillis = millis();  // Stop the counting
   }
   
    endMillis = millis(); //Start the counting from 0
    allSeconds = (endMillis - startMillis) / 1000;
    secsRemaining = allSeconds % 86400;
    runHours = secsRemaining / 3600;
    secsRemaining = allSeconds % 3600;
    runMinutes = secsRemaining / 60;
    runSeconds = secsRemaining % 60;
    sprintf(buf, "%02d:%02d:%02d", runHours, runMinutes, runSeconds);
    Blynk.virtualWrite(V15, buf);   //Show time on v15
  }


void setup() {

pinMode(2, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);

timer.setInterval(1000, sendSensorReadings);
delay(10);


Blynk.begin(auth, ssid, pass);
delay(20);
}

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

I can’t understand your logic with the way you are using the StartMillis, endMillis and allSeconds variables.
You’ve not said what these do, but presumably allSeconds is the cumulative time.

If the button on V1 is meant to reset the cumulative time then it should set allSeconds to 0.

When you want to start measuring the current period you should be grabbing the current millis() value from the MCU and storing it as startMillis (you currently seem to be storing it as endMillis).

If you want to know the elapsed time of the current period, while it’s still running, then this will be millis() - startMillis /1000

When the water reaches the desired level then you have 2 options…

  1. capture the current millis() value from the MCU and store it as endMillis then add the elapsed time to the overall total with allSeconds = ((endMillis - startMillis)/1000)

or

  1. don’t bother with the endMillis variable and simply do the calculation there and then with allSeconds = ((millis() - startMillis)/1000)

Pete