Mon Mar 12, 2018 8:23 am
I have been working on the some modified code for making a thermostat, and it all works until I try to get the newtemp going either + or -. the sense hat joystick will not function to the left right up or down correctly. However, it does work when pushed down. I am new a coding in Python so any help would be much appreciated. Once I get this running, then I will create the lines of code to measure the difference of newtemp and var to determine if the GPIO pin should be pulled high or not.
# Temperature display for Sense Hat
# Orignial code by Tony Goodhew 27 September 2015
# Python3 - "Use sudo idle3"
from sense_hat import SenseHat
import time
sense = SenseHat()
sense.clear()
#one row per 5x3 digit - 0 to 9
nums =[1,1,1,1,0,1,1,0,1,1,0,1,1,1,1, # Number 0
0,1,0,0,1,0,0,1,0,0,1,0,0,1,0, # Number 1
1,1,1,0,0,1,0,1,0,1,0,0,1,1,1, # Number 2
1,1,1,0,0,1,1,1,1,0,0,1,1,1,1, # Number 3
1,0,0,1,0,1,1,1,1,0,0,1,0,0,1, # Number 4
1,1,1,1,0,0,1,1,1,0,0,1,1,1,1, # Number 5
1,1,1,1,0,0,1,1,1,1,0,1,1,1,1, # Number 6
1,1,1,0,0,1,0,1,0,1,0,0,1,0,0, # Number 7
1,1,1,1,0,1,1,1,1,1,0,1,1,1,1, # Number 8
1,1,1,1,0,1,1,1,1,0,0,1,0,0,1] # Number 9
def show_num(val,xd,yd,r,g,b):
offset = val * 15
for p in range(offset,offset + 15):
if nums[p] == 1:
xt = p % 3
yt = (p-offset) // 3
sense.set_pixel(xt+xd,yt+yd,r,g,b)
def show_number(val,r,g,b): # Range -100 to 100
abs_val = abs(val)
tens = abs_val // 10
units = abs_val % 10
sense.clear()
if (abs_val > 9): show_num(tens,0,2,r,g,b)
show_num(units,4,2,r,g,b)
if abs_val == 100: # Deal with 3 digit number "100"
sense.clear()
show_num(0,5,2,r,g,b)
show_num(0,2,2,r,g,b)
for i in range(2,7):
sense.set_pixel(0,i,r,g,b)
if val < 0 : # Display 'Negative' bar
for i in range(0,8):
sense.set_pixel(i,0,0,0,128)
#========== MAIN ===============
print("Displays Temperature in Farenheight on 8x8 Matrix")
print("Anthony White - 2018, March 11")
print("Terminate program by pressing CTRL-C")
red = (255,0,0) # Setting color
green = (0,255,0 )# Setting color
white = (150, 150, 150) # Setting color
old_val = -999 # Out of range, first time through
while True:
temp = sense.get_temperature_from_humidity() # Gets temp from sensor
calctemp = 0.0071*temp*temp+0.857*temp-10.0 # Known temp correction for heat generated from ARM
val = ((calctemp/5)*9)+27 # conversion from Celcuius to Farenheight accounted for further temp deviaton that was found
sign = 1 # Used for low temperatures
if val < 0 :
sign = -1
val = int(abs(val + 0.5)) # round to nearest whole deg
if val != old_val: # Has temperature changed?
old_val = val
sense.clear()
show_number(val,50,50,255)
sense.set_pixel(0,0,red) # link indicator
sense.set_pixel(7,0,white) # creates degrees symbol
sense.set_pixel(6,0,white)
sense.set_pixel(7,1,white)
sense.set_pixel(6,1,white)
sense.set_rotation(270)
while True:
for event in sense.stick.get_events():
if event.action == "pressed":
if event.direction == "middle":
show_number(val,50,50,255)
sense.set_pixel(0,0,green) # link indicator
sense.set_pixel(7,0,white) # creates degrees symbol
sense.set_pixel(6,0,white)
sense.set_pixel(7,1,white)
sense.set_pixel(6,1,white)
sense.set_rotation(270)
elif event.direction == "right": #goes into a set temp mode
sense.clear()
show_number(val,50,50,255)
sense.set_pixel(7,0,white) # creates degrees symbol
sense.set_pixel(6,0,white)
sense.set_pixel(7,1,white)
sense.set_pixel(6,1,white)
sense.set_rotation(270)
while True:
if event.direction == "up": #set temp up
newtemp = val + 1
sense.clear()
show_number(newtemp,0,255,0)
sense.set_pixel(7,0,white) # creates degrees symbol
sense.set_pixel(6,0,white)
sense.set_pixel(7,1,white)
sense.set_pixel(6,1,white)
sense.set_rotation(270)
elif event.direction == "down": # set temp down
newtemp = val - 1
sense.clear()
show_number(newtemp,0,255,0)
sense.set_pixel(7,0,white) # creates degrees symbol
sense.set_pixel(6,0,white)
sense.set_pixel(7,1,white)
sense.set_pixel(6,1,white)
sense.set_rotation(270)
elif event.direction == "left": # go out of set temp routine
show_number(val,50,50,255)
sense.set_pixel(0,0,green) # link indicator
sense.set_pixel(7,0,white) # creates degrees symbol
sense.set_pixel(6,0,white)
sense.set_pixel(7,1,white)
sense.set_pixel(6,1,white)
sense.set_rotation(270)
sleep(0.5) # Wait a while and then clear the screen
sense.clear()
time.sleep(2) #Wait time before next sample and display