[SOLVED] [Newbie] LCD 16x2 text display code problem

Hello!

I'm a newbie to Arduino and therefore my code might be entirely wrong or my mistake too stupid. Please help me!

With the below code, the LCD display doesn't seem to be changing the point where text is displayed from on the screen and all the text churns out as one line, pushing the rest away until the entire screen is filled with text.

#include <LiquidCrystal.h>
#include <DHT.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTPIN 6
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  lcd.clear();
  lcd.setCursor(0, 2);
  lcd.print("Humid: ");
  lcd.print(h, DEC);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(t, DEC);
  lcd.print("C");
  delay(1000);

  lcd.clear();
  lcd.setCursor(0, 2);
  lcd.print("Heat Index");
  
  lcd.setCursor(0, 1);
  lcd.print("HI: ");
  lcd.print(hic, DEC);
  lcd.print("C");
  delay(500);

  lcd.setCursor(0, 2);
  lcd.print("Taking reading!");

  //DHT needs 2 seconds to read
  //2000/10 = 200
  //Add some animations for aesthetic
  lcd.setCursor(0, 1);
  //16 chars
  //[x/xx]..........
  lcd.print("[0/10]");
  delay(200);
  lcd.print("[1/10].");
  delay(200);
  lcd.print("[2/10]..");
  delay(200);
  lcd.print("[3/10]...");
  delay(200);
  lcd.print("[4/10]....");
  delay(200);
  lcd.print("[5/10].....");
  delay(200);
  lcd.print("[6/10]......");
  delay(200);
  lcd.print("[7/10].......");
  delay(200);
  lcd.print("[8/10]........");
  delay(200);
  lcd.print("[9/10].........");
  delay(200);
  lcd.print("[10/10]..........");
}

How do I fix it so that the text doesn't display incorrectly?
Thanks!

P.S The readings from the sensor are displayed as long, unrounded values with lots of decimal places. Anyway to fix this? (This is one of my stupid mistakes that I'm not sure how to fix)

You really should start out with just one set of data and get that to display as you would like it. Try to reduce your code to the smallest amount that will still display the problem that you are trying to fix.

There is one basic problem that I can see right away. You are dealing with a display that has two rows of characters yet in several places you are setting your cursor to the third row. Remember that in many cases, this is one of them, counting begins with 0, not 1.

Don

Hi.

"all the text churns out as one line", because that is exactly what you told your sketch to do.
You aren't resetting the cursor after putting your counter on the screen,of before.
Snip:

  //DHT needs 2 seconds to read
  //2000/10 = 200
  //Add some animations for aesthetic
  lcd.setCursor(0, 1);
  //16 chars
  //[x/xx]..........
  lcd.print("[0/10]");
  delay(200);
  lcd.print("[1/10].");
  delay(200);

That will print [0/10][1/10] and so on.
I think you want to replace [0/10] by [1/10] and so on.
If so, you need to reset the position of the cursor each time before the next step is printed.

Snip:

  lcd.print("[0/10]");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[1/10].");
  delay(200);

Something like that.
If this is not your problem, i don't know what is.

Thanks so much for the replies. I'll fix my code and post it soon.

Hello people,
I've fixed the code a lot, it works swell and I've been able to add more sensors for more data to be displayed on the 16x2.

Thanks so much for your help to this newbie.

Fixed code:

//Development of a water quality sensor
//Measuring environmental conditions with a DHTxx (IMP)
//Measuring turbidity with a photodiode (U-IMP)
//Measuring water level with a SR-04 (U-IMP)

//CHANGELOG - CURRENT VERSION Demov4
//Demov1 Implemented LCD 16x2 setup and tested text
//Demov2 Implemented DHT22 for a demo
//Demov3 Debugged most of demo code
//Demov4 Added SR-04 code to measure water level and debugged DHT code. Oops.
//Soon to implement turbidity sensor 

//Common LCD screen driver
#include <LiquidCrystal.h>
//DHT Lib from Adafruit
#include <DHT.h>
//Init LCD lib with pins declared
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Custom character
byte plusminus[8] = {
  B00100,
  B00100,
  B11111,
  B00100,
  B00100,
  B00000,
  B11111,
};
//Don't conflict sensor w/ screen pins else mojibake is displayed
//Vars can be changed based on sensor used. DHT11 as demo used.
#define DHTPIN 6
#define DHTTYPE DHT11
//Defining pins for SR-04 sensor 
#define trigPin 9
#define echoPin 8

//init DHT lib
DHT dht(DHTPIN, DHTTYPE);


void setup() {
  //Init lcd screen
  lcd.begin(16, 2);
  //Init custom character
  lcd.createChar(0, plusminus);
  //Init DHT comms
  dht.begin(); 
  //Init SR-04 Pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
}

void loop() {
  //Grab values from DHT & SR-04
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float hic = dht.computeHeatIndex(t, h);
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  //clear LCD 16x2 before entering text
  //Forum : count screen rows 0-1 not 1-2
  //Forum : call lcd.setCursor(x, x) before text to display properly
  //Humidity & Temp displayed in Celsius
  //Used round function because raw value has too many dps
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Humid: ");
  lcd.print(round(h));
  lcd.print("% ");
  lcd.write(byte(0));
  lcd.print("5%");

  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(round(t));
  lcd.print("C ");
  lcd.write(byte(0));
  lcd.print("2");
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Heat Index");
  
  lcd.setCursor(0, 1);
  lcd.print("HI: ");
  lcd.print(round(hic));
  lcd.print("C");
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Water Level");
  
  lcd.setCursor(0, 1);
  //The value below can be edited to max depth @ sensor
  //[DemovX] Assume 4m for demo
  lcd.print(round(distance));
  lcd.print("cm / 400cm");
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Taking reading!");

  //DHT needs 2 seconds to read
  //2000/10 = 200
  //Add some animations for aesthetic
  //16 chars
  //[x/xx]..........
  lcd.setCursor(0, 1);
  lcd.print("[0/10]");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[1/10].");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[2/10]..");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[3/10]...");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[4/10]....");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[5/10].....");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[6/10]......");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[7/10].......");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[8/10]........");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[9/10].........");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print("[10/10]..........");
}

Good for you.

It's about time for you to learn how to dump delay().
See my signoff message below.