micro:bit没有网络功能,但具有群组广播的功能,如能善用这个功能,也可以帮每一个手头拥有的micro:bit寻址。今日的micro:bit专案便是让micro:bit来报数。先选定一个micro:bit当作是服务器,其他的micro:bit开机后按A键,便能向伺服端的micro:bit注册,再由伺服端micro:bit给定一个流水编号传送给client端的micro:bit便完成了寻址的动作。伺服端的micro:bit按A键可以让client的micro:bit从1开始报数,按B可以让client的micro:bit倒数报数。若clinet端的某几个micro:bit关掉了或是移走了,伺服端的micro:bit可以按A+B键,让剩下的micro:bit重新取得编号来寻址。
让micro:bit寻址有什么好处?能够寻址让我们可以一对多的控制所有的micro:bit,一起组合成某种大型的LED图案或是动画,或是clinet端装上扩展板,伺服端就能控制某个client的马达前进或后退,或是完成更多群组化的机器人动作。
client端的板子会呈现自己的流水编号,伺服端的板子则会呈现现在受控制的板子数量
编号寻址过的micro:bit可程序呈现组合后的动画,或是可受伺服端的micro:bit控制
micro:bit报数程控方式
client端:按A键会向服务器micro:bit取得自己的编号
伺服端:
1.按A键会从1开始报数
2.按B键会倒数报数
3.A+B键会重新设定micro:bit的client端的编号(有需要时才会用到)
4.按P0脚位会呈现简单动画:会出现向右的箭头由1号一直传递到最后一号
5.按P2脚位会呈现简单动画:会出现向左的箭头由最后一号传递到1号
程序内容:以javascrip的语法呈现,
伺服端程序如下:
let show = 0
let countBits = 0
let myBits: string[] = []
let bitsTemp: string[] = []
let temp = 0
input.onButtonPressed(Button.A, () => {
radio.sendValue("clear", 0)
basic.pause(1)
temp = 0
show = 1
radio.sendValue("count", 0)
})
radio.onDataPacketReceived( ({receivedString: name, receivedNumber: bitValue }) => {
if (bitValue == -1) {
myBits.push(name)
radio.sendValue(name, countBits)
countBits += 1
if (temp == 0) {
basic.showNumber(countBits)
}
}
if (name == "getShow") {
radio.sendValue("show", show)
}
})
input.onButtonPressed(Button.AB, () => {
temp = 1
bitsTemp = []
countBits = 0
basic.showNumber(countBits)
show = 0
bitsTemp = myBits
myBits = []
radio.sendValue("reset", -1)
basic.pause(1)
radio.sendValue("show", show)
basic.pause(1)
for (let index = 0; index <= bitsTemp.length - 1; index++) {
radio.sendValue("response", parseInt(bitsTemp[index]))
basic.pause(1)
}
basic.showNumber(countBits)
show = 1
radio.sendValue("show", show)
basic.pause(1)
temp = 0
})
input.onPinPressed(TouchPin.P0, () => {
radio.sendValue("clear", 0)
basic.pause(200)
radio.sendValue("arrowR", 0)
})
input.onPinPressed(TouchPin.P2, () => {
radio.sendValue("clear", 0)
basic.pause(200)
radio.sendValue("arrowL", countBits - 1)
})
input.onButtonPressed(Button.B, () => {
radio.sendValue("clear", 0)
basic.pause(1)
temp = 0
show = 1
radio.sendValue("countDown", countBits - 1)
})
basic.showLeds(`
## # # #
## # # #
## # # #
## # # #
## # # #
`)
radio.setGroup(1)
show = 0
countBits = 0
temp = 0
client端的程序如下:
let myIndex = 0
let show = false
input.onButtonPressed(Button.A, () => {
radio.sendValue("getShow", 0)
basic.pause(50)
radio.sendValue("" + control.deviceSerialNumber(), myIndex)
})
radio.onDataPacketReceived( ({receivedString: name, receivedNumber: value }) => {
if (name == "count" && value == myIndex) {
show = true
basic.pause(300)
radio.sendValue("count", value + 1)
showMyNum()
}
if (name == "countDown" && value == myIndex) {
show = true
basic.pause(300)
if (myIndex > 0) {
radio.sendValue("countDown", value - 1)
}
showMyNum()
}
if (name == "" + control.deviceSerialNumber()) {
myIndex = value
showMyNum()
}
if (name == "show") {
show = value == 1
showMyNum()
}
if (name == "response" && value ==control.deviceSerialNumber()) {
radio.sendValue("" + control.deviceSerialNumber(), myIndex)
}
if (name == "reset") {
myIndex = value
showMyNum()
}
if (name == "clear") {
basic.clearScreen()
}
if (name == "arrowR" && value == myIndex) {
basic.showArrow(ArrowNames.East)
radio.sendValue("arrowR", value + 1)
basic.clearScreen()
}
if (name == "arrowL" && value == myIndex) {
basic.showArrow(ArrowNames.West)
if (myIndex > 0) {
radio.sendValue("arrowL", value - 1)
}
basic.clearScreen()
}
})
function showMyNum() {
if (myIndex < 0) {
basic.showString("?")
}else {
if (show) {
basic.showNumber(myIndex + 1)
} else {
basic.showIcon(IconNames.Triangle)
}
}
}
basic.showString("?")
radio.setGroup(1)
myIndex = -1
show = false
radio.sendValue("getShow", 0)
将上面的javascript程序代码贴到makecode网站上便会以积木的方式呈现。