0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

micro:bitでローラ式距離計を作成してみる

Posted at

仕事でざっくりとした距離が測れて
5m間隔等の一定間隔が測れる距離計が入用になったのでmicro:bitで作ってみました
(センサーとブザーがオンボードの安いマイコンなら何でもよかった)
micro:bitそのものをあまり分かっていないので 今回はブロックでの作成です
image.png

距離の測定は 別途用意した車輪に磁石をつけて磁石が磁気センサーを横切る回数をカウントすることにしました

磁気カウンター

まずはシンプルに磁力が一定値を超えた回数を記録するカウンター
image.png
「ずっと」のサイクルは20msごとに処理が実行されるようで
磁気が強い状態が継続する場合にはカウントアップしないようにフラグを入れたのだけれど・・・

どうやら磁力に変化がない状態でもセンサーの値が小さくなることがある様子で
予期しないタイミングでカウントアップされる症状が発生しました
なので 1サイクルだけ基準を下回っただけではカウント対象にならないようにフラグの使い方を調整
image.png
とりあえず誤作動はなくなりました

距離カウンター

磁気でワンカウントされる度に 外周の距離をカウントするようにして
5mを超える毎にブザーを鳴らすコードを入れてみました

距離もLED表示したいものの
通常 Micro:bit は複数桁の数字はスクロール表示するので
2桁の数字をLED表示する WhaleySansFontを導入してメートル単位での表示にしてみました

image.png
Python

microbit_magnet_mater
Counter = 0
CountFlag = 0
StepLength = 11.5

def on_forever():
    global Counter, CountFlag
    if 400 < abs(input.magnetic_force(Dimension.X)):
        if CountFlag == 0:
            Counter += 1
            if StepLength > Counter * StepLength % 500:
                music.play(music.string_playable("C C A - - - - - ", 400),
                    music.PlaybackMode.IN_BACKGROUND)
        CountFlag = 2
    else:
        if CountFlag == 2:
            CountFlag = 1
        else:
            CountFlag = 0
    whaleysans.show_number(int(Counter * StepLength / 100))
basic.forever(on_forever)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?