I'm new to Python and have been playing around with it and temperature sensors on the Raspberry Pi over pandemic. I'm trying to test three DHT22 sensors against one another and display the output on an LCD screen. I have the following, but I know it can be more elegant.
dht1 = dht.DHT22(board.D26)
dht2 = dht.DHT22(board.D19)
dht3 = dht.DHT22(board.D13)
#getting the values from the sensors left as floats for math
t1 = dht1.temperature
t2 = dht2.temperature
t3 = dht3.temperature
h1 = dht1.humidity
h2 = dht2.humidity
h3 = dht3.humidity
#convert to strings for display
t1s = str(t1)
t2s = str(t2)
t3s = str(t3)
h1s = str(h1)
h2s = str(h2)
h3s = str(h3)
#a little math
tavg = round(((t1 + t2 + t3)/3), 1)
tavgs = str(tavg)
havg = round(((h1 + h2 + h3)/3), 1)
havgs = str(havg)
I would love to be able to iterate through these so I don't have to duplicate the same operations over and over, but I can't seem to get for i in range(1, 3)
or lists or anything else to work to create variable names or iterate through taking the measurements.
Any ideas? Thank you!