To add-in a counter for the accumulated time in seconds that the door has been open during the day, I’d make these changes as well as those listed above…
timer.setInterval(1000L, read_button); // This timer in vopid setup needs to run once per second for the seconds counter to work
long int elapsed_time_secs;
void read_button()
{
bool currentState = digitalRead(BUTTON_PIN);
if (currentState)
{
// we get here if the door is open
elapsed_time_secs ++; // add 1 to the total number of seconds that the door has been open
Serial.print("Door has been open for = ");
Serial.print(elapsed_time_secs);
Serial.println(" seconds");
//Blynk.virtualWrite(??, elapsed_time_secs); // choose a virtual pin for this
// If you wanted an indicator of the door's current state (LED widget maybe)
// you could do a Blynk.virtualWrite(vPin,1) in here
if(prestate == 0)
{
// we get here if the door is open, but 1 second ago it was closed
counter++; // add 1 to the total number of times that the door has been opened
Serial.print("Door has been opened = ");
Serial.print(counter);
Serial.println(" times");
Blynk.virtualWrite(4, counter);
prestate = 1;
}
}
else
{
// we get here if the door is closed
prestate = 0;
// If youre adding the current state indicator LED then you'd need a
// Blynk.virtualWrite(vPin,0) in here.
}
}
It’s not clear how you are resetting your door opening counter to zero at 10pm, but you’d also need to do the same for the elapsed_time_secs as well.
In the comments I’ve suggested a widget (an LED maybe) that shows you the current state of the door. If this is something you’d find useful then its easy to implement.
Pete.