from machine import Pin, ADC
import time
AirValue = 462 # แทนค่า AirValue
WaterValue = 3813 # แทนค่า WaterValue
def map(val, loval, hival, tolow, tohigh):
if loval <= val <= hival:
return (val - loval)/(hival - loval) * (tohigh - tolow) + tolow
else:
raise ValueError
adc = ADC(Pin(35)) # ใช้หมายเลขพิน ADC ที่ถูกต้องสำหรับบอร์ดของคุณ
soilMoistureValue = 0
soilMoisturePercent = 0
while True:
soilMoistureValue = adc.read()
print(soilMoistureValue)
time.sleep(0.25)
soilMoisturePercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100)
if soilMoisturePercent >= 100:
print("100 %")
elif soilMoisturePercent <= 0:
print("0 %")
elif soilMoisturePercent > -1 and soilMoisturePercent < 101:
print(soilMoisturePercent, "%")
if soilMoisturePercent > 90:
print("Very Wet")
elif soilMoisturePercent > 65:
print("Wet")
elif soilMoisturePercent > 35:
print("Normal")
else:
print("Dry")
time.sleep(1)