Timer to determine how long heater has been turned on for in an hour

Hello all,

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.

Something similar may work for you.

...R

(deleted)

Thank you for the idea of how I can use millis.

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?

Much appreciated.

Something like this?

// DHT

#include "DHT.h"
#define DHT0PIN 2 // Inside
#define DHT0TYPE DHT22
DHT dht0(DHT0PIN, DHT0TYPE) ;

// Heater

const int SWARM = 5; // SWARM PIN

// Millis

unsigned long previousTime0 = 0 ;
unsigned long interval0 = 2000 ;
unsigned long previousTime1 = 0 ;
unsigned long interval1 = 10000 ;

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) ;
    
  unsigned long currentTime = millis() ; // current time in ms
  unsigned long secondson = currentTime / 1000 ; // convert time in s
  unsigned long start_millis = secondson ; // start timer
  unsigned long stop_millis = secondson ; // stop timer
  unsigned long result_millis = start_millis - stop_millis ;
  unsigned long total_millis = result_millis ; // total

 

  if ( currentTime - previousTime0 >= interval0 ) {
 
    Serial.println(t0) ;
  
  if ( t0 > 25 ) 
  
  {
    start_millis ;    
    digitalWrite(SWARM, LOW) ;
    Serial.println("OFF") ;
    Serial.println() ;
  
  }

  else

  {
    stop_millis ;
    digitalWrite(SWARM, HIGH) ;
    Serial.print(result_millis) ;
    Serial.println(" ON") ;
    Serial.println() ;

    // How long the swarm has been turned on for in an hour in seconds
  }
    previousTime0 = currentTime ;
  }
  
}