简体   繁体   中英

LCD Arduino not displaying correctly

My problem is when I input the string in the serial monitor it shows like this:

LCD Arduino Error The setCursor dont work and also there is another weird character created before the actual output.

This is my sample code:

void setup() {

    lcd.begin(16, 2);
    Serial.begin(9600);
    lcd.print("hello, world!");
}

void loop() {

    String readString;
    String Q;
    while (Serial.available()) {
        delay(1);
        if (Serial.available()>0) {
            char c = Serial.read();
            if(isControl(c)){
                break;
            }
            readString += c;
        }
    }
    Q = readString;
    if (Q == "1"){
        lcd.setCursor(0,1);
        lcd.print("Hello");
    }
    if (Q == "2"){
        lcd.setCursor(0,1);
        lcd.print("World");
    }
}

First of all you should understand the LCD lib functions.
To set the cursorto theFirst row you need

 lcd.setCursor(0,0);  // row index starts with 0

if you only set the cursor back without clearing the screen there might be weird chars,sodo a

 lcd.clear(); //clears the whole screen

OR define an empty String:

 String lineClear ="                ";  // should be 16 spaces for a 16x2 display

and do as a clearing sequence (eg for the top line)

 lcd.setCursor(0,0);
 lcd.print(lineClear);
 lcd.print("Hello");

Remember the syntax is

 lcd.setCursor(col, row)  
 // index for 16x2 is col 0-15,row 0-1 
 // index for 20x4 is col 0-19,row 0-3 

and in setup alwas do a

lcd.clear(); 

after initializing the lcd, to remove possible artefacts from the buffer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM