from sys import stdout from machine import ADC,Pin import uasyncio def led_s(): cn1 = Pin(18, Pin.OUT) init_value = -1 pin_value = 0 def ctrl_led(current_value): nonlocal init_value,pin_value if init_value == -1: init_value = current_value print('led init_value:', init_value) elif current_value > init_value + 3000: cn1.off() else: cn1.on() return ctrl_led async def sensor(): adc = ADC(Pin(34), atten=ADC.ATTN_11DB) # create an ADC object acting on a pin ctrl_led = led_s() while True: val = adc.read_u16() # read a raw analog value in the range 0-65535 ctrl_led(val) wdat = 'val:{}'.format(val) print(wdat, end='\x08' * len(wdat)) await uasyncio.sleep_ms(50) print(' ' * len(wdat), end='\x08' * len(wdat)) if __name__ == '__main__': uasyncio.run(sensor())