Advertisement
glerium

手势检测

Oct 22nd, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from machine import Pin,ADC,PWM,Timer
  2. import time
  3. sensor1 = Pin(18,Pin.IN)    # default: 0
  4. sensor2 = Pin(14,Pin.IN)    # default: 1
  5. LED1 = Pin(32,Pin.OUT)
  6. LED2 = Pin(33,Pin.OUT)
  7. covered1 = covered2 = False
  8. time1 = time2 = time.ticks_ms()
  9. LED1.value(1)
  10. LED2.value(1)
  11.  
  12. def led1_off(tim):
  13.     global LED1
  14.     LED1.value(1)
  15. def led2_off(tim):
  16.     global LED2
  17.     LED2.value(1)
  18.  
  19. while True:
  20.     val1 = sensor1.value()
  21.     val2 = sensor2.value()
  22. #     print('{:d} {:d}'.format(val1,val2))
  23.     if val1 != 1:
  24.         if covered2:
  25.             LED2.value(0)
  26.             covered1 = covered2 = False
  27.             print('left')
  28.             tim0 = Timer(0)
  29.             tim0.init(period=300, mode=Timer.ONE_SHOT, callback=led2_off)
  30.         else:
  31.             covered1 = True
  32.             time1 = time.ticks_ms()
  33.     if val2 != 0:
  34.         if covered1:
  35.             LED1.value(0)
  36.             covered1 = covered2 = False
  37.             print('right')
  38.             tim1 = Timer(1)
  39.             tim1.init(period=300, mode=Timer.ONE_SHOT, callback=led1_off)
  40.         else:
  41.             covered2 = True
  42.             time2 = time.ticks_ms()
  43.     if time.ticks_diff(time.ticks_ms(),time1) >= 300:
  44.         covered1 = False
  45.     if time.ticks_diff(time.ticks_ms(),time2) >= 300:
  46.         covered2 = False
  47.     time.sleep_ms(10)
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement