2024年7月22日 星期一

micro:bit 十種模式的燈條控制

這個程式控制一個 NeoPixel 燈條,實現了 10 種不同的顯示模式。以下是每個模式的解釋:

  • 模式 0: 清除顯示

清除燈條上的顏色,將所有燈熄滅。

  • 模式 1: 單色顯示

根據 submode 的值顯示不同的顏色。顏色從紅色開始,依次是橙色、黃色、綠色、藍色、靛藍色、紫色、紫羅蘭色、白色和黑色。

  • 模式 2: 單色顯示(自動變換顏色)

與模式 1 相同,但每 500 毫秒 submode 自動增加,改變顏色。

  • 模式 3: 彩虹旋轉

燈條上的顏色進行旋轉。

  • 模式 4: 移動彩虹

彩虹顏色向左移動,並在特定條件下重置顯示。

  • 模式 5: 部分顯示彩虹

在燈條的前部分顯示彩虹,範圍根據 ShiftCount 增加。

  • 模式 6: 單個 LED 顯示彩虹

每次僅在一個 LED 上顯示彩虹顏色,位置根據 ShiftCount 變化。

  • 模式 7: 隨機顏色顯示

隨機選擇一個 LED 顯示隨機顏色。

  • 模式 8: 聲音圖表

根據環境聲音的強度顯示條形圖。

  • 模式 9: 亮度圖表

根據環境光亮度顯示條形圖。


按鈕控制:

  • 按鈕 A:

切換模式,每次按下時 mode 增加 1,顯示當前模式。

  • 按鈕 B:

切換 submode,每次按下時 submode 增加 1,影響單色顯示的顏色。


主循環:

根據當前的 mode 執行對應的顯示模式。


積木程式:


JavaScript程式:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function rainbow2 () {
    strip.show()
    strip.shift(1)
    basic.pause(100)
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    if (ShiftCount == 0) {
        strip.showRainbow(1, 360)
    }
}
function rainbow3 () {
    strip.clear()
    range = strip.range(0, ShiftCount)
    range.showRainbow(1, 360)
    range.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(100)
}
function monochrome () {
    if (submode == 0) {
        strip.showColor(neopixel.colors(NeoPixelColors.Red))
    } else if (submode == 1) {
        strip.showColor(neopixel.colors(NeoPixelColors.Orange))
    } else if (submode == 2) {
        strip.showColor(neopixel.colors(NeoPixelColors.Yellow))
    } else if (submode == 3) {
        strip.showColor(neopixel.colors(NeoPixelColors.Green))
    } else if (submode == 4) {
        strip.showColor(neopixel.colors(NeoPixelColors.Blue))
    } else if (submode == 5) {
        strip.showColor(neopixel.colors(NeoPixelColors.Indigo))
    } else if (submode == 6) {
        strip.showColor(neopixel.colors(NeoPixelColors.Violet))
    } else if (submode == 7) {
        strip.showColor(neopixel.colors(NeoPixelColors.Purple))
    } else if (submode == 8) {
        strip.showColor(neopixel.colors(NeoPixelColors.White))
    } else if (submode == 9) {
        strip.showColor(neopixel.colors(NeoPixelColors.Black))
    }
    strip.show()
}
input.onButtonPressed(Button.A, function () {
    mode = (mode + 1) % maxMode
    basic.showString("" + (mode))
    if (mode == 3 && mode <= 4) {
        strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
        strip.showRainbow(1, 360)
    } else if (mode >= 5) {
        strip.clear()
        strip.show()
    }
})
function loudness () {
    strip.clear()
    strip.showBarGraph(Math.map(input.soundLevel(), 0, 255, 0, 16), 16)
    strip.show()
}
function rainbow5 () {
    strip.clear()
    range = strip.range(randint(0, Number2 - 1), 1)
    range.showColor(randint(0, 65535))
    range.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(200)
}
function rainbow () {
    strip.rotate(3)
    strip.show()
    basic.pause(100)
}
function rainbow4 () {
    strip.clear()
    range = strip.range(ShiftCount, 1)
    range.showRainbow(1, 360)
    range.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(100)
}
input.onButtonPressed(Button.B, function () {
    submode = (submode + 1) % 10
})
function brightness () {
    strip.clear()
    strip.showBarGraph(Math.map(input.lightLevel(), 0, 255, 0, 16), 16)
    strip.show()
}
let range: neopixel.Strip = null
let strip: neopixel.Strip = null
let ShiftCount = 0
let Number2 = 0
let submode = 0
let maxMode = 0
let mode = 0
mode = 0
maxMode = 10
submode = 0
Number2 = 16
ShiftCount = 0
strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
basic.showString("" + (mode))
basic.forever(function () {
    if (mode == 0) {
        strip.clear()
        strip.show()
    } else if (mode == 1) {
        monochrome()
    } else if (mode == 2) {
        monochrome()
        basic.pause(500)
        submode = (submode + 1) % 10
    } else if (mode == 3) {
        rainbow()
    } else if (mode == 4) {
        rainbow2()
    } else if (mode == 5) {
        rainbow3()
    } else if (mode == 6) {
        rainbow4()
    } else if (mode == 7) {
        rainbow5()
    } else if (mode == 8) {
        loudness()
    } else {
        brightness()
    }
})

