I want tried to make a programm to display a text on an I2C LCD-Display. The text should be scrolled for one position every time the function is called.
On the internet I saw a lot of solutions but there were all diffrent from what I need.
So I tried to make my own code. The code works well except of the part where I try to scroll in the Text from the left.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
char Str[12] = {'T', 'r', 'u', 'e', 't', 'z', 's', 'c', 'h', 'l', 'e', 'r'};
int pos = 0;
int posretry = 0;
bool ledon = false;
void setup()
{
pinMode(13, OUTPUT);
lcd.init();
lcd.backlight(); //lcd.noBacklight();
}
void scroll()
{
for (int i = 0; i < 12; i++)
{
//Print text
lcd.setCursor(i + pos, 0);
lcd.print(Str[i]);
//Move in text from the left
if (pos >= 6)
{
lcd.setCursor(i + pos, 0);
lcd.print(Str[i]);
lcd.setCursor(posretry, 0);
lcd.print(Str[11 - posretry]);
posretry++;
}
}
delay(500);
lcd.clear();
pos++;
}
void changeled()
{
if (ledon == false)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
ledon = !ledon;
}
void loop()
{
scroll();
changeled(); //Example Event
}
The goal is to move the text to the right and show the not displayed letters on the left.
Example:
|er Truetzschl| -->
| |
A Video about the error can be found here: https://data.jonas-heinze.de/shares/stackexchange/lcd/
Thanks for your help.
1 Answer 1
You do some strange things that I, at least, don't understand (e.g. I don't see what you need posretry
for), but you might have a reason for doing so.
But you don't reset posretry
and pos
, which means they will very soon be out of a reasonable range. At some point the int
will actually overflow to negative values, which is definitely not going to work!
To make it less complicate, you should write a function that prints the char array shifted by a desired value as a beginning.
The below code is completely untested as I don't have your hardware and I'm aware that it does not include all your desired features. But it should give you an idea of how scrolling can be implemented in principle and it's up to you to improve/fix it.
#define LCD_SIZE 16
#define STR_LEN 12
char str_to_print[STR_LEN]={'T', 'r', 'u', 'e', 't', 'z', 's', 'c', 'h', 'l', 'e', 'r'};
void print_scrolled (uint8_t scrolled_by) {
for (uint8_t i=0;i<LCD_SIZE;i++) {
lcd.set_cursor(i,0);
if (scrolled_by>=LCD_SIZE) scrolled_by=0;
if (scrolled_by<STR_SIZE) lcd.print(str_to_print[scrolled_by]);
else lcd.print(' ');
scrolled_by++;
}
}
You could call it like this:
for (uint8_t i=0;i<STR_LEN;i++) {
print_scrolled(i);
delay(500);
}
Once you are familiar with my approach you should be able to enhance it with additional features (end of text coming in from the other side, spaces between end and beginning, etc).
-
Thank you very much. I will do some testing tomorrow. Have a nice dayJonas Heinze– Jonas Heinze2020年05月03日 15:32:36 +00:00Commented May 3, 2020 at 15:32
if(pos>=6)
? Do you really needposretry
(for what)? A serious problem is that you never resetpos
andposretry
, so they will exceed the allowed range and even become negative when the int overflows itself. Definitely not right