I have been trying to setup Global Variable so they can be used in other function. I only need the temp_f variable from this one Function. I get an error (temp has not been define)
from ili934xnew import ILI9341, color565 from machine import Pin, SPI import tt24 from time import sleep import dht import _thread as th spi = SPI(1, baudrate=20000000, miso=Pin(19), mosi=Pin(23), sck=Pin(18)) display = ILI9341(spi, cs=Pin(2), dc=Pin(27), rst=Pin(33), w=320, h=240, r=1) display.set_font(tt24) sensor = dht.DHT11(Pin(5)) globals()[temp_f] = round((temp * 9 / 5) + 32, 1) globals()[temp] = sensor.temperature() def loop_Temp_Humid_sensor(): while True: sleep(4) sensor.measure() temp = sensor.temperature() hum = sensor.humidity() # temp_f = temp * (9/5) + 32.0 temp_f = round((temp * 9 / 5) + 32, 1) # print('Temperature: %3.1f C' %temp) # print('Temperature: %3.1f F' %temp_f) # print('Humidity: %3.1f %%' %hum) # display.erase() # display.set_font(tt24) # display.set_pos(10, 40) # display.print(str(temp_f)) # print(temp_f) # print(hum) # loop_Temp_Humid_sensor() # th.start_new_thread(loop_Temp_Humid_sensor, ()) def loop_Text_ili9341(): while True: display.erase() display.set_font(tt24) display.set_pos(60, 0) display.print("Pool Solar Heater!!") display.set_pos(10, 20) display.print("Temperature") display.set_pos(180, 20) display.print("Humidity") display.set_pos(20, 40) display.print(str(temp_f)) display.set_pos(0, 80) display.print("Pool Temp!") display.set_pos(180, 80) display.print("Solar Temp!") display.set_pos(60, 140) display.print("Solar Heater Run") display.set_pos(60, 180) display.print("Date and Time") sleep(2) loop_Text_ili9341() # th.start_new_thread(loop_Text_ili9341, ())
The problem also stop running at
display.print(str(temp_f))
When I take the str out the lcd runs fine. Hope you can help!!
1 Answers
Hi Ray.
The temp not defined error is because you’re using a variable called temp on the following line that was not declared before:
globals()[temp_f] = round((temp * 9 / 5) + 32, 1)
So, you need to swap the order of your lines like this:
globals()[temp] = sensor.temperature()
globals()[temp_f] = round((temp * 9 / 5) + 32, 1)
I hope this helps.
Regards,
Sara