micro:bit用蜂鸣器制作计时器

micro:bit小主板一般初学Python和图形编程的人都很喜欢,其中还有很多插件,声呐等。
今天我来教大家micro:bit连接蜂鸣器制作计时器。
1.蜂鸣器的连接方法。
打开micro:bit的盒子,拿出蜂鸣器,蜂鸣器正极用鳄鱼钳夹住,另一端连接micro:bit引脚GND。蜂鸣器另一接口用鳄鱼钳夹住,另一端连接micro:bit引脚P0
在这里插入图片描述
输入micro:bit的Python代码。

input.onButtonPressed(Button.A, function () {
    set_time += 1
    basic.showNumber(set_time)
    basic.clearScreen()
})
input.onButtonPressed(Button.AB, function () {
    in_time = set_time * 60
    in_time = in_time + set_time_m
    basic.showIcon(IconNames.Diamond)
    for (let index = 0; index < in_time; index++) {
        if (循环变量 >= in_time - 10) {
            j = 0
            if (循环变量 == in_time - 10) {
                basic.showLeds(`
                    # . # # #
                    # . # . #
                    # . # . #
                    # . # . #
                    # . # # #
                    `)
            } else {
                basic.showNumber(j)
                j += -1
            }
            music.playTone(1000, music.beat(BeatFraction.Quarter))
            basic.pause(500)
        } else {
            basic.pause(1000)
        }
        循环变量 += 1
    }
    music.beginMelody(music.builtInMelody(Melodies.BaDing), MelodyOptions.Forever)
})
input.onButtonPressed(Button.B, function () {
    set_time_m += 1
    basic.showNumber(set_time_m)
    basic.clearScreen()
})
input.onGesture(Gesture.ScreenDown, function () {
    music.stopMelody(MelodyStopOptions.All)
})
let j = 0
let 循环变量 = 0
let set_time_m = 0
let in_time = 0
let set_time = 0
basic.showLeds(`
    # . . . #
    . . # . .
    . # . # .
    . . # . .
    # . . . #
    `)
set_time = 0
in_time = 0
set_time_m = 0
let i = 0
循环变量 = 0
j = 9
basic.forever(function () {
    led.setBrightness(i)
    i += 50
    basic.pause(100)
    if (i == 250) {
        for (let index = 0; index < 5; index++) {
            i += -50
            basic.pause(100)
            led.setBrightness(255)
        }
    }
})

下载.hex文件,发送到micro:bit运行

在这里插入图片描述
在这里插入图片描述

posted @ 2020-02-02 14:14  Aeterna_Gungnir  阅读(450)  评论(0编辑  收藏  举报