Hey folks,
Probably a simple coding issue - but any thoughts would be appreciated here. I’m building a project to with a Unicorn phat on a Pi Zero that senses the room temp with a DS18B20. My intention is to show a different colour on the Unicorn phat (all pixels identical) with a colour dependent on the room temp. The code for the sensor works fine, and the demo projects for the Unicorn work fine. The issue seems to be my code and not the hardware. My code is as follows;
#!/usr/bin/env python
import os
import glob
import time
import datetime
import unicornhat as unicorn
import colorsys
unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.3)
width,height=unicorn.get_shape()
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
while True:
r=0
g=0
b=0
s=1.0
v=1.0
t=read_temp()
h=int(240 * (1-(t-15)/20))
r,g,b = [int(c * 255) for c in colorsys.hsv_to_rgb(h,s,v)]
print(r,g,b,t,h)
unicorn.set_all(r,g,b)
unicorn.show()
time.sleep(1)
The hue calculation is intended to sweep through colour space with temp such that hue is 240 at 15C room temp and 0 at 35C room temp. i.e. blue in a cool room and red in a hot room.
The issue seems to be that the r, g & b values are always 255 or 0. Why the coarseness of those? When I change the temp of the sensor temp (by holding it) I see the values of t rise as expected - and my h value rises as expected. What am I doing wrong?
thanks,
Ben