from lcdi2c import*
from utime import sleep
from machine import Pin,time_pulse_us
from time import sleep,sleep_us
from machine import Pin,ADC
from math import log
BETA = 3950
sensor=ADC(Pin(27))

echo = Pin(19, Pin.IN)
trigger = Pin(20, Pin.OUT)

cmd_wrt(0xc0+0)
string_write("LAB 6")
cmd_wrt(0xc0+8)
string_write("EMBEDDED")

def display(distance,temp):
    cmd_wrt(0x80)
    string_write("dis:")
    string_write(str(int(distance)))
    string_write("cm")

    cmd_wrt(0xc0)
    string_write("Temp:")
    string_write(str(int(temp)))
    string_write("^C")

def get_distance():
    trigger.value(1)
    sleep_us(10)
    trigger.value(0)
    timing=time_pulse_us(echo,1,1000000)
    if timing<0:
        print("error measurement")
    else:
        distance=timing/58
        print("distance",distance,"cm")
    sleep(0.5)
    return distance

def get_temp():
    nilai=sensor.read_u16() #to get reading in 	digital
    voltage=nilai/65535*3.300   #get reading in voltage
    print(nilai,voltage) 
    celsius = 1 / (log(1 / (65535.00 / nilai - 1)) / BETA + 1.0 / 298.15) - 273.15;
    print("temperature",celsius,"^C")
    return celsius

lcd_start()

while 1:
    tempe=get_temp()
    disa=get_distance()
    cmd_wrt(0x01)
    display(get_distance(),get_temp())
    
    if disa>10:
        cmd_wrt(0x80+10)
        string_write("Far")     
    elif disa<10:
        cmd_wrt(0x80+10)
        string_write("Near")
    if tempe>=30:
        cmd_wrt(0xc0+10)
        string_write("Hot")   
    elif tempe<=30:
        cmd_wrt(0xc0+10)
        string_write("Cool")     
   
    sleep(1)      


        
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT