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