3980| 1
|
[入门教程] BBC micro:bit|SCIENCE|物质的转换 |
micro:bit/ SCIENCE/ 物质的转换 本文转自micro:bit官方网站 翻译kiki 未经许可请勿转载 基于对编码概念的熟悉程度,该项目大约需要30-45分钟 物料: * 纸板 * 铝箔 * 标记笔(红色和黑色) * 1个micro:bit电池座和2节AAA电池 * 4个鳄鱼夹 不好意思 又要看视频广告了 步骤: 将一块纸板变成湿度和温度的探测器! * 折叠铝箔纸块,并将它们放在纸板周围。 * 将每片箔连接到micro:bit上的相应引脚。 而已! 代码: 你有没有试过代表物质的状态?让我们尝试在视觉上代表基于大气温度的各种物质状态 第1步:变量 为了使物质遵循大气温度的规则,我们需要添加存储数据的变量。然后我们将分配(设置)变量的值。 我们想要将两(2)个变量命名如下:“atmos_temperature”和“temperature”。将变量的值设置为100.修改代码,使其看起来像这样。 [mw_shl_code=applescript,true]let temperature = 100 let atmos_temperature = 100[/mw_shl_code] 第2步:检测固体 [mw_shl_code=applescript,true]let temperature = 0 let atmos_temperature = 0 input.onPinPressed(TouchPin.P2, () => { atmos_temperature = 0 basic.showString("SOLID") }) atmos_temperature = 100 temperature = 100[/mw_shl_code] 第3步:检测液体 [mw_shl_code=applescript,true]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[/mw_shl_code] 第4步:检测气体 [mw_shl_code=applescript,true]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[/mw_shl_code] 第5步:提高温度 [mw_shl_code=applescript,true]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[/mw_shl_code] 第6步:显示温度变化 [mw_shl_code=applescript,true]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[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed