this millis() business is driving me nuts

Here try this…

#include "DHT_U.h"
#include <timeObj.h>


#define LED_PIN      11    // Usual pin number for built in LED. (You wanted 11)
#define PUMP_PIN     9    // Pin number for pump.
#define SENSE_PIN    7    // Pin number for sensor.


DHT DHTT(SENSE_PIN, DHT11);
float thresholdTemp = 79;

timeObj  vTimer(30000,false);    // Setup a 3 sec timer. (Its a float so set it to whatever you need) [false means don't start]
timeObj  serialTimer(500,true);  // Calling delay is always a bad idea. This allows us to do the serail thing slowly without delay. [True means start now.]
bool     pumping;
float    temp;

// Your standard sketch setup()
void setup() {
   
   Serial.begin(9600);
   pinMode(LED_PIN,OUTPUT);         // Set up the LED pin for output.
   digitalWrite(LED_PIN,LOW);       // LED off.
   pinMode(PUMP_PIN,OUTPUT);        // Set up the pump pin for output.
   digitalWrite(PUMP_PIN,LOW);      // Pump off.
   pumping = false;                 // We ain't pumping now.
   DHTT.begin();
}


void doPump(void) {

   temp = DHTT.readTemperature(true);
   digitalWrite(LED_PIN,temp <= thresholdTemp);  // If LED is running backwards, swap the > sign.
   if (!pumping) {                              // If we are NOT pumping..
      if (temp > thresholdTemp) {               // If its getting too cold.. 
         digitalWrite(PUMP_PIN,HIGH);           // Pump on.
         vTimer.start();                        // Fire up timer.
         pumping = true;                        // Note we are now pumping.
      }
   } else {                                     // Else we are pumping..
      if (temp <= thresholdTemp) {              // If its getting warm..
         if (vTimer.ding()) {                   // If the time is up..
            digitalWrite(PUMP_PIN,LOW);         // Pump off.
            pumping = false;                    // Note we are now NOT pumping.
         }
      }
   }
}
 

// Your standard sketch loop()
void loop() {
     
   doPump();   // Do the temp/pump thing.
   
   // And now you can do the display if you like..

   // Lets loose the delay() call.
   if (serialTimer.ding()) {     // If the serial timer timer has expired..
      Serial.print("TempF: ");   // Write out the temp info. to the serial port.
      Serial.println(temp);
      serialTimer.start();       // Reset the serial timer.
   }
}

Not positive I got theLED stuff correct, you may have to swap its logic around. Also, I knocked out that pesky delay() call for you. Don’t be letting them delay()s creep back in here.

Let me know if it works.

-jim lee

So with hysteresis we look at at past states? But we aren't looking at the When of things?

The principle is simple
When the temperature becomes less than the low level turn on the pump
When the temperature becomes greater than the high level turn off the pump

The StateChangeDetection example in the IDE illustrates how to do this

I see no need for any timers if appropriate low and high temperature levels are used

@UKHeliBob

As far as hysteresis goes…this is what I have so far.

#include "DHT_U.h"
#define Type DHT11

int sensePin=7;
DHT DHTT(sensePin, Type);

float humidity;
float tempC;
float tempF;

float lowerThresholdTemp = 35;
float upperThresholdTemp = 45;

int redLed = 9;
int greenLed = 11;
int lastMotorState = 0;

void setup() {
Serial.begin(9600);
DHTT.begin();
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}


void loop()
{
  humidity = DHTT.readHumidity();
  tempC = DHTT.readTemperature();
  tempF = DHTT.readTemperature(true);
  
  if (tempF < lowerThresholdTemp && lastMotorState=0)
  {
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
    lastMotorState = 1;
  }
  
  if (tempF >= upperThresholdTemp && lastMotorState=1)
  {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    lastmotorState = 0;
  }
  
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(".   TempC: ");
  Serial.print(tempC);
  Serial.print(".   TempF: ");
  Serial.print(tempF);
  Serial.println(". ");

}

So did you test it and what were the results?

  if (tempF < lowerThresholdTemp && lastMotorState = 0)

I got no further than this. Unless you really want to set lastMotorState to zero it (and others like it) need fixing

= for assignment
== for comparison

@UKHeliBob I do that once a week. Seriously. It’s a weakness. I will fix it when I get home. Out with the wife doing holiday shopping. Yikes. We did stop at a pub. Insert smiley face emoji. Thank you and everyone else for the help.

“ We did stop at a pub. ”

Noooooooooo!

@UKHeliBob

We’re back. Fixed, the assignment vs compare issues, I hope. Sorry. I “suspect” I’m not quite dead one with this code but hopefully with some gentle nudging I’ll get it.

And thank you again to all that helped. It’s appreciated.

#include "DHT_U.h"
#define Type DHT11

int sensePin=7;
DHT DHTT(sensePin, Type);

float humidity;
float tempC;
float tempF;

