Unicorn phat project - what am I doing wrong?

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

Off the top of my head, I seem to recall hsv_to_rgb taking a hue value in the range 0.0 to 1.0, rather than 0 to 359. This could be it. Although the r,g,b values should never equal 255,255,255 since that would imply a saturation of 0.0 when you’re clearly passing in 1.0. So… uh… yeah, that’s odd!

Cheers Phil. Yeah, looks like my h parameter was the issue. I’ve tweaked its definition and the following now seems to work well; while True:

            r=0
            g=0
            b=0
            s=1.0
            v=1.0
            t=read_temp()
            h=240 * (1-(t-15)/20)
            hinput=h/360
            r,g,b = [int(c * 255) for c in colorsys.hsv_to_rgb(hinput,s,v)]
            print(r,g,b,t,h,hinput)
            unicorn.set_all(r,g,b)
            unicorn.show()
            time.sleep(1)

I can see RGB values varying nicely with temp now - and the Unicorn phat does show a nicely varying set of colours as the temp rises and falls. Thanks for the pointer.

EDIT: fixed the formatting. Yeah, don’t know why I didn’t do that on this 2nd post.

You’re welcome!

Ooh and for future reference you can preserve code formatting by using three backticks:

```
# Like so!
```

Eg:

while True:
    r=0
    g=0
    b=0
    s=1.0
    v=1.0
    t=read_temp()
    h=240 * (1-(t-15)/20)
    hinput=h/360
    r,g,b = [int(c * 255) for c in colorsys.hsv_to_rgb(hinput,s,v)]
    print(r,g,b,t,h,hinput)
    unicorn.set_all(r,g,b)
    unicorn.show()
    time.sleep(1)

Edit: Ignore that pointer I’m an idiot you clearly already know that from your first post :D