Skip to content

アクティビティ

無線ドアアラーム

上級 | MakeCode, Python | LED表示, コンパス, 無線 | センサー, 抽出, 磁気, 通信, 関係演算子, 電波

ステップ1: 作る

説明

ドアを開けたか、開けっ放しにしたときに警告するワイヤレスアラーム。

作り方

  • それぞれ異なるプログラムが入っている二つのmicro:bitが要ります。 1つのmicro:bitは無線送信機として、もう1つは無線受信機として機能します。
  • 送信用micro:bitとバッテリーパックをドア枠の角に取り付け、ドアの角に磁石を取り付けます。
  • 受信用のmicro:bitを近くに置いてください。
  • アラームが予想通りに動作しない場合は、送信機のプログラムで磁力強度に関する数字を変更する必要があるかもしれません。 Aボタンを押すと、現在の磁力を読み取った値が表示されます。 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を使用して、例えば「裏のドアが開いた」など、さまざまな無線メッセージを送信して、さまざまなドアの状態を追跡します。
  • 変数を使用して、ドアが開いたままの時間を測定します。暖房エネルギー節約に役立ちそうですか?