Skip to content

Instantly share code, notes, and snippets.

@clasqui
Last active October 14, 2017 01:49
Show Gist options
  • Save clasqui/bdf464d734f83dfc0abc to your computer and use it in GitHub Desktop.
Save clasqui/bdf464d734f83dfc0abc to your computer and use it in GitHub Desktop.
Adafruit LCD text scroll function for Raspberry Pi

Adafruit LCD text scroll function for Raspberry Pi

This tiny function, written in python, allows you to easily scroll text of any length in the LCD 16x2 of Adafruit, which only allows 16 characters per row.

#Install And Use To use this function just download the file named ScrollLCD.py and in your code import it. It takes two obligatory parameters, lcd and text.

Example:

from Adafruit_CharLCD import Adafruit_CharLCD
from ScrollLCD import *

lcd = Adafruit_CharLCD()
lcd.begin(16,1)

# Some code here ...

text = "This text has more than 16 characters, but it will print pretty fine in the LCD!"
scroll(lcd, text)

# More code here ...

You can customize things like the pause between next scroll, how many repetitions or the pause between every repetition. Pass those values in the function (scroll(lcd, text, pause1, pause2, repetitions)) or edit the defaults in the file.

What could you do now? I don't know.. maybe a program that shows you the latest tweet in your timeline in the LCD?

#Feedback This function needs lots of improvements, so feel free to collaborate. And of course, you can edit the code to fit your needings as you want. Things that should be corrected/added:

  • Detecting spaces and trying to no split words.
  • Detect new lines \n and apply them correctly.
"""
Author: Marc Clascà
Email: [email protected]
May 2014
@hulehule20
"""
from time import sleep
def scroll(lcd, text, pause1=False, pause2=False, rep=False):
# Edit following lines to change timing defaults. Remember you can decide them when you call the function.
PAUSE_NEXT = 2
PAUSE_REP = 2
REPETITIONS = 4
if pause1: PAUSE_NEXT = pause1
if pause2: PAUSE_REP = pause2
if rep: REPETITIONS = rep
n = 16
rows = [text[i:i+n] for i in range(0, len(text), n)]
n_rows = len(rows)
for i in range(REPETITIONS):
for x in range(n_rows):
lcd.home()
lcd.clear()
nxt = x + 1
lcd.message(rows[x]+"\n")
if nxt == n_rows:
sleep(2)
break
else:
lcd.message(rows[nxt])
sleep(PAUSE_REP)
lcd.clear()
@cscashby
Copy link

Thanks for this - very useful code!

FYI I've made a change to a fork (https://gist.github.com/cscashby/ba1b403d138ea81a522d) which adds an encoding line to the top of the file - otherwise the compiler breaks for the non-ASCII character in line 2.

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