Static vs Variable text displayed on LCD

Hi All,

Trying to get my head around some Coding. My aim is to use Arduino to do the following:

  1. Switch on a Heater when the Temp drops below a certain value. Also operating a relay and LED and report ON after the text “Heater:”. Otherwise shows OFF
  2. Switch on a Mister via a Solenoid when the Temp is above a certain value. Also operating a relay and LED and report ON after the text “Mister:”. Otherwise shows OFF

So I’ve connected a Temperature probe and it’s reporting the temperature to the Serial Monitor and the LCD.

However, every time the new temperature is reported (after the delay of 5000), all the text is replaced so it looks like a screen flicker.

For now, I would love to have all the text fixed or static and ONLY the 4 digit temperature reading to update every minute or so. I’ll play with the LED and Relay Codes after getting this sorted.

Here is my code. (Still needs to be tidied up and more LED’s and Output Relays added):

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header



// which analog pin to connect
#define THERMISTORPIN A0         
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000      
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25   
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000  


hd44780_I2Cexp lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display

int samples[NUMSAMPLES];

void setup(void) {
  Serial.begin(9600);
  analogReference(EXTERNAL);
}

void loop(void) {
  uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(THERMISTORPIN);
   delay(10);
  }
  
  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples[i];
  }
  average /= NUMSAMPLES;

  Serial.print("Average analog reading "); 
  Serial.println(average);
  
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;
  Serial.print("Thermistor resistance "); 
  Serial.println(average);
  
  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  
  Serial.print("Temperature "); 
  Serial.print(steinhart);
  Serial.println(" *C");

lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  

 
  lcd.setCursor(0,0);
  lcd.print("AVIARY TEMP CONTROL");
  
  lcd.setCursor(3,1);
  lcd.print("Temp  : ");
  lcd.print(steinhart);
  lcd.print(" "); //This put space after degrees reading
  lcd.print((char)223); //This add small 0 in degrees to text
  lcd.print("C"); 

  lcd.setCursor(3,2);
  lcd.print("Heater: ");

  lcd.setCursor(3,3);
  lcd.print("Mister: ");
 

  delay(50000);
  
}

I’m not too concerned about processing speed or the getting the code to a professional standard. This is a once off project, so if it’s functional, I’d be more than happy.

Many thanks in advance for your advice and support.

You do this in setup(), not in the loop():

lcd.init() ;

If you want to clear the screen, use lcd.clear().

owever, every time the new temperature is reported (after the delay of 5000), all the text is replaced so it looks like a screen flicker.

don't delete the full display, just update the variable, e.g.:

set the cursor to the starting position to write to,
write blanks (to "delete" the rest of the line/the old value)
set your cursor to the starting position to write to
write your variable.

Thanks for your replies.

Coding is still Greek to me, so I'm not even too sure what I'm looking at. Happy to search up what you mean by "cursor", "setup" and "loop" so I can try and modify my code. What I have here already was copied from examples and slightly modified what made sense to me. :slight_smile:

Any idea where I can read up about this or find a good example?
(I'm not expecting to be spoon fed and given the correct code)

Thanks.

aussie_perth:
Any idea where I can read up about this or find a good example?
(I'm not expecting to be spoon fed and given the correct code)

for shure: it's called "reference"

https://www.arduino.cc/en/Reference/LiquidCrystalSetCursor

and here is some overview: https://www.arduino.cc/en/Reference/LiquidCrystal

and as you are using the library from bill I'm confident, there are lot of examples in the IDE included

:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

You beauty!!!

After more hours of playing with it, I finally managed to get the Code for static text under Setup() and the varying Temperature Code under the Loop() section.

Thanks for the pointers guys.
Now I will try to incorporate the rest of the programming.

A few things about the hd44780 library.
while a constructor like:

hd44780_I2Cexp lcd(0x27,20,4);

Along with using lcd.init() do work, it isn't the "normal" nor the preferred way of using the API.
In fact, they are not documented.
I added those API interfaces as an attempt to help people get up and going, that are trying to use API interfaces from the LiquidCrystal_I2C library.
While they do work, I would recommend using the normal/preferred API interfaces as it makes the code more portable across other lcd h/w and other libraries like the standard LiquidCrystal library.

The examples that come with the hd44780 library for the hd44780_I2Cexp i/o class show how to initialize the library.
Take a look at the hd44780_I2Cexp i/o class HelloWorld sketch.

Unless you have a specific reason to specify the i2c address of the LCD backpack, the easiest way to use the hd44780_I2Cexp i/o class is to let the library discover the i2c address.
I would recommend using a constructor like you see in the hd44780_I2Cexp examples:

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

And then in your setup() routine call begin(LCD_ROWS, LCD_COLS) to initialize the device.
And as you see in the comment in the HelloWorld sketch, the backlight will automatically be turned on when begin() successfully returns.

The hd44780 library comes with extensive documentation. You can access it through the Documentation sketch.

--- bill