Skip to content

活动

无线门报警器

高级 | MakeCode, Python | LED显示器, 指南针, 无线电 | 传感器, 关系运算符, 无线电波, 磁性, 选择, 通信

第 1 步:制作

它是什么?

当有人打开门或没关上门时,无线警报会警告您。

How to make it

  • You will need two micro:bits with different code on each. One micro:bit acts as a radio transmitter and the other as a radio receiver.
  • Attach the transmitter micro:bit and battery pack to the corner of a door frame and attach a magnet close to it on the corner of a door.
  • Place the receiver micro:bit anywhere nearby.
  • If the alarm doesn’t work as you expected, you may need to change the magnetic force strength number in the transmitter code. Pressing button A shows the current magnetic force reading. Use this to decide on the threshold number, taking readings with the door open and closed.

工作原理

  • The transmitter program uses the micro:bit’s compass (magnetometer) input sensor and a loop to measure the strength of the magnetic field every two seconds.
  • It uses selection so when the magnetic field strength falls below a certain level (the threshold), it sends a radio signal ‘open’. If the magnetism reading goes above the threshold, it sends the signal ‘closed'.
  • When the receiver micro:bit receives the signal ‘open’, a cross appears on its LED display and an audible alarm sounds. When it receives the signal ‘closed’, a tick appears on its LED display and no sound plays.

所需材料

  • two micro:bits and at least one battery pack
  • 一块磁铁
  • some adhesive putty to fix a magnet to a door and a micro:bit to a door frame
  • if you have a V1 micro:bit and want to hear an audible alarm, optional headphones, buzzer or speaker and crocodile clip leads to attach them

第 2 步:编程

传感器/发射器:

1from microbit import *
2import radio
3radio.config(group=17)
4radio.on()
5
6while True:
7    if button_a.was_pressed():
8        display.scroll(compass.get_field_strength())
9    if compass.get_field_strength() < 100000:
10        display.show(Image.DIAMOND_SMALL)
11        radio.send('open')
12    else:
13        display.clear()
14        radio.send('closed')
15    sleep(2000)
16

警报器/接收器:

1from microbit import *
2import music
3import radio
4radio.config(group=17)
5radio.on()
6
7while True:
8    message = radio.receive()
9    if message:
10        if message == 'open':
11            display.show(Image.NO)
12            music.play(["C4:4"])
13        if message == 'closed':
14            display.show(Image.YES)
15

第3步:完善

  • 使用多个micro:bit来通过发送不同的无线电消息(例如“back door open”)以追踪不同门的状态。
  • 使用变量来测量门保持打开状态的时间,这能帮助节省热能吗?