2026年1月15日 星期四

[Q霸小車] 第二堂 :micro:bit遙控Q霸小車

 


Q霸小車程式:



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_received_string(receivedString):
    global mode
    if receivedString == "stop":
        mode = 0
    elif receivedString == "forward":
        mode = 1
    elif receivedString == "backward":
        mode = 2
    elif receivedString == "turnleft":
        mode = 3
    elif receivedString == "turnright":
        mode = 4
    elif receivedString == "turnon":
        mode = 5
    elif receivedString == "turnoff":
        mode = 6
radio.on_received_string(on_received_string)

def on_received_value(name, value):
    global mode, xValue, yValue
    mode = 7
    if name == "x":
        xValue = value
    if name == "y":
        yValue = value
radio.on_received_value(on_received_value)

yValue = 0
xValue = 0
mode = 0
ID = 1
radio.set_group(ID)
mode = 0
basic.show_number(ID)
basic.pause(5000)
basic.show_icon(IconNames.TORTOISE)
cuteBot.stopcar()

def on_forever():
    if mode == 0:
        cuteBot.stopcar()
    elif mode == 1:
        cuteBot.forward()
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            """)
    elif mode == 2:
        cuteBot.backforward()
        basic.show_leds("""
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
            """)
    elif mode == 3:
        cuteBot.turnleft()
        basic.show_leds("""
            . . # . .
            . # . . .
            # # # # #
            . # . . .
            . . # . .
            """)
    elif mode == 4:
        cuteBot.turnright()
        basic.show_leds("""
            . . # . .
            . . . # .
            # # # # #
            . . . # .
            . . # . .
            """)
    elif mode == 5:
        cuteBot.color_light(cuteBot.RGBLights.RGB_L, 0xff0000)
        cuteBot.color_light(cuteBot.RGBLights.RGB_R, 0xff0000)
        basic.show_icon(IconNames.HEART)
    elif mode == 6:
        cuteBot.closeheadlights()
        basic.clear_screen()
    else:
        cuteBot.motors(xValue + yValue, xValue - yValue)
        basic.show_icon(IconNames.TORTOISE)
basic.forever(on_forever)


遙控端:

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
def on_button_pressed_a():
    global mode, select
    if select == 1:
        mode = Temp
        basic.show_icon(IconNames.TORTOISE)
        select = 0
    else:
        if mode == 1:
            radio.send_string("turnleft")
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                """)
        elif mode == 3:
            radio.send_string("turnon")
            basic.show_leds("""
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                """)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_logo_pressed():
    global Temp, select
    basic.show_icon(IconNames.ANGRY)
    basic.show_number(mode)
    Temp = mode
    select = 1
input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)

def on_button_pressed_ab():
    if select == 0 and mode == 1:
        radio.send_string("forward")
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            """)
    basic.pause(500)
input.on_button_pressed(Button.AB, on_button_pressed_ab)

def on_button_pressed_b():
    global Temp
    if select == 1:
        Temp = (Temp + 1) % 4
        basic.show_number(Temp)
    else:
        if mode == 1:
            radio.send_string("turnright")
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                """)
        elif mode == 3:
            radio.send_string("turnoff")
            basic.clear_screen()
input.on_button_pressed(Button.B, on_button_pressed_b)

Temp = 0
select = 0
mode = 0
ID = 1
radio.set_group(ID)
basic.show_number(ID)
radio.send_string("stop")
basic.pause(5000)
basic.show_icon(IconNames.TORTOISE)
mode = 0
select = 0

def on_forever():
    if select == 0:
        if mode == 2:
            radio.send_value("x", input.acceleration(Dimension.X) / 10)
            basic.pause(100)
            radio.send_value("y", input.acceleration(Dimension.Y) / 10)
            basic.pause(100)
            basic.show_icon(IconNames.TORTOISE)
    else:
        radio.send_string("stop")
        basic.pause(500)
basic.forever(on_forever)

沒有留言:

張貼留言