Hi mates!
I already have a project that measures my cold chambers temperature by I want to add a feature that gives me the openings during day and how many minutes the door remains opened (sum of all openings). I don’t even know what libraries to use and how to start coding these new features. I don’t want any buttons to reset. I want the reset to occur automaticaly at 00:00 each day and the #s and minutes to be stored in a graph.
I think of an algorithm like this (input: limit switch: opened 1, closed 0)
when day starts (00:01), counter = 0, elapsed = 0
void loop
{
if switch = 1; counter++
if switch = 1,
starts chrono
when switch = 0,
stops chrono
elapsed = elapsed + chrono
}
You can use the millis() function to calculate the elapsed time after any open/close event. Or you can use an RTC module or NTP client library to track time and date instead.
That’s so easy. You can use the RTC module or NTP client to track time and perform the reset at 00:00:00 every day.
I think I’d use global variables to track the following:
Count of door openings
Previous door state (open/closed)
Elapsed door open time in seconds
I’d then use a BlynkTimer to call a function once every second.
That timed function would do the following…
If door is closed, set the “Previous door state” variable to closed (0)
If the door is open then:
If the “Previous door state” variable was closed (0) the add one to the “Count of door openings” variable (because this is a new door open event) and set the “Previous door state” variable to open(1) so that the next time this timed event runs in 1 second and the door still open it’s not counted as a new door open event.
Increment the “Elapsed door open time in seconds” timer by 1
Send updated values for “Count of door openings” and “Elapsed door open time in seconds” to Blynk
As far as the reset to zero at midnight part is concerned, I’d use a “Schedule” Automation to trigger a virtual pin at midnight every day. The BLYNK_WRITE(vPin) handler for this virtual pin would set the “Count of door openings” and “Elapsed door open time in seconds” variables to zero. I’d also set the “Previous door state” variable to closed (0) so that if the door is open when the counters are reset a new door open event is immediately triggered and the elapsed time counted.
I’m happy to share my little project with you!
For now, only “[variable] 1” are implemented. “Aberturas” means “openings” and it’s reseting to 0 by 10pm. Working flawlessly!
I’m still strugling with the elapsed time feature: couldn’t figure out how would be the code without flooding blynk server.
PS: understanding the graph: the compressor turns ON, that’s when curve starts to fall, removing heat from the cold chamber. After a given time interval, it shuts OFF and the heat resistance turns ON, to prevent forming ice and blocking the evaporator. that’s why air temperature rises so fast. After 30 minutes, the cycle restarts. And it goes on and on and on, working nonstop for the past 24 years in my company
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.