Posts: 7
Threads: 2
Joined: Mar 2018
Hello,
Im new to python, so forgive me if I am clumsy in my wording and if I am not providing enough information.
I am playing with writing to an 2 line LCD with a raspberry pi. I am using this example
https://github.com/adafruit/Adafruit_Pyt...har_lcd.py
# Print a two line message
lcd.message('Hello\nworld!' Where the example says Hello on the first line, and World on the second I would like to print two different temperature readings. One on each line. I can't seem to figure out how to print some text and within that call my temp reading variable. I haven't been able to even get one line to work let alone use both lines.
lcd.message('Indoor: (tempindoor)' lcd.message('Indoor: %d'(tempindoor) Neither of those work
lcd.message('str(tempindoor)' This prints my variable the way I want but I cannot figure out how to add in the text too.
Any comments are welcome.
Posts: 12,022
Threads: 484
Joined: Sep 2016
Mar-11-2018, 10:28 PM
(This post was last modified: Mar-11-2018, 10:28 PM by Larz60+.)
You are missing a close parenthesis on lcd.message line:,
should be: lcd.message('Hello\nworld!')
Posts: 7
Threads: 2
Joined: Mar 2018
(Mar-11-2018, 10:28 PM)Larz60+ Wrote: You are missing a close parenthesis on lcd.message line:,
should be: lcd.message('Hello\nworld!')
That was a copy/paste error. They are there in the actual program, but thank you!
Posts: 12,022
Threads: 484
Joined: Sep 2016
Have you seen anything on the LCD at all?
I took a look at the adafruit program.
If you have tried to run that code without modification, and it does not work
you need to start suspecting the hardware,
Perhaps connectors are not properly seated, reversed or something of the sort,
perhaps the device itself is damaged, or power not being properly supplied.
Look for another software routine designed for the device you are using and try that.
Search the web for other user comments on the device
You know what I'm saying here.
Posts: 7
Threads: 2
Joined: Mar 2018
(Mar-12-2018, 01:18 AM)Larz60+ Wrote: Have you seen anything on the LCD at all?
I took a look at the adafruit program.
If you have tried to run that code without modification, and it does not work
you need to start suspecting the hardware,
Perhaps connectors are not properly seated, reversed or something of the sort,
perhaps the device itself is damaged, or power not being properly supplied.
Look for another software routine designed for the device you are using and try that.
Search the web for other user comments on the device
You know what I'm saying here.
Yep, the demo script runs as it should on the LCD. Its only when I start trying to print a string and a variable into it that I have issues.
Posts: 12,022
Threads: 484
Joined: Sep 2016
please show your code, use BBCODE tags
Posts: 7
Threads: 2
Joined: Mar 2018
(Mar-12-2018, 01:41 AM)Larz60+ Wrote: please show your code, use BBCODE tags
#!/usr/bin/python
# Example using a character LCD connected to a Raspberry Pi or BeagleBone Black.
import time
import Adafruit_CharLCD as LCD
# Raspberry Pi pin configuration:
lcd_rs = 25 # Note this might need to be changed to 21 for older revision Pi's.
lcd_en = 24
lcd_d4 = 8
lcd_d5 = 7
lcd_d6 = 12
lcd_d7 = 16
lcd_backlight = 2
outdoor="/sys/bus/w1/devices/28-0517b184cdff/w1_slave"
indoor="/sys/bus/w1/devices/28-0517b196efff/w1_slave"
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Alternatively specify a 20x4 LCD.
# lcd_columns = 20
# lcd_rows = 4
# 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)
while True:
with open(outdoor) as odt:
odt_contents = odt.read()
odt_temp = odt_contents[69:75]
temp_outdoor = int(float(odt_temp))
outdoortemp = temp_outdoor / 1000 * 9 / 5 + 32
with open(indoor) as idt:
idt_contents = idt.read()
idt_temp = idt_contents[69:75]
temp_indoor = int(float(idt_temp))
indoortemp = temp_indoor / 1000 * 9 / 5 + 32
# Print a two line message
lcd.clear()
lcd.message("")
# Wait 5 seconds
time.sleep(5.0) Where lcd.message("") is I would like it to print:
Indoor: (indoortemp)
and one line 2
Outdoor: (outdoortemp)
Basically print text and then the variable.
Posts: 12,022
Threads: 484
Joined: Sep 2016
Mar-12-2018, 02:46 AM
(This post was last modified: Mar-12-2018, 02:46 AM by Larz60+.)
try the following:
lcd.clear()
msg = 'Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp)
lcd.message(msg) Note that there are no quotes around msg. That's because it is a variable.
you should be able to do it this way as well
lcd.clear()
lcd.message('Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp))
Posts: 7
Threads: 2
Joined: Mar 2018
(Mar-12-2018, 02:46 AM)Larz60+ Wrote: try the following:
lcd.clear()
msg = 'Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp)
lcd.message(msg) Note that there are no quotes around msg. That's because it is a variable.
you should be able to do it this way as well
lcd.clear()
lcd.message('Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp))
That works! Very grateful for your help! Thank you! I'll see if I can do some reading on what exactly is happening there. Ive never used .format before.
Posts: 12,022
Threads: 484
Joined: Sep 2016
if you are running python 3.6, there is an even better improvement over format, it's called f-string,
and for your code, the above would be coded like:
msg = f'Indoor: {indoortemp}\nOutdoor: {outdoortemp}' which is slick!
|