from machine import Pin,ADC
from GpioLCD import GpioLcd
from time import sleep
import math

adcpin = 26 
thermistor = ADC(adcpin) #NTC = Negative temperature coefficient

led_red = Pin(3, Pin.OUT)
led_orange = Pin(22, Pin.OUT)
led_yellow = Pin(27,Pin.OUT)
led_green = Pin(28,Pin.OUT)

btn_green = Pin(2, Pin.IN, Pin.PULL_UP)
btn_red = Pin(1, Pin.IN, Pin.PULL_UP)


# Voltage Divider
Vin = 3.3
Ro = 10000  # 10k Resistor

# Steinhart Constants
A = 0.001129148
B = 0.000234125
C = 0.0000000876741


# Create the LCD object
lcd = GpioLcd(rs_pin=Pin(16),
              enable_pin=Pin(17),
              d4_pin=Pin(18),
              d5_pin=Pin(19),
              d6_pin=Pin(20),
              d7_pin=Pin(21),
              num_lines=2, num_columns=16)


def initial():
    led_red.value(0)
    led_orange.value(0)
    led_yellow.value(0)
    led_green.value(0)
    btn_green.value(1)
    btn_red.value(1)

def avg(lst):
    s = 0
    for i in lst:
        s += i
    s /= len(lst)
    return round(s, 2)

def get_temp():
    # Get Voltage value from ADC   
    adc = thermistor.read_u16() #reads a 16-bit unsigned integer from  sensor
    Vout = (3.3/65535)*adc #65535 16-bit max val
    
    # Calculate Resistance
    Rt = (Vout * Ro) / (Vin - Vout) 
    # Rt = 10000  # Used for Testing. Setting Rt=10k should give TempC=25
    
    # Steinhart - Hart Equation
    TempK = 1 / (A + (B * math.log(Rt)) + C * math.pow(math.log(Rt), 3))

    # Convert from Kelvin to Celsius
    TempC = TempK - 273.15

    #Convert from Celsius to Farenheit
    TempF = (TempC * 9/5) + 32

    #reads the values for n secs
    print(round(TempF, 1))
    return TempF

def activate_led(temp):
    if temp<=99:
        led_green.value(1)
        lcd.move_to(17,0)
        lcd.putstr(" Normal")
    elif 99<temp<=100.8:
        led_yellow.value(1)
        lcd.move_to(17,0)
        lcd.putstr(" Low Fever")
    elif 100.8<temp<=102.6:
        led_orange.value(1)
        lcd.move_to(17,0)
        lcd.putstr(" Fever")
    else:
        led_red.value(1)
        lcd.move_to(17,0)
        lcd.putstr(" High Fever")

def meas():
    led_red.value(1)
    led_orange.value(1)
    led_yellow.value(1)
    led_green.value(1)
  
def calc_temp(itr):

    temp = []
    t=0

    while True:

        if t==(itr):
            break
        TempF = get_temp()

        #lcd display
        for i in range(3):
            lcd.clear()
            lcd.putstr("Measuring"+('.'*(i+1)))
            sleep(0.25)

        
        sleep(1)
        temp.append(TempF)
        t+=1

    
    return temp
    
def rep():
    while True:
        if (btn_green.value())!=1:
            sleep(0.5)
            btn_green.value(1)
            main1()
            break
            
        elif (btn_red.value())!=1:
            lcd.clear()
            lcd.putstr("Thank You!")
            delay(2500);
            initial();
            break
    
def main1():
    while True:

        initial()

        temp = calc_temp(10)
        temp = avg(temp)
        print("average:", temp)

        lcd.clear()
        lcd.putstr(str(temp)+' F')

        activate_led(temp)

        sleep(5)

        lcd.clear()
        lcd.putstr("Measure again?")
        meas()
        
        break
    
    rep() 

      
main1()
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT