16*2 I2C LCD Scrolling Message Warnings

Hi, I'm beginner on arduino projects. Just create a program to print scrolled message. It works well, however I see some Chinese characters at the end of the first line and on the second line of LCD as well.

I also get some warnings, as well. Can you comment on what could be the reason?

Below you can find the program and the warning messages during compilation.

PROGRAM
/***********************************************************

  • Description: LCD1602 display a string "Hello Gunay!" scrolling,

***********************************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to "0x27") for a 16 chars and 2 line display
char array1[]="Hello Gunay! "; //the string to print on the LCD

int tim = 250; //the value of delay time
void setup(void)
{
lcd.init(); //initialize the lcd
lcd.backlight(); //turn on the backlight

}
void loop(void)
{
lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner
lcd.setCursor(16,0); // set the cursor to column 16, line 1
for (int positionCounter2 = 0; positionCounter2 < 30; positionCounter2++)
{
lcd.scrollDisplayLeft(); //scrolls the contents of the display one space to the left.
lcd.print(array1[positionCounter2]); // Print a message to the LCD.
delay(tim); //wait for 250 microseconds
}

lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner.

}


WARNING MESSAGES
D:\OneDrive\KARIŞIK\Projeler\Arduino\LCD Control 16_2\LCD_Scrolling_Message\LCD_Scrolling_Message.ino: In function 'loop':

D:\OneDrive\KARIŞIK\Projeler\Arduino\LCD Control 16_2\LCD_Scrolling_Message\LCD_Scrolling_Message.ino:25:16: warning: iteration 18 invokes undefined behavior [-Waggressive-loop-optimizations]

D:\OneDrive\KARIŞIK\Projeler\Arduino\LCD Control 16_2\LCD_Scrolling_Message\LCD_Scrolling_Message.ino:22:53: note: containing loop

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\main.cpp: In function 'main':

D:\OneDrive\KARIŞIK\Projeler\Arduino\LCD Control 16_2\LCD_Scrolling_Message\LCD_Scrolling_Message.ino:25:16: warning: iteration 18 invokes undefined behavior [-Waggressive-loop-optimizations]

D:\OneDrive\KARIŞIK\Projeler\Arduino\LCD Control 16_2\LCD_Scrolling_Message\LCD_Scrolling_Message.ino:22:53: note: containing loop

Sketch uses 2810 bytes (8%) of program storage space. Maximum is 32256 bytes.
Global variables use 261 bytes (12%) of dynamic memory, leaving 1787 bytes for local variables. Maximum is 2048 bytes.

Can you comment on what could be the reason?

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to "0x27") for a 16 chars and 2 line display
    for (int positionCounter2 = 0; positionCounter2 < 30; positionCounter2++)
    {
      lcd.scrollDisplayLeft();             //scrolls the contents of the display one space to the left.
      lcd.print(array1[positionCounter2]); // Print a message to the LCD.
      delay(tim);                          //wait for 250 microseconds
    }

Can you sensibly scroll left 30 times on a display that is 16 characters wide ?

You are looping through an 18-element array (array1) 30 times (I count 18 elements; I suspect the compiler is numbering the iterations starting from 0 not 1, hence iteration 18 would be the first one that tries to read off the end of the array). This will result in it printing to the LCD whatever happens to be after it in the memory, which is not defined by the C standard - it could be anything, and changing something elsewhere in your program will change what happens to be there.

Never read or write off the end of an array - either declare the array of size such that that your code won't loop off the end, or check the actual length, rather than 30 in the loop. .

Also, delay() takes the delay time in milliseconds, your comment described it as microseconds; I suspect you do mean milliseconds, but - it doesn't match your comment. To do a delay for a number of microseconds, use delayMicroseconds(). Note that the I2C write probably takes more than 250 microseconds.

@ukhelibob - I think the idea is to make the given text scroll across the LCD screen.... That part is fine. Reading elements 18~29 of a 18 element array - not so much.