1
我是Python的noob,所以请忍受我。 以下代码用于读取通过I2C接口连接的LCD屏幕20x4的湿度和温度传感器。 该程序工作得很好!但我希望在摄氏度和华氏度都包含同一行中的读数,在摄氏温度测量后如何显示值'f'或'f1'(表示华氏转换),因此它看起来像:如何将多个变量写入单个LCD显示行?
Temp_Entry:20.4C/68F ........
谢谢!
#!/usr/bin/env python3
import lcddriver
import time
import Adafruit_DHT as dht
lcd = lcddriver.lcd()
while (True):
#Read values from AM2302 Sensors in Pin 4 & 24
h,t = dht.read_retry(dht.AM2302, 4)
h1,t1 = dht.read_retry(dht.AM2302, 24)
#Humidity calibration compensation
h1 += 5
h -= 9
#Celcius to Fahrenheit conversion
f = t * 9/5 + 32
f1 = t1 * 9/5 + 32
# Print temp and humidity values
lcd.cursor_pos = (0,0)
lcd.lcd_display_string ('Temp_Entry:{0:0.1f}C'.format(t1), 1)
lcd.lcd_display_string ('Hum_Entry :{1:0.1f}%'.format(t1, h1), 2)
lcd.lcd_display_string ('Temp_Back :{0:0.1f}C'.format(t), 3)
lcd.lcd_display_string ('Hum_Back :{1:0.1f}%'.format(t, h), 4)
time.sleep(10)
最终代码(后修正/清理):
#!/usr/bin/env python3
import lcddriver
import time
import Adafruit_DHT as dht
lcd = lcddriver.lcd()
while (True):
#Read values from AM2302 Sensors in Pin 4 & 24
h,t = dht.read_retry(dht.AM2302, 4)
h1,t1 = dht.read_retry(dht.AM2302, 24)
#Humidity calibration compensation
h1 += 5
h -= 9
#Celcius to Fahrenheit conversion
f = t * 9/5 + 32
f1 = t1 * 9/5 + 32
# Print temp and humidity values
lcd.cursor_pos = (0,0)
lcd.lcd_display_string ('Temp_Ent:{0:0.1f}C/{1:0.1f}F'.format(t1, f1), 1)
lcd.lcd_display_string ('Hum_Ent :{0:0.1f}%'.format(h1), 2)
lcd.lcd_display_string ('Temp_Bak:{0:0.1f}C/{1:0.1f}F'.format(t, f), 3)
lcd.lcd_display_string ('Hum_Bak :{0:0.1f}%'.format(h), 4)
time.sleep(10)
谢谢!这就像一个魅力! – syncopium