2026年1月17日 星期六

[樹藝AI] micro:bit用MP3播放器來說故事

 


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
def on_button_pressed_a():
    global play
    play = mode + 1
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_received_string(receivedString):
    global play
    if receivedString == "Apple":
        play = 2
    elif receivedString == "Grape":
        play = 1
    elif receivedString == "Pear":
        play = 3
radio.on_received_string(on_received_string)

def on_button_pressed_b():
    global mode
    mode = (mode + 1) % 3
    basic.show_number(mode)
input.on_button_pressed(Button.B, on_button_pressed_b)

mode = 0
play = 0
radio.set_group(88)
mp3Player.mp3_set_serial(SerialPin.P1)
basic.show_icon(IconNames.HEART)
play = 0
mode = 0

def on_forever():
    global play
    if play == 2:
        basic.show_string("A")
        mp3Player.set_tracking(2, mp3Player.repeatList.NO)
        play = 0
    elif play == 1:
        basic.show_string("G")
        mp3Player.set_tracking(1, mp3Player.repeatList.NO)
        play = 0
    elif play == 3:
        basic.show_string("P")
        mp3Player.set_tracking(3, mp3Player.repeatList.NO)
        play = 0
basic.forever(on_forever)

沒有留言:

張貼留言