2024年12月4日 星期三

[樹藝micro:bit] 唱唱鼠說故事

 

主要零件:樹藝材料、micro:bit、MP3播放器

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
def sayStory():
    basic.show_number(count)
    basic.pause(1000)
    mp3Player.set_tracking(count + 1, mp3Player.repeatList.NO)
    mp3Player.set_volume(volumn)
    mp3Player.execute(mp3Player.playType.PLAY)

def on_received_number(receivedNumber):
    global count
    if receivedNumber == 10:
        count = 0
        sayStory()
    elif receivedNumber == 6:
        count = 9
        sayStory()
    elif receivedNumber == 7:
        count = 8
        sayStory()
    elif receivedNumber == 8:
        count = 7
        sayStory()
    elif receivedNumber == 9:
        count = 4
        sayStory()
    elif receivedNumber >= 16 and receivedNumber <= 20:
        count = receivedNumber - 1
        sayStory()
    elif receivedNumber == 1:
        count = 1
        sayStory()
    elif receivedNumber == 2:
        count = 2
        sayStory()
    elif receivedNumber == 3:
        count = 3
        sayStory()
    elif receivedNumber == 4:
        count = 6
        sayStory()
    elif receivedNumber == 5:
        count = 5
        sayStory()
radio.on_received_number(on_received_number)

# 0.歡樂蛙
# 1.歡樂猪
# 2.智齡鳥
# 3.酷烗猪
# 4.行動牛
# 5.亮亮鼠
# 6.彩虹鳥
# 7.蓪草燈
# 8.先知鳥
# 9.時尚兔

def on_button_pressed_a():
    global count
    mp3Player.execute(mp3Player.playType.STOP)
    count = (count + 1) % 20
    sayStory()
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_b():
    global volumn
    volumn = (volumn + 1) % 30
    basic.show_number(volumn)
    mp3Player.set_volume(volumn)
input.on_button_pressed(Button.B, on_button_pressed_b)

def on_pin_pressed_p1():
    global count
    mp3Player.execute(mp3Player.playType.STOP)
    count = (count + 1) % 20
    sayStory()
input.on_pin_pressed(TouchPin.P1, on_pin_pressed_p1)

volumn = 0
count = 0
basic.show_icon(IconNames.HEART)
count = 0
volumn = 15
mp3Player.mp3_set_serial(SerialPin.P2)
mp3Player.execute(mp3Player.playType.STOP)
loud_sound = 1
radio.set_group(49)

沒有留言:

張貼留言