import time, utime, dht
from machine import Pin

step = Pin(17)
direction = Pin(16, Pin.OUT)

# To control speed just modify the amount/value of nop[dely amount 0-31].
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def move():
    set(pins, 1)   [31]
    nop()          [31]
    nop()          [31]
    set(pins, 0)   [31]
    nop()          [31]
    nop()          [31]

"""Instantiate a state machine with the move
program, at 100000Hz, with set base to step pin."""

motor = rp2.StateMachine(0, move, freq=100000, set_base=step)

# For temperature sensor

sensor = dht.DHT22(Pin(28)) 
 
# For ultrasolic sensor
trigger = Pin(4, Pin.OUT)
echo = Pin(5, Pin.IN)
def ultra():
    trigger.low()
    utime.sleep_us(2)
    trigger.high()
    utime.sleep_us(5)
    trigger.low()
    while echo.value() == 0:
        signaloff = utime.ticks_us()
    while echo.value() == 1:
        signalon = utime.ticks_us()
    timepassed = signalon - signaloff
    distance = (timepassed * 0.0343) / 2
    return distance
b=0
c=0
while True:
    a=ultra()
    
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    time.sleep(2)

    if a < 400 and (temp > 25 or hum > 50):
        c=c+1
        direction.value(1)
        while b<50:
            motor.active(1)
            utime.sleep(0.001)
            b=b+1
        print("The distance from object is ",a," cm.")
        print("Temperature is ",temp," degree celsius.")
        print("Humidity is ",hum," percent.\n")
    elif not c==0 :
        if a>=400 or (temp <= 25 and hum <= 50):
            direction.value(0)
            while b>0:
                motor.active(1)
                utime.sleep(0.001)
                b=b-1
    motor.active(0)
    utime.sleep(0.01)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
A4988