a function to scroll data across a LiquidCrystal.h compatible display using a sliding buffer on a larger string:
// function displayMsgScroll()
// scrolls a string from right to left across an LCD
// alternaltes between lines to even wear on a lcd-compatible VFD
// expects a pointer to an array of chars
// returns nothing
void displayMsgScroll(char *currentString)
{
int strPos,
bufPos,
relStrPos,
strSize,
bufSize,
r;
char displayBuffer[21] = " ";
bufSize = (strlen(displayBuffer)) - 1;
strSize = (strlen(currentString)) - 1;
// slide the buffer from the left side of the string all the way
// over to the right side plus the length of the buffer
for (strPos = 0; strPos < (strSize + bufSize + 1); strPos++)
{
// fill the buffer with spaces to clear it each time for display
// we are only going to copy data where it exists
// but only if we aren't past the end of the string
if (strPos < strSize)
{
for (r = 0; r < (strlen(displayBuffer) - 1); r++)
{
displayBuffer[r] = ' ';
}
}
// store the current position in the string
relStrPos = strPos;
// start from the right side of the buffer
bufPos = bufSize;
// loop until we try to copy past the left side of the string
// or we reach the left side of the buffer
while ((relStrPos > -1) && (bufPos > -1))
{
// only copy if the relative position in the string based on the position in
// the buffer is not past the right side of the string
if (relStrPos < (strSize + 1))
{
displayBuffer[bufPos] = currentString[relStrPos];
}
else
// if the buffer is past the end of the string insert spaces
{
displayBuffer[bufPos] = ' ';
}
// move left one in the string
relStrPos--;
// move left one in the buffer
bufPos--;
}
// set the cursor to the start of the row
lcd.setCursor(0, row);
// send the buffer to the lcd
lcd.print(displayBuffer);
// loop and check serial for data
previousMillis = millis();
while (millis() - previousMillis < INTER_DELAY)
{
if (Serial.available() > 0)
{
// jump back to main
// even the wear on the VFD by toggling the row used
if (row == 0)
{
row = 1;
}
else
{
row = 0;
}
lcd.clear();
return;
}
}
}
Note the check for serial data. In the project this is used, the serial port is used to communicate with the device, so I poll it during my delay loop.
Do you have a better way of doing this? You might be able write blanks to the buffer instead of blanking the buffer beforehand.