KIKIYA 发表于 2019-1-29 15:52:01

BBC micro:bit|SCIENCE|物质的转换

micro:bit/ SCIENCE/ 物质的转换本文转自micro:bit官方网站翻译kiki 未经许可请勿转载所需时间:
基于对编码概念的熟悉程度,该项目大约需要30-45分钟

物料:
* 纸板
* 铝箔
* 标记笔(红色和黑色)
* 1个micro:bit电池座和2节AAA电池
* 4个鳄鱼夹不好意思 又要看视频广告了
https://v.qq.com/x/page/n0833r2yi6w.html
步骤:
将一块纸板变成湿度和温度的探测器!
* 折叠铝箔纸块,并将它们放在纸板周围。
* 将每片箔连接到micro:bit上的相应引脚。
而已!

代码:
你有没有试过代表物质的状态?让我们尝试在视觉上代表基于大气温度的各种物质状态

第1步:变量

为了使物质遵循大气温度的规则,我们需要添加存储数据的变量。然后我们将分配(设置)变量的值。
我们想要将两(2)个变量命名如下:“atmos_temperature”和“temperature”。将变量的值设置为100.修改代码,使其看起来像这样。
let temperature = 100
let atmos_temperature = 100

第2步:检测固体

let temperature = 0
let atmos_temperature = 0
input.onPinPressed(TouchPin.P2, () => {
    atmos_temperature = 0
    basic.showString("SOLID")
})
atmos_temperature = 100
temperature = 100

第3步:检测液体
let temperature = 0
let atmos_temperature = 0
input.onPinPressed(TouchPin.P2, () => {
    atmos_temperature = 0
    basic.showString("SOLID")
})
input.onPinPressed(TouchPin.P1, () => {
    atmos_temperature = 80
    basic.showString("LIQUID")
})
atmos_temperature = 100
temperature = 100

第4步:检测气体
let atmos_temperature = 0
let temperature = 0
input.onPinPressed(TouchPin.P0, () => {
    atmos_temperature = 250
    basic.showString("GAS")
})
input.onPinPressed(TouchPin.P2, () => {
    atmos_temperature = 0
    basic.showString("SOLID")
})
input.onPinPressed(TouchPin.P1, () => {
    atmos_temperature = 80
    basic.showString("LIQUID")
})
atmos_temperature = 100
temperature = 100

第5步:提高温度
let atmos_temperature = 0
let temperature = 0
input.onGesture(Gesture.Shake, () => {
    temperature += 50
    basic.showIcon(IconNames.Triangle)
})
input.onPinPressed(TouchPin.P0, () => {
    atmos_temperature = 250
    basic.showString("GAS")
})
input.onPinPressed(TouchPin.P2, () => {
    atmos_temperature = 0
    basic.showString("SOLID")
})
input.onPinPressed(TouchPin.P1, () => {
    atmos_temperature = 80
    basic.showString("LIQUID")
})
atmos_temperature = 100
temperature = 100

第6步:显示温度变化

let atmos_temperature = 0
let temperature = 0
input.onGesture(Gesture.Shake, () => {
    temperature += 50
    basic.showIcon(IconNames.Triangle)
})
basic.forever(() => {
    if (temperature < atmos_temperature) {
      temperature += 20
    } else {
      temperature += -20
    }
    if (temperature < 32) {
      basic.showIcon(IconNames.Square)
    } else if (temperature < 212) {
      basic.showIcon(IconNames.Umbrella)
    } else {
      basic.showIcon(IconNames.Chessboard)
    }
    basic.clearScreen()
    basic.pause(100)
})
input.onPinPressed(TouchPin.P0, () => {
    atmos_temperature = 250
    basic.showString("GAS")
})
input.onPinPressed(TouchPin.P2, () => {
    atmos_temperature = 0
    basic.showString("SOLID")
})
input.onPinPressed(TouchPin.P1, () => {
    atmos_temperature = 80
    basic.showString("LIQUID")
})
atmos_temperature = 100
temperature = 100

gada888 发表于 2019-2-16 08:41:41

有趣
页: [1]
查看完整版本: BBC micro:bit|SCIENCE|物质的转换