I’m trying to count the time that passes from turning a relay on

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.

Hi Natial,
When I compiled the above code. Following compilation error occur
exit status 1
redefinition of ‘void ElapsedTime()’

after then, I used
void ElapsedTime_2()
timer.setInterval(1000L, ElapsedTime_2);
compiled ok but ElapsedTime of both button not work.

Hello Sir,

How we write the above highlighted variable
To new version.

Thanks PeteKnight and Natial for your Guidance.

Finally i succeed by declaring the variable for two relay like this

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

int runHours_a;
int runMinutes_a;
int runSeconds_a;
int secsRemaining_a;
char buff[21];**
unsigned long startMillis_a;
unsigned long endMillis_a;
unsigned long allSeconds_a;

@Rizwan_Saeed whenever you post code on the forum it needs to have triple backticks at the beginning and end of the code so that it displays correctly.
Triple backticks look like this:
```

Please go back and edit your posts (using the pencil icon at the bottom) and add triple backticks to your code.

Pete.