KRITSADA

microbit Excercise for VEC 2020 #2

Jan 27th, 2020
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. # เฉลยโจทย์การแข่งขัน microbit ข้อที่ 2 การอ่านค่าจากเซนเซอร์วัดระยะทาง เปิดปิดการทำงานของหลอดไฟ LED 5x5
  2. # ควบคุมการติดดับของ LED 5x5 ด้วยระยะทางจาก ZX-Sonar
  3. from microbit import *
  4. x1=0
  5. x2=0
  6. x3=0
  7. x4=0
  8. x5=0
  9. while 1:
  10.     sonar=pin1.read_analog()
  11.     print('sonar=',int(sonar*520/1023),'CM')
  12.     sleep(100)
  13.     if sonar>20 and sonar<30 :
  14.         while sonar>20 and sonar<30 :
  15.             sonar=pin1.read_analog()
  16.             print('sonar=',int(sonar*520/1023),'CM')
  17.             sleep(100)
  18.         if x1==0 :
  19.             x1=1
  20.             for i in range(0,5):
  21.                 display.set_pixel(0,i,9)
  22.         else :
  23.             x1=0
  24.             for i in range(0,5):
  25.                 display.set_pixel(0,i,0)
  26.     elif sonar>30 and sonar<40 :
  27.         while sonar>30 and sonar<40 :
  28.             sonar=pin1.read_analog()
  29.             print('sonar=',int(sonar*520/1023),'CM')
  30.             sleep(100)
  31.         if x2==0 :
  32.             x2=1
  33.             for i in range(0,5):
  34.                 display.set_pixel(1,i,9)
  35.         else :
  36.             x2=0
  37.             for i in range(0,5):
  38.                 display.set_pixel(1,i,0)
  39.     elif sonar>40 and sonar<50 :
  40.         while sonar>40 and sonar<50 :
  41.             sonar=pin1.read_analog()
  42.             print('sonar=',int(sonar*520/1023),'CM')
  43.             sleep(100)
  44.         if x3==0 :
  45.             x3=1
  46.             for i in range(0,5):    
  47.                 display.set_pixel(2,i,9)
  48.         else :
  49.             x3=0
  50.             for i in range(0,5):
  51.                 display.set_pixel(2,i,0)
  52.     elif sonar>50 and sonar<60 :
  53.         while sonar>50 and sonar<60 :
  54.             sonar=pin1.read_analog()
  55.             print('sonar=',int(sonar*520/1023),'CM')
  56.             sleep(100)
  57.         if x4==0 :
  58.             x4=1
  59.             for i in range(0,5):
  60.                 display.set_pixel(3,i,9)
  61.         else :
  62.             x4=0
  63.             for i in range(0,5):
  64.                 display.set_pixel(3,i,0)
  65.     elif sonar>60 and sonar<70 :
  66.         while sonar>60 and sonar<70 :
  67.             sonar=pin1.read_analog()
  68.             print('sonar=',int(sonar*520/1023),'CM')
  69.             sleep(100)
  70.         if x5==0 :
  71.             x5=1
  72.             for i in range(0,5):
  73.                 display.set_pixel(4,i,9)
  74.         else :
  75.             x5=0
  76.             for i in range(0,5):
  77.                 display.set_pixel(4,i,0)
Advertisement
Add Comment
Please, Sign In to add comment