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网站上便会以积木的方式呈现。
作者:刘正吉
更多精彩文章请关注微信公众号易心microbit编程
易心STEAM编程