Re: I2c based LCD to print in 2nd row through serial

Hy,

wishing good day.

Can i get the i2c based solution for this below problem.

https://forum.arduino.cc/index.php?topic=114434.0

here is my i2c based code .

Am unable to print in 2nd row .

#include <LiquidCrystal_I2C.h>  
#include <Wire.h>

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

void setup()
{
 Serial.begin(9600);
 lcd.begin();
 lcd.backlight();
}

void loop()
{
 char inSerial[5];
 char dis[20];  
 int i=0;
 delay(1000);
 
 if (Serial.available() > 0)
 {            
      while (Serial.available() > 0) {
        inSerial[i]=Serial.read(); //read data  
        i++;      
      }
      inSerial[i]='\0';
     Check_Protocol(inSerial);
   }    
}


void Check_Protocol(char inStr[])
{  
 Serial.print("Command: ");
 Serial.println(inStr);
     
if(strcmp(inStr,"DISPLAY0\n")==0)
 {
  lcd.backlight();
  delay(1000);
  lcd.clear();                                          
  Serial.println("LCD Cleared");
 }
 else if(strcmp(inStr,"DISPLAY1\n")==0)
 {
  display1();
 }
 else if(strcmp(inStr,"DISPLAY2\n")==0) 
 {
  display2();
 }
 else if(strcmp(inStr,"DISPLAY3\n")==0) 
 {
  display3();
 }
 else if(strcmp(inStr,"DISPLAY4\n")==0) 
 {
  display4();
 }
}

void display1()
{
  Serial.println("DISPLAY1 function called");
  delay(5000);
  if (Serial.available())
  {
  delay(100);
  lcd.clear();
  while(Serial.available() > 0)
  {
  for(int i=22;i<=39;i++)
  {
  delay(200);
  lcd.setCursor(i,2);
  lcd.backlight();
  lcd.print((char)Serial.read());
  delay(100);
  }
  }
  }
 }
void display2()
{
  Serial.println("DISPLAY2 function called");
  lcd.backlight();
  delay(1000);
  lcd.setCursor(3,1);
  lcd.print("ACD-Main_Tester");
  delay(1000);
}
void display3()
{
  Serial.println("DISPLAY3 function called");
  lcd.backlight();
  delay(1000);
  lcd.setCursor(4,2);
  lcd.print("Revision 0.0");
  delay(1000);
}
void display4()
{
  Serial.println("DISPLAY4 function called");
  lcd.backlight();
  delay(1000);
  lcd.setCursor(4,3);
  lcd.print("Version 0.0");
  delay(1000);
}

Does the code ever enter the display2() function ?
Can you print on the 2nd line when running a simple "Hello World" sketch and the same hardware ?

Yes , i can write to 2nd line by setting cursor.

lcd.setCursor(3,1);
lcd.print("ACD-Main_Tester");

but i can't able to set cursor for serial data as i can do for 1st line .

Does the code ever enter the display2() function ?

UKHeliBob:

yes it enters to display2() function when i give command DISPLAY2

What happens if you temporarily change the display2() function to print on a different row ?

Look at the bottom of the serial monitor window, do you have the line ending set to Newline, and not NL & CR?

I'd be surprised if the sketch works at all, you are only allocating enough room in inSerial to hold four characters plus the terminating null, not enough space to hold the text you are expecting.