|
楼主 |
发表于 2020-6-24 12:53
|
显示全部楼层
7、超声波传感器的入侵报警器
[mw_shl_code=arduino,true]#MicroPython动手做(34)——通用传感器的综合运用
#超声波传感器的入侵报警器
from hcsr04 import HCSR04
from mpython import *
import time
import music
hcsr04 = HCSR04(trigger_pin=Pin.P13, echo_pin=Pin.P14)
while True:
oled.fill(0)
oled.DispChar(" 超声波传感器入侵报警器", 0, 0, 1)
oled.DispChar((''.join([str(x) for x in ["检测的距离:", str(hcsr04.distance_mm()), "mm"]])), 0, 16, 1)
oled.show()
rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
if hcsr04.distance_mm() < 200:
oled.DispChar(" 有人入侵!报警!", 0, 32, 1)
oled.show()
for count in range(3):
rgb.fill((int(102), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
music.pitch(392, 500)
time.sleep_ms(300)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
music.pitch(784, 500)
time.sleep_ms(300)
else:
oled.fill_rect(0, 32, 128, 16, 0)
oled.show()
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)[/mw_shl_code]
|
|