Hi Rizwan,
It’s been a while since I created this code, but maybe you can try to do something like this:
BlynkTimer timer;
int latchButton;
int latchButton_2;
int latchFlag;
int latchFlag_2;
int runHours;
int runMinutes;
int runSeconds;
int secsRemaining;
char buf[21];
unsigned long startMillis;
unsigned long endMillis;
unsigned long allSeconds;
//===== Timeed latching button =====
BLYNK_WRITE(V1) { // Button Widget set as switch
latchButton = param.asInt();
if (latchButton == 1 && latchFlag == 0) {
latchFlag = 1; // Keeps from allowing button press more then once while relay activated
digitalWrite(0, HIGH); // Activate digital pin
Serial.println(latchButton); //Debug
}
else{
digitalWrite(0, LOW); // Deactivate digital pin
Serial.println("off"); //Debug
Blynk.virtualWrite(V1, 0); // Reset Latching Button to OFF
latchFlag = 0; // resets to allow next interaction
}
}
//===== Timeed latching second button =====
BLYNK_WRITE(V3) { // Second button Widget set as switch
latchButton_2 = param.asInt();
if (latchButton_2 == 1 && latchFlag_2 == 0) {
latchFlag_2 = 1; // Keeps from allowing button press more then once while relay activated
digitalWrite(1, HIGH); // Activate digital pin
Serial.println(latchButton_2); //Debug
}
else{
digitalWrite(1, LOW); // Deactivate digital pin
Serial.println("off"); //Debug
Blynk.virtualWrite(V3, 0); // Reset Latching Button to OFF
latchFlag_2 = 0; // resets to allow next interaction
}
}
void ElapsedTime(){
if (latchFlag == 1){ //When the button is active
endMillis = millis(); //Start the counting from 0
//The key process for displaying the converted duration
allSeconds = (endMillis - startMillis) / 1000;
secsRemaining = allSeconds % 86400;
runHours = secsRemaining / 3600;
secsRemaining = allSeconds % 3600;
runMinutes = secsRemaining / 60;
runSeconds = secsRemaining % 60;
sprintf(buf, "Duration: %02d:%02d:%02d", runHours, runMinutes, runSeconds);
Blynk.virtualWrite(V2, buf); // Display Widget
}
else{
startMillis = millis(); // Stop the counting
}
}
void ElapsedTime(){
if (latchFlag_2 == 1){ //When the second button is active
endMillis = millis(); //Start the counting from 0
//The key process for displaying the converted duration
allSeconds = (endMillis - startMillis) / 1000;
secsRemaining = allSeconds % 86400;
runHours = secsRemaining / 3600;
secsRemaining = allSeconds % 3600;
runMinutes = secsRemaining / 60;
runSeconds = secsRemaining % 60;
sprintf(buf, "Duration: %02d:%02d:%02d", runHours, runMinutes, runSeconds);
Blynk.virtualWrite(V4, buf); // Display Widget
}
else{
startMillis = millis(); // Stop the counting
}
}
void setup()
{
Serial.begin(115200);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
Blynk.begin(auth, ssid, pass);
// Display digital clock every 1 seconds
timer.setInterval(1000L, ElapsedTime);
}
void loop()
{
Blynk.run();
timer.run();
}
Play with it and see if it works.