2025年7月24日 星期四

鋼鐡之心

 


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
def on_button_pressed_a():
    global Run, Mode
    Run = 0
    Mode = (Mode + 1) % 9
    basic.show_number(Mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_b():
    global Run, Temp
    Run = 1
    basic.show_icon(IconNames.ANGRY)
    Temp = (Temp + 1) % 2
    if Mode != 5:
        ContinuousServo.turn_off_motor(DigitalPin.P2)
    if Mode != 6:
        strip.clear()
        strip.show()
    if Mode == 6 or Mode == 7:
        strip.show_rainbow(1, 360)
        strip.show()
input.on_button_pressed(Button.B, on_button_pressed_b)

Temp = 0
strip: neopixel.Strip = None
Run = 0
Mode = 0
basic.show_icon(IconNames.ANGRY)
Mode = 0
Run = 0
ContinuousServo.spin_one_way(AnalogPin.P2)
ContinuousServo.spin_other_way(AnalogPin.P2)
ContinuousServo.turn_off_motor(DigitalPin.P2)
strip = neopixel.create(DigitalPin.P1, 16, NeoPixelMode.RGB)
strip.show_rainbow(1, 360)
strip.clear()
strip.show()
strip.show_rainbow(1, 360)
mp3Player.mp3_set_serial(SerialPin.P0)
mp3Player.set_volume(25)

def on_forever():
    global Temp, Mode
    if Run == 1:
        if Mode == 1:
            basic.show_icon(IconNames.HEART)
            basic.pause(500)
            basic.show_icon(IconNames.SMALL_HEART)
            basic.pause(500)
        elif Mode == 2:
            if input.light_level() >= 100:
                basic.show_icon(IconNames.HEART)
            else:
                basic.show_icon(IconNames.SMALL_HEART)
        elif Mode == 3:
            if input.compass_heading() >= 100:
                basic.show_icon(IconNames.HEART)
            else:
                basic.show_icon(IconNames.SMALL_HEART)
        elif Mode == 4:
            if input.sound_level() >= 150:
                if Temp == 0:
                    basic.show_icon(IconNames.HEART)
                else:
                    basic.show_icon(IconNames.SMALL_HEART)
                Temp = (Temp + 1) % 2
        elif Mode == 5:
            if Temp == 0:
                ContinuousServo.spin_one_way_with_speed(AnalogPin.P2, 50)
            else:
                ContinuousServo.spin_other_way_with_speed(AnalogPin.P2, 50)
        elif Mode == 6:
            strip.rotate(1)
            strip.show()
            basic.pause(200)
        elif Mode == 7:
            if Temp == 0:
                ContinuousServo.spin_one_way_with_speed(AnalogPin.P2, 50)
                strip.rotate(1)
            else:
                ContinuousServo.spin_other_way_with_speed(AnalogPin.P2, 50)
                strip.rotate(-1)
            strip.show()
            basic.pause(200)
        elif Mode == 8:
            mp3Player.set_tracking(1, mp3Player.repeatList.NO)
            mp3Player.execute(mp3Player.playType.PLAY)
            Mode = 0
basic.forever(on_forever)

沒有留言:

張貼留言