Python程式:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def rainbow2():
    global ShiftCount
    strip.show()
    strip.shift(1)
    basic.pause(100)
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    if ShiftCount == 0:
        strip.show_rainbow(1, 360)
def rainbow3():
    global range2, ShiftCount
    strip.clear()
    range2 = strip.range(0, ShiftCount)
    range2.show_rainbow(1, 360)
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(100)
def monochrome():
    if submode == 0:
        strip.show_color(neopixel.colors(NeoPixelColors.RED))
    elif submode == 1:
        strip.show_color(neopixel.colors(NeoPixelColors.ORANGE))
    elif submode == 2:
        strip.show_color(neopixel.colors(NeoPixelColors.YELLOW))
    elif submode == 3:
        strip.show_color(neopixel.colors(NeoPixelColors.GREEN))
    elif submode == 4:
        strip.show_color(neopixel.colors(NeoPixelColors.BLUE))
    elif submode == 5:
        strip.show_color(neopixel.colors(NeoPixelColors.INDIGO))
    elif submode == 6:
        strip.show_color(neopixel.colors(NeoPixelColors.VIOLET))
    elif submode == 7:
        strip.show_color(neopixel.colors(NeoPixelColors.PURPLE))
    elif submode == 8:
        strip.show_color(neopixel.colors(NeoPixelColors.WHITE))
    elif submode == 9:
        strip.show_color(neopixel.colors(NeoPixelColors.BLACK))
    strip.show()

def on_button_pressed_a():
    global mode, strip
    mode = (mode + 1) % maxMode
    basic.show_string("" + str((mode)))
    if mode == 3 and mode <= 4:
        strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
        strip.show_rainbow(1, 360)
    elif mode >= 5:
        strip.clear()
        strip.show()
input.on_button_pressed(Button.A, on_button_pressed_a)

def loudness():
    strip.clear()
    strip.show_bar_graph(Math.map(input.sound_level(), 0, 255, 0, 16), 16)
    strip.show()
def rainbow5():
    global range2, ShiftCount
    strip.clear()
    range2 = strip.range(randint(0, Number2 - 1), 1)
    range2.show_color(randint(0, 65535))
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(200)
def rainbow():
    strip.rotate(3)
    strip.show()
    basic.pause(100)
def rainbow4():
    global range2, ShiftCount
    strip.clear()
    range2 = strip.range(ShiftCount, 1)
    range2.show_rainbow(1, 360)
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(100)

def on_button_pressed_b():
    global submode
    submode = (submode + 1) % 10
input.on_button_pressed(Button.B, on_button_pressed_b)

def brightness():
    strip.clear()
    strip.show_bar_graph(Math.map(input.light_level(), 0, 255, 0, 16), 16)
    strip.show()
range2: neopixel.Strip = None
strip: neopixel.Strip = None
ShiftCount = 0
Number2 = 0
submode = 0
maxMode = 0
mode = 0
mode = 0
maxMode = 10
submode = 0
Number2 = 16
ShiftCount = 0
strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
basic.show_string("" + str((mode)))

def on_forever():
    global submode
    if mode == 0:
        strip.clear()
        strip.show()
    elif mode == 1:
        monochrome()
    elif mode == 2:
        monochrome()
        basic.pause(500)
        submode = (submode + 1) % 10
    elif mode == 3:
        rainbow()
    elif mode == 4:
        rainbow2()
    elif mode == 5:
        rainbow3()
    elif mode == 6:
        rainbow4()
    elif mode == 7:
        rainbow5()
    elif mode == 8:
        loudness()
    else:
        brightness()
basic.forever(on_forever)

沒有留言:

張貼留言