2024年7月22日 星期一

micro:bit控制雙顆16個光環環板控制程式以及王米葉編織的花

 

  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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def rainbow2():
    global ShiftCount
    strip.show()
    strip2.show()
    strip.shift(1)
    strip2.shift(1)
    basic.pause(100)
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    if ShiftCount == 0:
        strip.show_rainbow(1, 360)
        strip2.show_rainbow(1, 360)
def rainbow3():
    global range3, range2, ShiftCount
    strip.clear()
    strip2.clear()
    range3 = strip.range(0, ShiftCount)
    range2 = strip2.range(0, ShiftCount)
    range3.show_rainbow(1, 360)
    range2.show_rainbow(1, 360)
    range3.show()
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(100)
def monochrome():
    if submode == 0:
        strip.show_color(neopixel.colors(NeoPixelColors.RED))
        strip2.show_color(neopixel.colors(NeoPixelColors.RED))
    elif submode == 1:
        strip.show_color(neopixel.colors(NeoPixelColors.ORANGE))
        strip2.show_color(neopixel.colors(NeoPixelColors.ORANGE))
    elif submode == 2:
        strip.show_color(neopixel.colors(NeoPixelColors.YELLOW))
        strip2.show_color(neopixel.colors(NeoPixelColors.YELLOW))
    elif submode == 3:
        strip.show_color(neopixel.colors(NeoPixelColors.GREEN))
        strip2.show_color(neopixel.colors(NeoPixelColors.GREEN))
    elif submode == 4:
        strip.show_color(neopixel.colors(NeoPixelColors.BLUE))
        strip2.show_color(neopixel.colors(NeoPixelColors.BLUE))
    elif submode == 5:
        strip.show_color(neopixel.colors(NeoPixelColors.INDIGO))
        strip2.show_color(neopixel.colors(NeoPixelColors.INDIGO))
    elif submode == 6:
        strip.show_color(neopixel.colors(NeoPixelColors.VIOLET))
        strip2.show_color(neopixel.colors(NeoPixelColors.VIOLET))
    elif submode == 7:
        strip.show_color(neopixel.colors(NeoPixelColors.PURPLE))
        strip2.show_color(neopixel.colors(NeoPixelColors.PURPLE))
    elif submode == 8:
        strip.show_color(neopixel.colors(NeoPixelColors.WHITE))
        strip2.show_color(neopixel.colors(NeoPixelColors.WHITE))
    elif submode == 9:
        strip.show_color(neopixel.colors(NeoPixelColors.BLACK))
        strip2.show_color(neopixel.colors(NeoPixelColors.BLACK))
    strip.show()

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

def loudness():
    strip.clear()
    strip2.clear()
    strip.show_bar_graph(Math.map(input.sound_level(), 0, 255, 0, 16), 16)
    strip2.show_bar_graph(Math.map(input.sound_level(), 0, 255, 0, 16), 16)
    strip.show()
    strip2.show()
def rainbow5():
    global range3, range2, ShiftCount
    strip.clear()
    strip2.clear()
    range3 = strip.range(randint(0, Number2 - 1), 1)
    range2 = strip2.range(randint(0, Number2 - 1), 1)
    range3.show_color(randint(0, 65535))
    range2.show_color(randint(0, 65535))
    range3.show()
    range2.show()
    ShiftCount = (ShiftCount + 1) % (Number2 + 1)
    basic.pause(200)
def rainbow():
    strip.rotate(3)
    strip2.rotate(3)
    strip.show()
    strip2.show()
    basic.pause(100)
def rainbow4():
    global range3, range2, ShiftCount
    strip.clear()
    strip2.clear()
    range3 = strip.range(ShiftCount, 1)
    range2 = strip2.range(ShiftCount, 1)
    range3.show_rainbow(1, 360)
    range2.show_rainbow(1, 360)
    range3.show()
    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()
    strip2.clear()
    strip.show_bar_graph(Math.map(input.light_level(), 0, 255, 0, 16), 16)
    strip2.show_bar_graph(Math.map(input.light_level(), 0, 255, 0, 16), 16)
    strip.show()
    strip2.show()
range2: neopixel.Strip = None
range3: neopixel.Strip = None
strip2: neopixel.Strip = None
strip: neopixel.Strip = None
ShiftCount = 0
Number2 = 0
submode = 0
maxMode = 0
mode = 0
mode = 2
maxMode = 10
submode = 0
Number2 = 16
ShiftCount = 0
strip = neopixel.create(DigitalPin.P0, Number2, NeoPixelMode.RGB)
strip2 = neopixel.create(DigitalPin.P1, Number2, NeoPixelMode.RGB)
basic.show_string("" + str(mode))

def on_forever():
    global submode
    if mode == 0:
        strip.clear()
        strip2.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)

沒有留言:

張貼留言