Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # เฉลยโจทย์การแข่งขัน microbit ข้อที่ 2 การอ่านค่าจากเซนเซอร์วัดระยะทาง เปิดปิดการทำงานของหลอดไฟ LED 5x5
- # ควบคุมการติดดับของ LED 5x5 ด้วยระยะทางจาก ZX-Sonar
- from microbit import *
- x1=0
- x2=0
- x3=0
- x4=0
- x5=0
- while 1:
- sonar=pin1.read_analog()
- print('sonar=',int(sonar*520/1023),'CM')
- sleep(100)
- if sonar>20 and sonar<30 :
- while sonar>20 and sonar<30 :
- sonar=pin1.read_analog()
- print('sonar=',int(sonar*520/1023),'CM')
- sleep(100)
- if x1==0 :
- x1=1
- for i in range(0,5):
- display.set_pixel(0,i,9)
- else :
- x1=0
- for i in range(0,5):
- display.set_pixel(0,i,0)
- elif sonar>30 and sonar<40 :
- while sonar>30 and sonar<40 :
- sonar=pin1.read_analog()
- print('sonar=',int(sonar*520/1023),'CM')
- sleep(100)
- if x2==0 :
- x2=1
- for i in range(0,5):
- display.set_pixel(1,i,9)
- else :
- x2=0
- for i in range(0,5):
- display.set_pixel(1,i,0)
- elif sonar>40 and sonar<50 :
- while sonar>40 and sonar<50 :
- sonar=pin1.read_analog()
- print('sonar=',int(sonar*520/1023),'CM')
- sleep(100)
- if x3==0 :
- x3=1
- for i in range(0,5):
- display.set_pixel(2,i,9)
- else :
- x3=0
- for i in range(0,5):
- display.set_pixel(2,i,0)
- elif sonar>50 and sonar<60 :
- while sonar>50 and sonar<60 :
- sonar=pin1.read_analog()
- print('sonar=',int(sonar*520/1023),'CM')
- sleep(100)
- if x4==0 :
- x4=1
- for i in range(0,5):
- display.set_pixel(3,i,9)
- else :
- x4=0
- for i in range(0,5):
- display.set_pixel(3,i,0)
- elif sonar>60 and sonar<70 :
- while sonar>60 and sonar<70 :
- sonar=pin1.read_analog()
- print('sonar=',int(sonar*520/1023),'CM')
- sleep(100)
- if x5==0 :
- x5=1
- for i in range(0,5):
- display.set_pixel(4,i,9)
- else :
- x5=0
- for i in range(0,5):
- display.set_pixel(4,i,0)
Advertisement
Add Comment
Please, Sign In to add comment