LCD Display problems

I am trying to get the display to display from the millis rather than using a delay so I can take constant data but I'm not sure how to wipe the screen without it wiping and rewriting the entire loop. `I am also running the Adafruit LCD Library with an I2C

#include <Adafruit_LiquidCrystal.h>

bool displayScreenOne = false;

const unsigned long eventInterval = 5000;
unsigned long previousTime = 0;
int soilMoisture = 0;
int thermometer = 0;
int photoresistor = 0;
int pumpOn = 0;

Adafruit_LiquidCrystal lcd_1(0);

  void setup()
  {
    lcd_1.begin(16, 2);
    pinMode(A0, INPUT);
    pinMode(13, OUTPUT);
    pinMode(A3, INPUT);
    pinMode(A1, INPUT);
  }

  void loop()
  {
    readValues();
    displayStuff();
  }

  void readValues()
  {
    soilMoisture = analogRead(A0);
    thermometer = analogRead(A1);
    photoresistor = analogRead(A2);
    pumpOn = digitalRead(13);
  }

  void displayStuff(){
    {
    if (millis() - previousTime >= eventInterval)
    {
      previousTime = millis();

      displayScreenOne = !displayScreenOne;
      if (displayScreenOne)
      {
        lcd_1.setCursor(0, 0);
        lcd_1.print("Temp: "); lcd_1.print(thermometer);
        lcd_1.setCursor(0, 1);
        lcd_1.print("Moisture: "); lcd_1.print(soilMoisture);
      }
      else
      {
        lcd_1.setCursor(0, 0);
        lcd_1.print("Light "); lcd_1.print(photoresistor);
        lcd_1.setCursor(0, 1);
        lcd_1.print("Pump: "); lcd_1.print(pumpOn);
      }
    }
  }
}

Hello eturrentine1

Welcome to the best Arduino Forum ever :slight_smile:

Describe your problems with the LCD display in detail.

Which Arduino is being used?

Thank you for the welcome!

I'm just getting started so pardon the lack of knowledge, but the project is an automatic watering system and as of right now I'm still just designing it in tinkerCAD.

I am an Agricultural Education teacher grades 8-12th and I'd love to have an upper level agricultural technology class within the next few years and i need to educate myself on the ins and outs before I can teach it!

It is on an Uno board with a thermometer, soil moisture sensor, an output to a relay for the water pump and a photoresistor (however this may be not needed in practical sense because I'll have a grow light, which will be on a timer rather than an 'as needed' basis). I want an LCD to display the information and have it switch between 2 data readings on each page every 5 seconds. I seem to have worked out some of the issues I was having just by adapting other codes, however I'm still unsure of what bool() does, just that it seems to work! An explanation on that would be so appreciated.

Also here is the updated code

#include <Adafruit_LiquidCrystal.h>

bool displayScreenOne = false;

const unsigned long eventInterval = 5000;
unsigned long previousTime = 0;
int soilMoisture = 0;
int thermometer = 0;
int light = 0;
int pump = 0;

Adafruit_LiquidCrystal lcd_1(0);

  void setup()
  {
    lcd_1.begin(16, 2);
    pinMode(A0, INPUT);
    pinMode(13, OUTPUT);
    pinMode(A2, INPUT);
    pinMode(A1, INPUT);
  }

  void loop()
  {
    readValues();
    displayStuff();
    soilMoisture = analogRead(A0);
    if (soilMoisture <= 150){
      digitalWrite (13, HIGH);
    }
    if (soilMoisture > 150){
      digitalWrite (13, LOW);
    }
  }

  void readValues()
  {
    soilMoisture = analogRead(A0);
    thermometer = analogRead(A1);
    light = analogRead(A2);

  }

  void displayStuff(){
    {
    if (millis() - previousTime >= eventInterval)
    {
      previousTime = millis();

      displayScreenOne = !displayScreenOne;
      if (displayScreenOne)
      {
        lcd_1.setCursor(0, 0);
        lcd_1.print("Temp: "); lcd_1.print(thermometer);
        lcd_1.setCursor(0, 1);
        lcd_1.print("Moisture: "); lcd_1.print(soilMoisture);
      }
      else
      {
        lcd_1.clear();
        lcd_1.setCursor(0, 0);
        lcd_1.print("Light: "); lcd_1.print(light);
        lcd_1.setCursor(0, 1);
        lcd_1.print("Pump: ");
 if (soilMoisture <= 150) { lcd_1.print("ON");} 
if (soilMoisture > 150) { lcd_1.print("OFF");}
      }
    }
    }
  }
  

    

Take a view into this textbook to gain the knowledge:

https://www.learncpp.com/cpp-tutorial/boolean-values/

or

        lcd_1.print("Temp :")
        lcd_1.print("Moist:");
        lcd_1.print("Light:");
        lcd_1.print("Pump :");
  1. Ensure the above four lines have the same number of characters.
  2. Remove lcd_1.clear();
  3. Set cursor to 0,0 to print temp or light
  4. Set cursor to 0,1 to print moist or pump
  5. Set cursor to (13, x) or (7, x) to print data at the end of the words.

Thanks!
That makes the display look so much cleaner!

  • Always, always start your projects by drawing a schematic showing all your component interconnections.
    This is the language we use to communicate in hardware discussions.

  • Do you know what it means when we assign a type to a variable ?

Number 'type's.

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0 to 255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0 to 65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
  • float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We'll touch on this later. Sparkfun.
  • You select the 'type' best suited for your variables.

ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. char joystick;
  • if your variable is used for ASCII then you need type 'char', char myText[ ] = "Raspberry Pie Smells";
  • if your variable enables some code then boolean can be used. boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, unsigned long currentTime = millis();
    etc.

BTW
These lines of code are unnecessary.

    pinMode(A0, INPUT);
    pinMode(A2, INPUT);
    pinMode(A1, INPUT);

there is no bool() function in your code but you have a

bool displayScreenOne = false;

that is a boolean variable. displayScreenOne can have two values, either true or false.
In the code you use that variable in the function displayStuff() to display either the screen one or the other screen.

displayScreenOne = !displayScreenOne;

inverts the value stored in the variable. If the value was true it gets false, if the value was false it gets true.

P.S.: please prettify your code and press CTRL-T in your IDE before you show it your kids.

P.S.: read how if else is working in c++ to avoid things like:

 if (soilMoisture <= 150) { lcd_1.print("ON");} 
if (soilMoisture > 150) { lcd_1.print("OFF");}

P.S.: usually people don't hardcode the pin numbers, they use constant variables or expressions to represent a pin number and use that in the code.

e.g.

  // as global:
  constexpr uint8_t soilPin {A0};

  // and later in your code
  soilMoisture = analogRead(soilPin); 

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.