微信公众号
蘑菇云创造


微信交流群

定时门/Timing gates
将两个门链接到micro:bit,就可以检测到通过的车辆。
当汽车通过 闸门0 时,它通过 on pin pressed 发送一个事件到micro:bit。micro:bit在变量中记录时间t0。
当车通过 闸门1 的时候,它通过 on pin pressed 发送一个事件到micro:bit。micro:bit在变量中记录时间t1。
其余的是一些数学和物理知识。穿过门的时间计算为t1-t0。通过将门之间的距离除以持续时间,我们得到了车的速度!
代码
在LED屏幕上显示图片
参数
leds是一个字符串,用于控制打开和关闭哪些LED。
interval是一个可选数字,表示显示图片后等待多少毫秒。如果使用块编程,interval则设置为400毫秒。
例
该程序显示具有该show leds功能的图片。
参数
let count = 0
basic.showNumber(count, 100)
input.onPinPressed(TouchPin.P0, () => {
count = count + 1
basic.showNumber(count, 100)
})
let item = 5
basic.showNumber(item)
在变量中存储字符串
let name = "Joe"
basic.showString(name);
input.onButtonPressed(Button.B, () => {
let now = input.runningTime()
basic.showNumber(now)
})
basic.showNumber(10)
要显示存储在变量中的数字:
let x = 1
basic.showNumber(x)
示例:计数到5
for (let i = 0; i < 6; i++) {
basic.showNumber(i)
basic.pause(200)
}
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
input.onPinPressed(TouchPin.P0, () => {
basic.showLeds(`
# . . . .
# . . . .
# . . . .
# . . . .
# . . . .
`)
})
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
input.onPinPressed(TouchPin.P0, () => {
basic.showLeds(`
# . . . .
# . . . .
# . . . .
# . . . .
# . . . .
`)
})
input.onPinPressed(TouchPin.P1, () => {
basic.showLeds(`
# . . . #
# . . . #
# . . . #
# . . . #
# . . . #
`)
})
let t0 = 0;
let t1 = 0;
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
input.onPinPressed(TouchPin.P0, () => {
t0 = control.eventTimestamp();
basic.showLeds(`
# . . . .
# . . . .
# . . . .
# . . . .
# . . . .
`)
})
input.onPinPressed(TouchPin.P1, () => {
t1 = control.eventTimestamp();
basic.showLeds(`
# . . . #
# . . . #
# . . . #
# . . . #
# . . . #
`)
let d = t1 - t0
basic.showNumber(d)
})
测量门之间的距离并应用牛顿定律来计算汽车的速度(它的速度)。
v = d / t