I am struggling to figure out how I can implement a way to determine the time the heater has been turned on for in seconds for a period of an hour. So if the swarm is low stop the timer. If the swarm is on, continue the term where it has been left off.
Would I use something like serial print millis for the time the swarm is on?
Here is my code so far:
// DHT
#include "DHT.h"
#define DHT0PIN 2 // Inside
#define DHT0TYPE DHT22
DHT dht0(DHT0PIN, DHT0TYPE) ;
// Heater
const int SWARM = 5; // SWARM PIN
void setup() {
pinMode(SWARM, OUTPUT) ; // Set heater as output
digitalWrite(SWARM, LOW) ; // Initial stage to heater OFF
Serial.begin(115200) ; //Initialize Arduino default serial port
dht0.begin() ;
}
void loop() {
float t0 = dht0.readTemperature() ;
float h0 = dht0.readHumidity() ;
float f0 = dht0.readTemperature(false) ;
if ( t0 > 32 )
{
digitalWrite(SWARM, LOW) ;
Serial.println("OFF") ;
Serial.println() ;
}
else
{
digitalWrite(SWARM, HIGH) ;
Serial.println("ON") ;
Serial.println() ;
// How long the swarm has been turned on for in an hour in seconds
}
}
Save the value of millis() as the start time when you turn the heater on. When you turn the heater off save the value of millis() as the stop time. Subtract the start time from the stop time and add the result to the total time
For the 1 hour timer user the BlinkWithoutDelay principle (see the example in the IDE) with a period of 1 hour, remembering to set the total to zero at the start of the hour. At the end of the hour do what you want/need with the total
I have an Arduino program that controls my fridge. It checks the fridge temperature every second and while doing that it increments one of two variables - one for ON and one for OFF. I have some Serial.print() statements that print the two values so I can see that it was (say) ON for 5,000 secs and off for 18,000 secs.
Would it be reasonable to write a line of code that adds all the times together when the swarm has been turned on for and then totalled when one hour is reached?
Could you please send me some examples so I can read, understand and then implement?