NEWBIE: how to get text onto 2 lines on a 16x2 lcd display
RobH
rob at despammer.com
Thu Sep 26 11:34:50 EDT 2019
On 26/09/2019 12:55, Rhodri James wrote:
> On 26/09/2019 11:58, RobH wrote:
>> Thanks, but was is Python REPR.
>
> DL was referring to the interactive program you get when you type
> "python" at a Linux or Windows command prompt. Here's an example,
> copied from my Linux box:
>
> rhodri at scrote:~$ python
> Python 2.7.15+ (default, Jul 9 2019, 16:51:35)
> [GCC 7.4.0] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import cgi
> >>> help(cgi)
> [[screeds of documentation for the cgi module cut]]
> >>> [[type control-D to get out]]
> rhodri at scrote:~$
>
> (The bits in [[double square brackets]] are my comments, not something
> either I or Python typed!)
>
>> This my adaptation of non working code. Bodged from the char_lcd.py code:
>>
>> GNU nano 2.7.4 File: char_lcd.py
>>
>> #!/usr/bin/python
>> # Example using a character LCD connected to a Raspberry Pi or
>> BeagleBone Black.
>> import time
>>
>> import Adafruit_CharLCD as LCD
>
> Aha, that's the critical information we were lacking earlier. I found
> the Adafruit_CharLCD library after a little googling. It's marked as
> deprecated (the author now uses something else), but if it works for you
> you might as well keep on using it.
>
> [[Much set-up for the Pi cut for brevity]]
>
>> # Initialize the LCD using the pins above.
>> lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
>> lcd_d7,
>> lcd_columns, lcd_rows, lcd_backlight)
>>
>> # Print a two line message
>> # lcd.message('Hello\nworld!')
>> lcd.message( "Hello" 1)
>
> It looks like lcd.message() takes a text string and displays on the LCD,
> starting at the current cursor position (I'll get back to that). You
> optimistically added an extra argument, the line number to start at
> (you're missing a comma, but that's an easy typo to make and I assume
> you already caught it). Unfortunately lcd.message() only takes the one
> argument, the text string. This is where that error message comes from:
> "TypeError: message() takes exactly 2 arguments (3 given)" means that
> you gave the function more arguments than it knew what to do with.
>
> (Why 2 and 3 instead of 1 and 2? "lcd" itself is an argument to
> message(), telling it which lcd to send the message to if you had more
> than one. When you look at the function definition, you'll see that it
> start with "def message(self, text):" which makes that a bit more
> explicit. Anyway, that's beside the point.)
>
> So how do you control where you start your message on the LCD? It looks
> like the answer is the lcd.set_cursor() function. The cursor is the
> point from which your printing starts. In your text editor it's
> probably a blinking vertical line (or underscore, or blob... editors
> vary). On your LCD display it's probably invisible, though it looks
> like there's another function to change that.
>
> Anyway, you give set_cursor() a column number and a row number in that
> order. I think they start from 0, so to write "Hello" to the second
> line of your display you would write:
>
> lcd.set_cursor(0, 1)
> lcd.message("Hello")
>
> The gotcha here is that message() is only going to write the characters
> you tell it to, and won't overwrite anything else. If for example you
> followed up the two lines above with:
>
> lcd.set_cursor(0, 1) # Back to the start of the line
> lcd.message("Bye")
>
> you would end up with "Byelo" displayed on the LCD. You would need to
> put extra spaces on the end of your message to overwrite the characters
> you don't want any more, but not so many that you overwrite what you
> want to keep. Working out how many spaces that is for any given message
> could be quite tedious. You may well find it easier to lcd.clear() the
> whole display and rewrite everything when you want to change anything.
>
> Anyway, I hope that helps.
>
Thank you, that does help me just great.
So simple as well. Actually I have worked quite a lot with Arduinos and
the code or sketch for an lcd there is just the same to write to the 2nd
line. I didn't think it would work for python, but it does!
Other lines like lcd.delay(1000) and lcd.begin(16,2), do not work in
python, but I thought to try them to find out.
I am using a Raspberry Pi Zero for this little project, which now does
what I want for now.
Thanks again.
More information about the Python-list
mailing list