Micro:bit第二集——温控与风扇

let average = 0
let sum = 0
let min = 0
let max = 0
let line = 0
let 转速 = 0
let 温度 = 0
input.onButtonPressed(Button.A, function () {
    basic.pause(2000)
    OLED12864_I2C.showNumber(0, line, line * 2)
    OLED12864_I2C.showString(2, line, "sec")
    OLED12864_I2C.showNumber(7, line, 温度)
    OLED12864_I2C.showNumber(11, line, 转速)
    line += 1
    sum = 温度
    max = 温度
    min = 温度
    for (let i = 0; i < 4; i++) {
        basic.pause(2000)
        OLED12864_I2C.showNumber(0, line, line * 2)
        OLED12864_I2C.showString(2, line, "sec")
        OLED12864_I2C.showNumber(7, line, 温度)
        OLED12864_I2C.showNumber(11, line, 转速)
        line += 1
        sum = sum + 温度
        if (温度 >= max) {
            max = 温度
        }
        if (温度 <= min) {
            min = 温度
        }
    }
    average = Math.floor(sum / 5)
    OLED12864_I2C.showString(0, line, "max")
    OLED12864_I2C.showNumber(5, line, max)
    OLED12864_I2C.showString(8, line, "min")
    OLED12864_I2C.showNumber(11, line, min)
    OLED12864_I2C.showString(0, line + 1, "average")
    OLED12864_I2C.showNumber(9, line + 1, average)
})
input.onButtonPressed(Button.B, function () {
    OLED12864_I2C.init()
    line = 1
    max = 0
    min = 99
    sum = 0
    average = 0
    OLED12864_I2C.showString(0, 0, "start")
    OLED12864_I2C.showString(7, 0, "tem")
    OLED12864_I2C.showString(11, 0, "speed")
})
OLED12864_I2C.init()
line = 1
max = 0
min = 99
sum = 0
average = 0
OLED12864_I2C.showString(0, 0, "start")
OLED12864_I2C.showString(7, 0, "tem")
OLED12864_I2C.showString(11, 0, "speed")
basic.forever(function () {
    温度 = input.temperature()
    转速 = 温度 * 5
    pins.analogWritePin(AnalogPin.P1, 转速)
})

  今天给大家介绍microbit的温控与风扇,要求是

1.每两秒测一次温度,十秒内测5次

2.风扇转速按温度改变而改变

3.计算每两秒的温度平均值,十秒的最大值,最小值

上面是Javascript的源代码,大家可以到源软件里去转换成块状的看哦~

(温馨提示:本次用的网站是ide.ithingedu.com,不是原来的!!!)

原文地址:https://www.cnblogs.com/lucasyy/p/13233012.html