0

I am attempting to capture the high and low temperature and humidity values, then clearing the LCD and displaying them on 2 successive screen updates. All is working correctly except that the variables for high and low are updating on every loop to the current sensor pin read after the "if" statements are run. Why is this occurring? Also note that I declare the floats for the highs and lows to the unlikely value of 10000 in order to change them all to the initial pin read on the 1st loop as I didn't find a method to determine null on the initial loop. I would appreciate any suggestions for a better method! TIA

    #include <dht.h>
    #include <LiquidCrystal.h>

    //Low and High Temp and Humidity Variables
    float lTemp=10000;
    float hTemp=10000;
    float lHumid=10000;
    float hHumid=10000;

    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

    dht DHT;

    #define DHT11_PIN 7

    void setup(){
      lcd.begin(16, 2);
      Serial.begin(9600);
    }


    void loop(){
       lcd.clear();

      int chk = DHT.read11(DHT11_PIN);

      float CurrTemp = DHT.temperature * 1.8 + 32;  //Converts celsius read from DHT read to farenheit

    //Sets High and Low Temperature Readings

       if (lTemp = 10000) {
         lTemp = CurrTemp;
       }
      if (CurrTemp < lTemp) {
         lTemp = CurrTemp;
      }

      if (hTemp = 10000) {
        hTemp= CurrTemp;
      }
       if (CurrTemp > hTemp) {
        hTemp = CurrTemp;
      }

    //Sets High and Low Humidity Readings

      if (lHumid = 10000) {
        lHumid = DHT.humidity;
       }
      if (DHT.humidity < lHumid) {
        lHumid = DHT.humidity;
      }

      if (hHumid = 10000) {
        hHumid = DHT.humidity;
      }
      if (DHT.humidity > hHumid) {
        hHumid = DHT.humidity;
      }

      //Prints Current Temp and Humidity to LCD and Serial Monitor
      lcd.setCursor(0,0); 
      lcd.print("Temp: ");
      lcd.print(CurrTemp,1);
      lcd.print((char)223);
      lcd.print("F");

      Serial.print(CurrTemp,1);

      Serial.println(" F");

      lcd.setCursor(0,1);

      lcd.print("Humidity: ");
      lcd.print(DHT.humidity,0);
      lcd.print("%");

      Serial.print("Humidity: ");
      Serial.print(DHT.humidity,0);
      Serial.println("%");
      Serial.println("");

      delay(10000);

      //Prints Low and High Temp to LCD and Serial Monitor
      lcd.clear();

      lcd.setCursor(0,0); 
      lcd.print("L Temp: ");
      lcd.print(lTemp,1);
      lcd.print((char)223);
      lcd.print("F");

      Serial.print("Low Temp: ");
      Serial.print(lTemp,1);
      //Serial.print((char,223);
      Serial.println(" F");

      lcd.setCursor(0,1);

      lcd.print("H Temp: ");
      lcd.print(hTemp,1);
      lcd.print((char)223);
      lcd.print("F");

      Serial.print("High Temp: ");
      Serial.print(hTemp,1);
      Serial.println(" F");
      Serial.println("");

      delay(3000);

     //Prints Low and High Humidity to LCD and Serial Monitor
      lcd.clear();

      lcd.setCursor(0,0); 
      lcd.print("Low Humid: ");
      lcd.print(lHumid,0);
      lcd.print("%");

      Serial.print("Low Humid: ");
      Serial.print(lHumid,0);
      Serial.println("%");

      lcd.setCursor(0,1);

      lcd.print("High Humid: ");
      lcd.print(hHumid,0);
      lcd.print("%");

      Serial.print("High Humid: ");
      Serial.print(hHumid,0);
      Serial.println("%");
      Serial.println("");

      delay(3000);

    }
1
  • you have a small logic error in your code ... unnecessary code actually ... in if (lTemp == 10000) { set lTemp, hTemp, lHumid and hHumid ... there is no need for if (hTemp == 10000) { or if (lHumid == 10000) { or if (hHumid == 10000) {
    – jsotola
    Nov 24, 2020 at 4:48

3 Answers 3

2

You have this or similar a number of times:

  if (lTemp = 10000) {
     lTemp = CurrTemp;
   }

This is incorrect. The == operator is used for comparison.

Try:

  if (lTemp == 10000) {
     lTemp = CurrTemp;
   }

The way you have it, the lTemp variable is assigned 10000 every time the statement is executed. It's not doing the comparison as you intend.

3
  • Turning up the compiler warning level File/Preferences will show a useful message pointing to this in the code.
    – timemage
    Nov 24, 2020 at 2:39
  • Thank you! I knew that but couldn't see it. Works great now.
    – Mav
    Nov 24, 2020 at 2:53
  • 1
    @Mav If this was the answer to your question, please accept it as an answer. Thanks.
    – jwh20
    Jan 8, 2021 at 11:39
2

When programming in C, the "=" assigns a value and the "==" tests a value. So in this line of code:

if (lTemp = 10000)

... you are actually assigning variable lTemp the value of 10000. Consider changing the line of code to this:

if (lTemp == 10000)

... this will test if the variable lTemp is equal to 10000.

3
  • Turning up the compiler warning level File/Preferences will show a useful message pointing to this in the code.
    – timemage
    Nov 24, 2020 at 2:39
  • Thanks much! I appreciate your time and knowledge.
    – Mav
    Nov 24, 2020 at 3:00
  • @Mav where you found their answers useful, you can upvote them.
    – timemage
    Nov 24, 2020 at 3:12
2

Although you already have two perfectly valid answers, I will share a trick that completely sidesteps the problem. Instead of choosing a particular number (here, 10,000) as a “magic” value that has a special meaning, you can instead initialize the highs and lows in a way that ensures they will be updated on the very first iteration:

  • make lTemp higher than any temperature you may ever read
  • make hTemp lower than any temperature you may ever read

For example:

float lTemp = +INFINITY;
float hTemp = -INFINITY;

and the same of course for the humidities. This way the first iteration does not need special handling anymore, as it becomes a regular iteration:

if (CurrTemp < lTemp) lTemp = CurrTemp;
if (CurrTemp > hTemp) hTemp = CurrTemp;

Note that the choice of ±∞ for the min and max of an empty set of readings is not just a dirty hack. It is actually consistent with the mathematical notion of infimum and supremum of the empty set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.