first feature complete implementation

This commit is contained in:
Martin Brodbeck 2022-04-30 15:54:29 +02:00
parent 8723ef3c0c
commit 0e138e1c43

View file

@ -2,9 +2,9 @@ from machine import Pin, I2C
from onewire import OneWire
from ds18x20 import DS18X20
from time import sleep_ms
from machine_i2c_lcd import I2cLcd
from lcd_api import LcdApi
#from pico_i2c_lcd import I2cLcd
from pico_i2c_lcd import I2cLcd
import _thread
# Relais
relais = Pin(15, machine.Pin.OUT, value = 1)
@ -15,18 +15,34 @@ temp_sensor = DS18X20(OneWire(Pin(16)))
sensor_id = temp_sensor.scan()[0]
# LCD Display
#i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000)
#lcd = I2cLcd(i2c, 0x27, 2, 16)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd_addr = 39
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Text in Zeilen
line_top = 'Soll: '
line_bot = 'Ist : '
# Custom characters
degree = (
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
)
# Buttons
button1 = Pin(2, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(3, Pin.IN, Pin.PULL_DOWN)
button3 = Pin(4, Pin.IN, Pin.PULL_DOWN)
global button1_pressed
global button2_pressed
global button3_pressed
button1_pressed = False
button2_pressed = False
button3_pressed = False
def ctrl_relais(active = True):
if active:
@ -35,17 +51,66 @@ def ctrl_relais(active = True):
else:
relais.value(1)
led_onboard.off()
def button_reader_thread():
global button1_pressed
global button2_pressed
global button3_pressed
while True:
if button1.value() == 1:
button1_pressed = True
if button2.value() == 1:
button2_pressed = True
if button3.value() == 1:
button3_pressed = True
sleep_ms(100)
lcd.backlight_on()
lcd.clear()
lcd.custom_char(0, degree)
temp_tgt = 28.0
temp_gap = 0.5
is_heating = False
heat_string = ""
system_on = False
system_on_string = ""
_thread.start_new_thread(button_reader_thread, ())
while True:
temp_sensor.convert_temp()
sleep_ms(750)
temp_curr = str(temp_sensor.read_temp(sensor_id))
temp_curr = temp_sensor.read_temp(sensor_id)
if button1_pressed == True:
temp_tgt -= 0.5
button1_pressed = False
if button2_pressed == True:
temp_tgt += 0.5
button2_pressed = False
if button3_pressed == True:
system_on = not system_on
button3_pressed = False
if system_on == True and temp_curr < temp_tgt - temp_gap:
is_heating = True
elif system_on == True and temp_curr > temp_tgt + temp_gap:
is_heating = False
else:
is_heating = False
ctrl_relais(is_heating)
if system_on == True:
system_on_string = "ON "
else:
system_on_string = "OFF"
if is_heating == True:
heat_string = ">H<"
else:
heat_string = " "
lcd.move_to(0, 0)
lcd.putstr(line_top + "\n" + line_bot + temp_curr + " °C")
sleep_ms(2000)
#ctrl_relais(True)
#sleep_ms(5000)
#ctrl_relais(False)
#sleep_ms(5000)
lcd.putstr("ACT: {0:3.1f}".format(temp_curr) + chr(0) + "C " + heat_string +
"\nTGT: {0:3.1f}".format(temp_tgt) + chr(0) + "C " + system_on_string)