from machine import Pin, ADC
from time import sleep
import servo
import math
adc_bits = 2**12 # 4095
voltage = 3.3 # max voltage
servo_max_angle = 180 # max angle of the servo
BETA = 3950 # no idea what that is
max_cel_reading = 80 # maximum reading the thermistor can take in celsius
starting_point_of_cel = 30 # starting point where the servo will start moving
# declaring our pins
pot = ADC(Pin(35, Pin.IN))
servo1 = servo.Servo(18)
servo2 = servo.Servo(19)
thermistor = ADC(Pin(34, Pin.IN))
print("Welcome")
while True:
# therm code
# read the thermistor
therm_val = thermistor.read()
# print(f"Raw thermistor reading: {therm_val}")
# check if thermistor reading is valid
if therm_val == 0 or therm_val >= adc_bits-1:
print("Invalid thermistor reading, skipping calculation.")
sleep(0.5)
continue
# if it's valid, calculate the temperature in celsius
else:
# convert from bits to celsius reading
celsius = int(1 / (math.log(1 / (adc_bits / therm_val - 1)) / BETA + 1.0 / 298.15) - 273.15)
# print(celsius)
# check if celsius reading is less than 30 degrees
if celsius < 30:
pass
# if so, move servo based on the celsius degree
else:
mapping_for_servo_temp = ((celsius-starting_point_of_cel)*servo_max_angle) / (max_cel_reading - starting_point_of_cel)
servo1.move(mapping_for_servo_temp)
# pot code
# read the pot value
pot_val = pot.read()
# print(pot_val)
# map the angle based on the pot reading
mapping_for_servo_pot = (pot_val * servo_max_angle) / adc_bits
# move the servo on the angle we have calculated
servo2.move(mapping_for_servo_pot)
# a little sleep
sleep(0.1)