zoukankan      html  css  js  c++  java
  • 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运行

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

  • 相关阅读:
    Ubuntu 14.04设置开机启动脚本的方法
    python 筛选
    分段压缩
    ubuntu 16.04 启用root用户方法
    Ubuntu 16.04 设置MySQL远程访问权限
    [分享]在ubuntu9.10下实现开机自动登录并运行自己的图形程序
    ubuntu live cd修复grub引导项
    安装dcm4chee-arc-light-5.4.1-mysql步骤
    数据库学习--wildfly配置postgreSQL数据源
    wildfly配置PostgreSQL数据源
  • 原文地址:https://www.cnblogs.com/coding365/p/12872396.html
Copyright © 2011-2022 走看看