Text control with If and Else issues [SOLVED]

Hi All

OK so I built a nice greenhouse controller, which works well, now I`m doing all the improvements, cos its fun playing with code.

So I`m using if / else statements to achieve different colours of text on my TFT display.

In my example code the first part prints the humidity level to the screen in white characters, if the humidity goes above 55% the display turns green, just as I wanted, then drops back to white colour when the humidity goes down.

Great, I did the same with temperature, so when the temp hit 30 C, the text would turn red, which worked as well.

Where I`m having issues, I also want a higher warning level so if the temp reaches 32 C, the red text would actually flash, indicating much too hot.

I have now managed to make that happen, at 32 C the display is flashing red, great, but before that if the temp reaches 30 rather than just having the display turn red, it flashing between red and white.

I have no idea why the display is flashing red and white, it should be solid red.

Any thoughts appreciated.

Cheers Alan

// Colour Change of text depending on temperature and humidity levels
  
  if (h >= 55)
  {
  tft.setCursor (180,24);
  tft.setTextSize (3);
  tft.setTextColor (GREEN,BLACK);
  tft.print (h); 
  tft.print (" %");
  }

  else
  {
  tft.setCursor (180,24);
  tft.setTextSize (3);
  tft.setTextColor (WHITE,BLACK);
  tft.print (h); 
  tft.print (" %");
  }


if (t >= 30)
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (RED,BLACK);
  tft.print (t); 
  tft.print (" C");
  }
  
  else
  
   {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (WHITE,BLACK);
  tft.print (t); 
  tft.print (" C");
   }
   
  if (t >= 32)
      
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (RED,BLACK);
  tft.print (t); 
  tft.print (" C");
  
  delay(200);

  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (BLACK,BLACK);
  tft.print (t); 
  tft.print (" C");
  
  }
  
  else
  
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (WHITE,BLACK);
  tft.print (t); 
  tft.print (" C"); 
  }

The way you’ve got your temperature tests set up, both else segments get executed if t < 30. At t >= 30 the first if gets executed but then immediately after, the following else gets executed as well.

HI DK

I don`t have a if t < 30.

Only reading higher than 30 should do something

Hello???

if (t >= 30) is false, what is t? It's less than 30! So the else gets run.

Next if (t >= 32), well it wasn't greater than 30 so it sure as sh?t isn't greater than 32 so the else clause gets run. OK?

When you do stuff like this it's useful to work it reverse, test the higher values first.

If t >= 32 do the hot stuff else if t >= 30 do the not so hot stuff else do the cool stuff

I`ll try that, thank you

I tried what you suggested

if (t >= 32)
      
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (RED,BLACK);
  tft.print (t); 
  tft.print (" C");
  
  delay(200);
 
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (BLACK,BLACK);
  tft.print (t); 
  tft.print (" C");
  }
  
 else
  
  { 
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (WHITE,BLACK);
  tft.print (t); 
  tft.print (" C");
   }
   
   
  if (t >= 30)
      
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (RED,BLACK);
  tft.print (t); 
  tft.print (" C");

  }
else
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (WHITE,BLACK);
  tft.print (t); 
  tft.print (" C"); 
  }

No difference, at 30 C the display flashed red and white

Your code should look like this:

if (t >= 32)
{
    tft.setCursor (180,80);
    tft.setTextSize (3);
    tft.setTextColor (RED,BLACK);
    tft.print (t);
    tft.print (" C");

    delay(200);

    tft.setCursor (180,80);
    tft.setTextSize (3);
    tft.setTextColor (BLACK,BLACK);
    tft.print (t);
    tft.print (" C");

}
else if (t >= 30)
{
    tft.setCursor (180,80);
    tft.setTextSize (3);
    tft.setTextColor (RED,BLACK);
    tft.print (t);
    tft.print (" C");
}
else
{
    tft.setCursor (180,80);
    tft.setTextSize (3);
    tft.setTextColor (WHITE,BLACK);
    tft.print (t);
    tft.print (" C");
}

I just tried this

if (t >= 32)
      
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (RED,BLACK);
  tft.print (t); 
  tft.print (" C");
  
  delay(200);

  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (BLACK,BLACK);
  tft.print (t); 
  tft.print (" C");
  }
  
 else if
   
   
  (t >= 30)
      
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (RED,BLACK);
  tft.print (t); 
  tft.print (" C");

  }
else
  {
  tft.setCursor (180,80);
  tft.setTextSize (3);
  tft.setTextColor (WHITE,BLACK);
  tft.print (t); 
  tft.print (" C"); 
  }

And yes it worked, cheers

I just looked up else if in Google, now it makes sense.

Cheers DK

Thanks for the karma.