0

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.

8
  • What doesn’t work about it? You didn’t give us a symptom. You did describe your expected behavior so good job on that. Now you need to say what actually happens and how that’s different from your expectations.
    – Delta_G
    Commented May 3, 2020 at 13:55
  • Oh im so sorry I forget to describe the problem. I added a video of the problem to my post. Commented May 3, 2020 at 14:16
  • OK, I would have helped if you could have described it, but I can't see the video on this device. Good luck.
    – Delta_G
    Commented May 3, 2020 at 14:18
  • A diffrent Text seems to randomly shown up. Sometimes in the first other times in the second row. The often are strange characters. Its hard to explain. Cause of that I added the Video. Commented May 3, 2020 at 14:26
  • Hmm, should be an easy task where you just need to increment a string index and handle its overflow. What do you achieve by checking if(pos>=6)? Do you really need posretry (for what)? A serious problem is that you never reset pos and posretry, so they will exceed the allowed range and even become negative when the int overflows itself. Definitely not right
    – Sim Son
    Commented May 3, 2020 at 14:42

1 Answer 1

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).

1
  • Thank you very much. I will do some testing tomorrow. Have a nice day Commented May 3, 2020 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.