Skip to content

Instantly share code, notes, and snippets.

@RC1140
Created August 8, 2012 22:01
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RC1140/3299197 to your computer and use it in GitHub Desktop.
Save RC1140/3299197 to your computer and use it in GitHub Desktop.
Arduino LCD Scrolling
#include <LiquidCrystal.h>
#include <string.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
char message[] = "This is some long message that will end up scrolling";
int previous = 0;
int pos = 0;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print(message);
}
void printLine(int refreshSeconds){
//Check if the current second since restart is a mod of refresh seconds ,
//if it is then update the display , it must also not equal the previously
//stored value to prevent duplicate refreshes
if((millis()/1000) % refreshSeconds == 0 && previous != (millis()/1000)){
previous = (millis()/1000);//Store the current time we entered for comparison on the next cycle
lcd.setCursor(0, 1);//Set our draw position , set second param to 0 to use the top line
char lcdTop[16];//Create a char array to store the text for the line
int copySize = 16; // What is the size of our screen , this could probably be moved outside the loop but its more dynamic like this
if(strlen(message) < 16)
{
//if the message is bigger than the current buffer use its length instead;
copySize = strlen(message);
}
//Store the current position temporarily and invert its sign if its negative since we are going in reverse
int tempPos = pos;
if(tempPos < 0)
{
tempPos = -(tempPos);
}
//Build the lcd text by copying the required text out of our template message variable
memcpy(&lcdTop[0],&message[tempPos],copySize);
lcd.print(lcdTop);//Print it from position 0
//Increase the current position and check if the position + 16 (screen size) would be larger than the message length , if it is go in reverse by inverting the sign.
pos += 1;
if(pos +16 >= strlen(message))
{
pos = -(pos);
}
}
}
void loop() {
printLine(1);
}
@alastairhm
Copy link

Thanks just what I was looking for, save me reinventing the wheel ;)

@RC1140
Copy link
Author

RC1140 commented Sep 25, 2013

No prob , surprised anyone found this

@softfoot
Copy link

softfoot commented Oct 4, 2014

A nice way to do it :-)

But, on my 20 char wide lcd I found that line 43 needed to be changed to "if (pos + 20 >= (int)strlen(message))" or the display wouldnt reverse properly.

You also need lcdTop to be one byte bigger with the last byte initialised to 0 - otherwise "lcd.print(lcdTop)" puts one or more garbage characters on the screen, in my case, since print() is expecting a null terminated string. You may not notice this on a 16 char display because of the way the LCD memory is mapped.

@nyitgroup
Copy link

Thank you for sharing your code
The following line causes he text to stop scrolling at the end
if(pos +16 >= strlen(message))
when changed to
if(pos +16 == strlen(message))
It scrolls back and forth

@el-fran
Copy link

el-fran commented Apr 28, 2016

Hi tks u for sharing! works Great! I think its better change the variable refreshSeconds by refreshMiliSeconds, one second is a to large period refresh, also you must change:
if((millis()/1000) % refresMilihSeconds == 0 && previous != (millis()/1000))
by:
if((millis()) % refreshMiliSeconds == 0 && previous != (millis()))

this i think alow to have better control on time scroll.

@Phi-Van-Lam
Copy link

Thanks for your code but I have problems:
Display of line 2 collum 0 is missing.
I don't know where is a change in the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment