LCD code-related errors when compiling sketch

In trying to do the project in the above book on the water level sensor, I got the following error.

Sketch

`// include the library code:
#include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int resval = 0; // holds the value
int respin = A5; // sensor pin used
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop() {
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to
resval variable
if (resval<=100){ lcd.println("Empty "); } else if (resval>100 &&
resval<=300){ lcd.println("Low "); } else if (resval>300 && resval<=330){
lcd.println("Medium "); } else if (resval>330){lcd.println("High");}
delay(1000);
}

Error Message

/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino: In function 'void setup()':
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:8:1: error: 'lcd' was not declared in this scope
lcd.begin(16, 2);
^~~
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino: In function 'void loop()':
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:14:1: error: 'lcd' was not declared in this scope
lcd.setCursor(0, 1);
^~~
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:16:8: error: expected ';' before 'variable'
resval variable
^~~~~~~~
/tmp/.arduinoIDE-unsaved202403-1912-ul2rp2.xmbq/sketch_jan3a/sketch_jan3a.ino:17:44: error: 'else' without a previous 'if'
if (resval<=100){ lcd.println("Empty "); } else if (resval>100 &&
^~~~

exit status 1

Compilation error: 'lcd' was not declared in this scope

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like
//initialise the library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Where is the code that this comment refers to ?. From the error message saying that 'lcd' has not been declared I assume that it should have been declared in that code

Another error

resval variable

This line of code should be on the previous line as part of the comment

1 Like

It got stuck in the comment (one too many backspaces)

Change this line:

//initialise the library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);

to these two lines...

//initialise the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
1 Like

I have tided up the code and it compiles but I cannot test it

// include the library code:
#include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int resval = 0;   // holds the value
int respin = A5;  // sensor pin used
void setup()
{
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("WATER LEVEL: ");
}
void loop()
{
    // set the cursor to column 0, line 1
    lcd.setCursor(0, 1);
    resval = analogRead(respin);  //Read data from analog pin and store it to resval variable
    if (resval <= 100)
    {
        lcd.println("Empty ");
    }
    else if (resval > 100 && resval <= 300) { lcd.println("Low "); }
    else if (resval > 300 && resval <= 330)
    {
        lcd.println("Medium ");
    }
    else if (resval > 330) { lcd.println("High"); }
    delay(1000);
}

NOTE THE USE OF CODE TAGS

1 Like

As long as the sensor range is < 100 to > 330...
water

2 Likes

I was able to upload the sketch to my board all right but then nothing appeared on the display. Not the water level line, nor any values. I'm enclosing a photo of my setup because I wonder if the picture in the book and the code don't match up. Thanks again for your all your help.

You actually need to set up the correct address for the display, similar to "LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address old display".

There is a sample program with your IDE that will find and display the address of your diaplay.

1 Like

Yes. I don't know what all the extra parameters in the example are for, usually it's simple like

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

The program @moecat might use to verify the address and see if the display is hooked up correctly is called an i2c scanner and google will turn up a few variations.

But at least you do have to use the code that is correct for an I2C display as opposed to the older fashioned so-called four or eight wires interface.

And of course not bungle copy pasting the code so critical elements get absorbed by comments…

a7

But you have not even included an address for the display.

Sorry, I believed the comment that was in the line of code that I cut and pasted from an example of getting an I2C LCD display to function.

// set the LCD address to 0x27 for a 20 chars and 4 line display

so I did not look further for any address to include.

What should this be?

LiquidCrystal_I2C lcd(0x27,20,4);

a7

// include the library code:
#include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(A4, A5);
int resval = 0; // holds the value
int respin = A5; // sensor pin used
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop()
{
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to resval variable
if (resval <= 100)
{
lcd.println("Empty ");
}
else if (resval > 100 && resval <= 300) { lcd.println("Low "); }
else if (resval > 300 && resval <= 330)
{
lcd.println("Medium ");
}
else if (resval > 330) { lcd.println("High"); }
delay(1000);
}

Error Message

/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino:4:1: error: 'LiquidCrystal_I2C' does not name a type; did you mean 'LiquidCrystal_h'?
LiquidCrystal_I2C lcd(A4, A5);
^~~~~~~~~~~~~~~~~
LiquidCrystal_h
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino: In function 'void setup()':
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino:10:5: error: 'lcd' was not declared in this scope
lcd.begin(16, 2);
^~~
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino: In function 'void loop()':
/tmp/.arduinoIDE-unsaved202403-1781-6jo9zf.onhmv/sketch_jan3a/sketch_jan3a.ino:17:5: error: 'lcd' was not declared in this scope
lcd.setCursor(0, 1);
^~~

exit status 1

Compilation error: 'LiquidCrystal_I2C' does not name a type; did you mean 'LiquidCrystal_h'?

Sorry, I was attempting to reply to the original OP posting of the code with NO address for the display.

OIC.

I just noticed that. If @moecat copied the patterns from a working example, perhaps there is a different library… the few I just looked at did not seem to have calls that would default to a certain I2C address, nor calls to use I2C pins other than the hardware I2C at A4 and A5.

@moecat please read #2 above before you post code anymore.

Have you gotten the display to work using code you did not write or mess with?

Where did you exactly get the library you are using?

Have you tried any I2C scanner as advised?


Gack! I just now see

Using A5 for both analog input and I2C signal SCL is not gonna go well. Move your sensor to an unused analog input pin.

It looks like your book was using an old fashioned interface and you have been left to struggle heroically converting it to I2C.

TBH liquid crystal displays can be a challenge, which is why the first stop always should be to,use code you did neither write nor mess with and get to "Hellish World!".

S'all downhill from there. :expressionless:

a7

It suddenly dawned on me when I saw the picture of the setup provided by xfpd, that this code actually applies to an older version of LCD display. So I tried wiring it up as in the picture, and using the corrected code provided by UKHeliBob, but I still got no display of the words "water level". What could I have missed? See the photo I provided of my setup.
Plus I'm wondering why the LCD only shows one row lit up. Shouldn't there be two rows? Thanks again for all the help.

That is just the way mine work when the I2C address is wrong. When initialized correctly, both lines show up.

I went back to my original project with the LCD to compare the code to the present project. I tried to copy the original code as much as possible, and it uploaded just fine. But it still won't display "Water Level" or any values.What is going on? Here is my code .

// include the library code:
#include <LiquidCrystal.h>
//initialise the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int resval = 0; // holds the value
int respin = A0; // sensor pin used
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("WATER LEVEL: ");
}
void loop()
{
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
resval = analogRead(respin); //Read data from analog pin and store it to resval variable
if (resval <= 100)
{
lcd.println("Empty ");
}
else if (resval > 100 && resval <= 300) { lcd.println("Low "); }
else if (resval > 300 && resval <= 330)
{
lcd.println("Medium ");
}
else if (resval > 330) { lcd.println("High"); }
delay(1000);
}

No code tags used again when posting your code I notice. Please edit your post and add them

1 Like

I followed the instuctions for creating code tags.

What I2C address does your LCD use?