import machine
from time import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
Pressure = machine.Pin(2, machine.Pin.OUT)
Camera = machine.Pin(2, machine.Pin.OUT)
Level = machine.Pin(12, machine.Pin.OUT)
DC_Motor = machine.Pin(15, machine.Pin.OUT)
PH = machine.Pin(22, machine.Pin.OUT)
Moist = machine.Pin(18, machine.Pin.OUT)
servo_pin = machine.Pin(15)
servo = machine.PWM(servo_pin)
servo.freq(50)
adc_servo = machine.ADC(26) # classification feedback to pin 26
adc_servo.atten(machine.ADC.ATTN_11DB)
i2c = machine.I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Use I2C0 on GP0 (SDA) and GP1 (SCL)
I2C_ADDR = 0x27 # LCD I2C address, check scanner
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) # 16x2 LCD
adc_ph = machine.ADC(27) # ph feedback to pin 27
acidic_threshold = 32766
neutral_threshold = 32767
alkaline_threshold = 32768
adc_moist = machine.ADC(28) # moist feedback to pin 28
dry_threshold = int((1.0 / 3.3) * 65535)
moist_threshold = int((2.5 / 3.3) * 65535)
ignore_pressure = False
def handle_pressure(pin):
global ignore_pressure
if ignore_pressure:
return
main()
def set_angle(angle):
duty_cycle = int((angle/180) * 1023)
servo.duty(duty_cycle)
def main():
global ignore_pressure
try:
#remove while true
if Pressure.value() == 1:
Camera.value(1)
verify_organic = adc_servo.read_u16()
if verify_organic == 0:
set_angle(0)
sleep(30)
set_angle(90)
Camera.value(0)
if Level.value() == 1:
DC_Motor.value(1)
PH.value(1)
Moist.value(1)
ignore_pressure = True
sleep(30)
ph_reading = adc_ph.read_u16()
pH_level = ((ph_reading / 65535) * 14)
moist_reading = adc_moist.read_u16()
lcd.clear()
if ph_reading <= acidic_threshold:
lcd.move_to(0, 0)
lcd.putstr("PH:{:.1f}, ACIDIC".format(pH_level))
elif ph_reading == neutral_threshold:
lcd.move_to(0, 0)
lcd.putstr("PH:{:.1f}, NEUTRAL".format(pH_level))
else:
lcd.move_to(0, 0)
lcd.putstr("PH:{:.1f}, ALKALINE".format(pH_level))
if moist_reading <= dry_threshold:
lcd.move_to(0, 1)
lcd.putstr("Moist: DRY")
else:
lcd.move_to(0, 1)
lcd.putstr("Moist: WET")
sleep(5)
DC_Motor(0)
PH.value(0)
Moist.value(0)
else:
Camera.value(0)
else:
set_angle(180)
sleep(30)
set_angle(90)
Camera.value(0)
else:
Camera.value(0)
except:
servo.deinit() # clean up PWM
Pressure.irq(trigger=machine.Pin.IRQ_RISING, handler=handle_pressure)