【12 DAYS OF TECHMAS 】micro:bit扩展板-圣诞节装饰品
【12 DAYS OF TECHMAS 】Christmas Ornament-圣诞节装饰品
On the first day of Christmas, my true love
gave to me a partridge in a pear tree
On the second day of Christmas, my true love
gave to me two turtledoves and a partridge in a pear tree
·········
一首12days of Christmas送给这个圣诞节。祝福的音乐响起来了,
空气里处处洋溢着暖心的声音,宣告着圣诞节也已经来到身边。
micro:bit RGB 全彩LED灯环扩展板做的圣诞节饰品
我们的一个日本用户利用micro:bit RGB 全彩LED灯环扩展板制作了一件装饰品。
整个过程花费数个小时。
按下A按钮后,圣诞装饰品会播放歌曲并呈现彩色符号,其中歌曲通过micro:bit RGB 全彩LED灯环扩展板的蜂鸣器播放,符号则通过红绿光矩阵演示。
按下B按钮可进入K歌模式。我使用了带扩展板的麦克风。当检测到声音时,麦克风发出黄光, "Merry Xmas"会在屏幕上显示。
MakeCode
使用了MakeCode进行编码。
MakeCode模块
以下是这个圣诞装饰物的模块:
https://hackster.imgix.net/uploads/attachments/683242/_2018-12-08_17_40_06_fUvlHVE59f.png?auto=compress%2Cformat&w=740&h=555&fit=max变量https://hackster.imgix.net/uploads/attachments/683243/_2018-12-08_17_44_17_cr8iv2QKL9.png?auto=compress%2Cformat&w=740&h=555&fit=max
变量、输入引脚、单元数和亮度的初始化模块,以用于指定的回放旋律。
A模式
按下A按钮,执行此模式。
2.按钮检测https://hackster.imgix.net/uploads/attachments/683245/_2018-12-08_17_47_22_jreicPYNqD.png?auto=compress%2Cformat&w=740&h=555&fit=max按下A按钮,变量状态将设置为1。
3.照明设定https://hackster.imgix.net/uploads/attachments/683246/_2018-12-08_17_49_09_lCJrCXb41P.png?auto=compress%2Cformat&w=740&h=555&fit=max当变量状态为1时(按下A按钮),扩展板会交替重复闪烁红/绿光。
4. LED矩阵设置https://hackster.imgix.net/uploads/attachments/683249/_2018-12-08_17_50_26_CD5GT1Nt4G.png?auto=compress%2Cformat&w=740&h=555&fit=max当变量状态为1时(按下A按钮),两个音符在micro:bit上交替显示。
5. 音频播放当变量状态为1时(按下A按钮),通过扩展板蜂鸣器重复播放这些歌曲。当变量状态变为非1的其它数值时,蜂鸣器将退出循环,并停止播放声音。
B模式
按下B按钮时执行此操作。
6. B按钮检测按下B键,变量状态将设置为0,并重置矩阵显示。
7. 声音检测https://hackster.imgix.net/uploads/attachments/683258/_2018-12-08_17_58_16_Yj8z8zxB4W.png?auto=compress%2Cformat&w=740&h=555&fit=max重置扩展板,并打开MIC检测功能。
当通过MIC检测到声音(模拟值超过200)时,将变量状态设置为2,并亮起9个黄灯。
当变量状态为2时(在语音检测时),它将旋转显示9个黄灯。
8. LED矩阵设置
当变量状态为2时(当检测到语音时),字符串将显示在矩阵中。当显示完成时,可变状态设置为0,并等待语音检测。https://hackster.imgix.net/uploads/attachments/683262/_2018-12-08_18_05_10_rwQzbMHKps.png?auto=compress%2Cformat&w=740&h=555&fit=max9.重置同时按下A按钮和B按钮进行复位,LED矩阵将停止运行。
https://hackster.imgix.net/uploads/attachments/683263/_2018-12-08_18_06_13_4yaRVV4aAs.png?auto=compress%2Cformat&w=740&h=555&fit=max
代码:let sound = 0
let state = 0
input.onButtonPressed(Button.A, function () {
state = 1
})
input.onButtonPressed(Button.B, function () {
state = 0
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
input.onButtonPressed(Button.AB, function () {
state = 3
strip.clear()
strip.show()
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
let range: neopixel.Strip = null
let strip: neopixel.Strip = null
state = 3
strip = neopixel.create(DigitalPin.P2, 24, NeoPixelMode.RGB)
strip.setBrightness(30)
music.setTempo(52)
basic.forever(function () {
while (state == 1) {
strip.clear()
for (let index = 0; index < 24; index+=2) {
range = strip.range(index, 1)
range.showColor(neopixel.colors(NeoPixelColors.Red))
}
for (let index2 = 1; index2 <= 24; index2+=2) {
range = strip.range(index2, 1)
range.showColor(neopixel.colors(NeoPixelColors.Green))
}
basic.pause(500)
for (let index3 = 0; index3 < 24; index3+=2) {
range = strip.range(index3, 1)
range.showColor(neopixel.colors(NeoPixelColors.Green))
}
for (let index22 = 1; index22 <= 24; index22+=2) {
range = strip.range(index22, 1)
range.showColor(neopixel.colors(NeoPixelColors.Red))
}
basic.pause(500)
}
})
basic.forever(function () {
while (state == 1) {
basic.showLeds(`
. . # . .
. . # # .
. . # . #
# # # . .
# # # . .
`)
basic.pause(500)
if (!(state)) {
break
}
basic.showLeds(`
. # # # #
. # . . #
. # . . #
# # . # #
# # . # #
`)
basic.pause(500)
}
})
basic.forever(function () {
while (state == 1) {
music.playTone(196, music.beat(BeatFraction.Quarter))
music.playTone(330, music.beat(BeatFraction.Quarter))
music.playTone(294, music.beat(BeatFraction.Quarter))
music.playTone(262, music.beat(BeatFraction.Quarter))
if (!(state)) {
break
}
music.playTone(196, music.beat(BeatFraction.Whole))
music.playTone(196, music.beat(BeatFraction.Quarter))
music.playTone(330, music.beat(BeatFraction.Quarter))
music.playTone(294, music.beat(BeatFraction.Quarter))
music.playTone(262, music.beat(BeatFraction.Quarter))
if (!(state)) {
break
}
music.playTone(220, music.beat(BeatFraction.Whole))
music.playTone(220, music.beat(BeatFraction.Quarter))
music.playTone(349, music.beat(BeatFraction.Quarter))
music.playTone(330, music.beat(BeatFraction.Quarter))
music.playTone(294, music.beat(BeatFraction.Quarter))
if (!(state)) {
break
}
music.playTone(247, music.beat(BeatFraction.Whole))
music.playTone(392, music.beat(BeatFraction.Quarter))
music.playTone(392, music.beat(BeatFraction.Quarter))
music.playTone(349, music.beat(BeatFraction.Quarter))
music.playTone(294, music.beat(BeatFraction.Quarter))
if (!(state)) {
break
}
music.playTone(330, music.beat(BeatFraction.Whole))
}
})
basic.forever(function () {
if (state == 0) {
strip.clear()
strip.show()
sound = pins.analogReadPin(AnalogPin.P1)
if (sound > 200) {
state = 2
strip.range(0, 9).showColor(neopixel.colors(NeoPixelColors.Yellow))
}
}
if (state == 2) {
strip.rotate(1)
strip.show()
basic.pause(50)
}
})
basic.forever(function () {
if (state == 2) {
basic.showString("Merry Xmas")
strip.clear()
strip.show()
state = 0
}
})
运行用该饰品装扮圣诞树可点缀节日气氛,非常可爱。
https://v.qq.com/x/page/v0815rpm1jp.html
酷 美 真想动手制作一个 好设备,元旦攒一个。。。。。 真是可爱学习 不错啊,刚入手一块,打算玩下
页:
[1]