2024年12月4日 星期三

[樹藝:micro:bit] 時尚兔玩AI

 

主要材料:樹藝、micro:bit、Smart AI Lens

MakeCode程式碼:


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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def on_button_pressed_a():
    global ID
    ID = (ID + 1) % 6
    basic.show_string("" + str((ID)))
    if ID == 0:
        PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)
    else:
        PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.THINGS)
    basic.pause(3000)
input.on_button_pressed(Button.A, on_button_pressed_a)

def doLight():
    if RUN == 0 or (RUN == 5 or RUN == 11):
        pins.digital_write_pin(DigitalPin.P1, 1)
        pins.digital_write_pin(DigitalPin.P2, 0)
        pins.digital_write_pin(DigitalPin.P13, 0)
        pins.digital_write_pin(DigitalPin.P14, 0)
        pins.digital_write_pin(DigitalPin.P15, 0)
    elif RUN == 1 or (RUN == 6 or RUN == 12):
        pins.digital_write_pin(DigitalPin.P1, 0)
        pins.digital_write_pin(DigitalPin.P2, 1)
        pins.digital_write_pin(DigitalPin.P13, 0)
        pins.digital_write_pin(DigitalPin.P14, 0)
        pins.digital_write_pin(DigitalPin.P15, 0)
    elif RUN == 2 or (RUN == 7 or RUN == 13):
        pins.digital_write_pin(DigitalPin.P1, 0)
        pins.digital_write_pin(DigitalPin.P2, 0)
        pins.digital_write_pin(DigitalPin.P13, 1)
        pins.digital_write_pin(DigitalPin.P14, 0)
        pins.digital_write_pin(DigitalPin.P15, 0)
    elif RUN == 3 or (RUN == 8 or RUN == 14):
        pins.digital_write_pin(DigitalPin.P1, 0)
        pins.digital_write_pin(DigitalPin.P2, 0)
        pins.digital_write_pin(DigitalPin.P13, 0)
        pins.digital_write_pin(DigitalPin.P14, 1)
        pins.digital_write_pin(DigitalPin.P15, 0)
    elif RUN == 4 or (RUN == 9 or RUN == 15):
        pins.digital_write_pin(DigitalPin.P1, 0)
        pins.digital_write_pin(DigitalPin.P2, 0)
        pins.digital_write_pin(DigitalPin.P13, 0)
        pins.digital_write_pin(DigitalPin.P14, 0)
        pins.digital_write_pin(DigitalPin.P15, 1)
def checkCard():
    global RUN
    if PlanetX_AILens.number_card(PlanetX_AILens.numberCards.ZERO) and RUN != 0:
        RUN = 0
        radio.send_number(10)
        basic.show_number(0)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.ONE) and RUN != 1:
        RUN = 1
        radio.send_number(1)
        basic.show_number(1)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.TWO) and RUN != 2:
        RUN = 2
        radio.send_number(2)
        basic.show_number(2)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.THREE) and RUN != 3:
        RUN = 3
        radio.send_number(3)
        basic.show_number(3)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.FOUR) and RUN != 4:
        RUN = 4
        radio.send_number(9)
        basic.show_number(4)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.FIVE) and RUN != 5:
        RUN = 5
        radio.send_number(5)
        basic.show_number(5)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.SIX) and RUN != 6:
        RUN = 6
        radio.send_number(4)
        basic.show_number(6)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.SEVEN) and RUN != 7:
        RUN = 7
        radio.send_number(8)
        basic.show_number(7)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.EIGHT) and RUN != 8:
        RUN = 8
        radio.send_number(7)
        basic.show_number(8)
    elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.NINE) and RUN != 9:
        RUN = 9
        radio.send_number(6)
        basic.show_number(9)

def on_button_pressed_b():
    if ID == 1:
        PlanetX_AILens.learn_object(PlanetX_AILens.learnID.ID1)
    elif ID == 2:
        PlanetX_AILens.learn_object(PlanetX_AILens.learnID.ID2)
    elif ID == 3:
        PlanetX_AILens.learn_object(PlanetX_AILens.learnID.ID3)
    elif ID == 4:
        PlanetX_AILens.learn_object(PlanetX_AILens.learnID.ID4)
    elif ID == 5:
        PlanetX_AILens.learn_object(PlanetX_AILens.learnID.ID4)
    basic.pause(6000)
input.on_button_pressed(Button.B, on_button_pressed_b)

def checkLOGO():
    global RUN
    if PlanetX_AILens.object_check(PlanetX_AILens.learnID.ID1) and RUN != 11:
        RUN = 11
        basic.show_string("A")
        radio.send_number(16)
    elif PlanetX_AILens.object_check(PlanetX_AILens.learnID.ID2) and RUN != 12:
        RUN = 12
        basic.show_string("B")
        radio.send_number(17)
    elif PlanetX_AILens.object_check(PlanetX_AILens.learnID.ID3) and RUN != 13:
        RUN = 13
        basic.show_string("C")
        radio.send_number(18)
    elif PlanetX_AILens.object_check(PlanetX_AILens.learnID.ID4) and RUN != 14:
        RUN = 14
        basic.show_string("D")
        radio.send_number(19)
    elif PlanetX_AILens.object_check(PlanetX_AILens.learnID.ID5) and RUN != 15:
        RUN = 15
        basic.show_string("E")
        radio.send_number(20)
RUN = 0
ID = 0
basic.show_icon(IconNames.HEART)
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)
ID = 0
basic.show_leds("""
    . # . . #
    # . # . #
    # # # . #
    # . # . #
    # . # . #
    """)
RUN = 0
radio.set_group(49)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P13, 0)
pins.digital_write_pin(DigitalPin.P14, 0)
pins.digital_write_pin(DigitalPin.P15, 0)
Old_Run = 0
count = 12
LED_status = 0

def on_every_interval():
    global LED_status, count, Old_Run
    if Old_Run == RUN:
        if count > 10:
            if LED_status == 0:
                LED_status = 1
                pins.digital_write_pin(DigitalPin.P1, 1)
                pins.digital_write_pin(DigitalPin.P2, 1)
                pins.digital_write_pin(DigitalPin.P13, 1)
                pins.digital_write_pin(DigitalPin.P14, 1)
                pins.digital_write_pin(DigitalPin.P15, 1)
            else:
                LED_status = 0
                pins.digital_write_pin(DigitalPin.P1, 0)
                pins.digital_write_pin(DigitalPin.P2, 0)
                pins.digital_write_pin(DigitalPin.P13, 0)
                pins.digital_write_pin(DigitalPin.P14, 0)
                pins.digital_write_pin(DigitalPin.P15, 0)
        else:
            count = count + 1
    else:
        Old_Run = RUN
        count = 0
loops.every_interval(500, on_every_interval)

def on_forever():
    PlanetX_AILens.camera_image()
    if ID == 0:
        checkCard()
    else:
        checkLOGO()
    if count < 10:
        doLight()
    basic.pause(200)
    basic.show_leds("""
        . # . . #
        # . # . #
        # # # . #
        # . # . #
        # . # . #
        """)
basic.forever(on_forever)

沒有留言:

張貼留言