float lowerThresholdTemp = 35;
float upperThresholdTemp = 45;

int redLed = 9;
int greenLed = 11;
int lastMotorState = 0;

void setup() {
Serial.begin(9600);
DHTT.begin();
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}


void loop()
{
  humidity = DHTT.readHumidity();
  tempC = DHTT.readTemperature();
  tempF = DHTT.readTemperature(true);
  
  if (tempF < lowerThresholdTemp && lastMotorState==0)
  {
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
    lastMotorState = 1;
  }
  
  if (tempF >= upperThresholdTemp && lastMotorState==1)
  {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    lastmotorState = 0;
  }
  
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(".   TempC: ");
  Serial.print(tempC);
  Serial.print(".   TempF: ");
  Serial.print(tempF);
  Serial.println(". ");

}

@ jimLee Your code works. Thank you. Geez I have a lot to learn, and I'm no spring chicken anymore. I want to try to see this hysteresis thing through as well. Might as well try to learn as much as I can.

Thanks for the report. I always wonder the stuff I post actually works. The hysteresis approach is probably the preferred method. But at least now you have a backup solution if you need it.

-jim lee

Wait, it couldn’t be that easy. Could it? Below is my hysteresis code for the deicer. Please comment.

#include "DHT_U.h"
#define Type DHT11

int sensePin=7;
DHT DHTT(sensePin, Type);

float humidity;
float tempC;
float tempF;

int redLed = 9;
int greenLed = 11;

float lowerThresholdTemp = 35;
float upperThresholdTemp = 45;

void setup() {
Serial.begin(9600);
DHTT.begin();
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}


void loop()
{
  humidity = DHTT.readHumidity();
  tempC = DHTT.readTemperature();
  tempF = DHTT.readTemperature(true);
  
  if (tempF <= lowerThresholdTemp )
  {
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
  }

  if (tempF >= upperThresholdTemp )
  {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
  }
  
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(".   TempC: ");
  Serial.print(tempC);
  Serial.print(".   TempF: ");
  Serial.print(tempF);
  Serial.println(". ");

}

It looks fine. I suggest that you alter the thresholds so you can test it by touching the sensor with a finger.

For best results, it might be better to keep track of whether the pump is on, so you only change it when necessary, but what you have will work anyway.

@wild Whew. You know I spent a lot of time trying to figure this out. Eventually I figured it out. Regarding testing, I basically did what you suggested except I blew into the HDT11, setting the lower threshold right at the room temp being reported by the sensor. The upper threshold I put at about 82. The red light comes on as expected upon power up. When I breathe into the sensor a little at a time I can raise the temp gradually. As the temp creeps up the red light stays on until the upper threshold is met, when the green light goes on. You can then watch the temp fall on the serial monitor and see that when it returns to the low threshold, room temp in this case, the red light comes back on.

For best results, it might be better to keep track of whether the pump is on, so you only change it when necessary, but what you have will work anyway.

If you have a minute, can you elaborate on the above. I didn't understand.

Thank you for the help. Happy holidays.

The pin that controls the pump is set low by your program every time through loop(), even if it's already low. Sort of like standing at a light switch and turning it on, turning it on, turning it on, turning it on... That's unnecessary but harmless. If you keep track of the pump state using a variable, you can elect to actually digitalWrite() the pin if, and only if, the pump state changes.

@aarg Can you show me where this is happening in the code? I looked but couldn't see where I was setting the pin low every time through the loop. Sorry but I'm very new to all of this.

tperry724:
@aarg Can you show me where this is happening in the code? I looked but couldn’t see where I was setting the pin low every time through the loop. Sorry but I’m very new to all of this.

void loop()  // loop repeats many times a second
{
  humidity = DHTT.readHumidity();
  tempC = DHTT.readTemperature();
  tempF = DHTT.readTemperature(true);
 
  if (tempF <= lowerThresholdTemp )  // suppose this condition is true for a few minutes - plausible.
  {
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);  // then this happens every time loop() executes, see first line

So I just keep writing the redLed pin high, each time through the loop, even though the pin is already high from the last time through? Sorry for being such a blockhead. I didn't see where I set the redLed pin low.

It has an LCD screen now.

it couldn't be that easy. Could it?

Yes, it can be that easy

You can see why I suggested using hysteresis

tperry724:
If you have a minute, can you elaborate on the above. I didn't understand.

Aarg has already covered the specifics. What you have will work fine at the moment, but developers would refer to it as 'dirty', meaning that your code is doing unnecessary work and that it may bite you in the rear later.

Perhaps you decide that you would like to record when the pump comes on. The obvious place to do that would be just after the digitalWrite but in fact the time presented will be the last time you 'turned the pump on' again, not when it actually started pumping.

So you've left a place in your sketch to fit a bug into later. With (lots of) experience, seeing something like that in your code will make you uneasy about it, even if it doesn't matter now.