最新消息:

Micro:bit创意课程系列:大型跑马灯

Micro Bit 少儿编程 2474浏览 0评论

今天的专案是在前面一对多的程序上改编成一个大型跑马灯程序。

micro:bit上的LED可显示文字,如果长度超过二个字的文字便会以炮马灯的方式由左到右依序出现。但是micro:bitLED实在太小,一次只能出现一个字母,如果有多个micro:bit就可以组合成一个大型跑马灯。

 

Micro:bit创意课程系列:大型跑马灯

Micro:bit创意课程系列:大型跑马灯

先准备一个micro:bit当做控制器,将以下的程序安装进这个micro:bit里面,然后让这个控制器先开好机,跑马灯的micro:bit开机后就会向这个控制器要求给定一个流水编号。控制器按A键或B键可选择大型跑马灯要显示的字符串,A+B键一起按,则会将选定好的字符串送给跑马灯micro:bit来显示。要显示的字符串请自行修改以下的程序代码的最后一行数组值:

let arrayStr: string[] = []

let myBits: string[] = []

let selectedStr = 0

input.onButtonPressed(Button.B, () => {

selectedStr += 1

if (selectedStr > arrayStr.length – 1) {

selectedStr = arrayStr.length – 1

}

basic.showNumber(selectedStr + 1)

})

input.onButtonPressed(Button.A, () => {

selectedStr += 0 – 1

if (selectedStr < 0) {

selectedStr = 0

}

basic.showNumber(selectedStr + 1)

})

radio.onDataPacketReceived(({receivedString: receivedName, receivedNumber: receivedValue }) => {

if (receivedValue == -1) {

myBits.push(receivedName)

radio.sendValue(receivedName, myBits.length – 1)

basic.showNumber(myBits.length)

}

})

input.onButtonPressed(Button.AB, () => {

radio.sendValue(“bits”, myBits.length)

basic.pause(1)

radio.sendValue(arrayStr[selectedStr], -2)

})

radio.setGroup(1)

myBits = []

selectedStr = -1

//可修改、增删以下的字符串数组,让跑马灯有更多字符串可供显示

arrayStr = ["micro:bit","Hello World!", "Yes, I got it!"]

再准备多个micro:bit当做要显示文字的跑马灯,将以下的程序安装进这些个micro:bit跑马灯里面,要先将上面所介绍的控制端的micro:bit先开机,然后依序打开跑马灯micro:bit,以取得自身的顺序编号,才会依照正确的顺序显示文字。

let myString = “”

let bits = 0

let myIndex = 0

radio.onDataPacketReceived(({receivedString: receivedName, receivedNumber: receivedValue }) => {

if (receivedName == “” + control.deviceSerialNumber()) {

myIndex = receivedValue

basic.showNumber(myIndex)

}else if (receivedName == “bits”) {

bits = receivedValue

}else if (receivedValue == -2) {

myString = receivedName+”  ”

basic.clearScreen()

marquee()

}

})

function marquee() {

led.setBrightness(0)

basic.showString(myString.charAt(myIndex))

for (let i = 0; i < 200; i++) {

led.setBrightness(i)

basic.pause(1)

}

basic.pause(1800)

basic.showString(myString.substr(myIndex + 1))

}

basic.showString(“?”)

radio.setGroup(1)

myIndex = -1

myString = “”

bits = 0

radio.sendValue(“” +control.deviceSerialNumber(), myIndex)

可将程序代码贴于makecode上的javascript页面上就可以转成积木式语法使用

 

Micro:bit创意课程系列:大型跑马灯

Micro:bit创意课程系列:大型跑马灯

参考原文链接

https://www.facebook.com/permalink.php?story_fbid=1477479652287593&id=100000767174283&pnref=story

您必须 登录 才能发表评论!