顯示具有 micro:bit 標籤的文章。 顯示所有文章
顯示具有 micro:bit 標籤的文章。 顯示所有文章

2026年7月24日 星期五

Q-Robot 的 micro:bit 入門任務

智慧生活科技教育|micro:bit 入門

從輸入到輸出:Q-Robot 的 micro:bit 入門任務

讓孩子先學會「看、想、做」,再進一步理解 micro:bit 如何接收訊號、 進行判斷,並透過燈光、聲音與馬達產生回應。

Q-Robot 的 micro:bit 入門任務課程封面
第二站「程式基地」:今天的口令是「輸入 → 判斷 → 輸出」。

對剛接觸程式設計的孩子而言,「程式」常常看起來很抽象。 因此,這堂課不先要求學生背指令,而是從一台模型風機出發: 風機的葉片為什麼會轉?哪些零件會感受到變化?micro:bit 又能做出什麼反應?

本課核心: 讓學生建立「輸入、處理、輸出」的基本概念,並透過事件、變數與條件判斷, 完成一個可以操作、可以觀察、也可以反覆修改的 micro:bit 小任務。

一、先認識 micro:bit 的正面與背面


micro:bit 正面與背面以及輸入處理輸出說明
micro:bit 具備按鈕、加速度、光線與溫度等輸入,也能以 LED、聲音與馬達控制作為輸出。

micro:bit 可以被想像成一個小型電腦。它會先「收到訊號」,再依照程式進行處理, 最後做出一個可觀察的反應。

輸入 Input 按鈕、方向、磁力、光線、溫度、腳位訊號等,都是 micro:bit 接收到的資訊。
處理 Process 程式依照設定好的規則進行計算、比較與判斷。
輸出 Output LED 圖案、聲音、伺服馬達或其他外接裝置,會呈現程式執行結果。

二、MakeCode:三步把想法放進板子

MakeCode 連接拼程式與下載三步驟
連接、拼程式、下載,是初學者最重要的三個操作步驟。
  1. 連接:使用 USB 線把 micro:bit 接到電腦。
  2. 拼程式:在 MakeCode 中把積木拖曳到正確位置。
  3. 下載:將產生的 .hex 檔案放進 MICROBIT 磁碟。

對兒童學習而言,最重要的不是一次就寫對,而是「下載、觀察、修改、再測試」。 這個反覆迭代的過程,正是運算思維與工程設計的核心。

三、事件:什麼時候開始做?

micro:bit 當開機時重複無限次與按鈕A事件
不同事件會決定程式在什麼時候被執行。

在 MakeCode 中,常見的事件可分成三種:

  • 當開機時:程式啟動後只執行一次,適合設定初始值。
  • 重複無限次:持續執行,適合反覆讀取感測器。
  • 當按鈕 A 被按下:等待使用者操作後才開始執行。
課堂提問: 若希望學生按下按鈕後才開始測量風機,應該使用哪一種事件?
答案:使用「當按鈕 A 被按下」事件。

四、輸入與輸出:micro:bit 的眼睛和手

micro:bit 輸入與輸出概念
輸入讓 micro:bit 感知環境,輸出讓它做出回應。

可以把輸入想像成 micro:bit 的「眼睛與耳朵」,把輸出想像成它的「手與嘴巴」。 例如,按鈕、方向、磁力與腳位訊號屬於輸入;LED 圖案、聲音與伺服馬達則屬於輸出。

想一想:風機轉速是哪一種?
當 micro:bit 透過感測器讀取風機轉速時,轉速資料屬於輸入; 若 micro:bit 控制馬達快慢,則「控制轉速」屬於輸出

五、變數:幫數字準備一個盒子

轉速角度與能量三種變數示例
變數名稱不變,但裡面的數值可以持續更新。

變數就像一個有名字的盒子,用來保存會改變的資料。 例如 rpm 可以保存轉速、angle 可以保存角度、 energy 可以保存能量值。

當感測器取得新資料時,只要把新數值放進相同的變數中, 程式就可以持續顯示、比較或記錄最新狀態。

程式碼:



六、條件判斷:如果……就……

如果能量大於80就顯示大燈號
條件判斷能讓 micro:bit 根據不同狀態做出不同回應。

條件判斷是讓程式「做決定」的重要工具。例如:

如果 energy > 80,就顯示大燈號;否則顯示小燈號。

程式:


這個概念可以延伸到風機課程:當轉速高於某個標準時亮起警示圖案, 當轉速較低時顯示另一個圖案,讓學生把抽象數值轉化為清楚的視覺訊號。

七、暖身任務:按 A 加一、按 B 歸零

按A加一按B歸零的micro:bit任務
透過簡單計數器,同時練習事件、變數與輸出。

這個任務可以分成三個步驟:

  1. 建立變數 count,並在開機時設為 0。
  2. 按下 A 鍵時,讓 count 加 1,並在 LED 顯示數值。
  3. 按下 B 鍵時,將 count 設回 0。

程式:




雖然這只是一個簡單的計數器,但學生已經實際運用了三個重要概念: 按鈕是輸入,程式進行處理, LED 顯示則是輸出

八、從計數器延伸到風機轉速

完成暖身任務後,可以把「按一次 A 鍵加一」改成「感測器偵測到葉片轉過一次就加一」。 程式在固定時間內統計轉動次數,再換算成每分鐘轉速 RPM, 就能逐步完成風機轉速量測。

哪個零件會直接量到轉速?
常見做法是使用磁鐵與霍爾感測器偵測葉片或輪轂每轉一圈所產生的脈衝, 再由 micro:bit 計算轉速。若只根據馬達產生的電壓估算,則屬於間接推估。

九、教學價值:先理解系統,再學習程式

這套入門任務不是單純教孩子拖拉積木,而是透過模型風機, 讓學生理解一個智慧系統如何運作:

感測器取得資料 → micro:bit 儲存與判斷 → LED、聲音或馬達做出回應

當孩子能用自己的話說明「輸入、處理、輸出」,也能完成事件、變數與條件判斷, 就已經具備進一步學習智慧控制、能源監測與 AIoT 應用的重要基礎。

AI 協作說明:
本文由作者主導內容規劃,並使用生成式 AI 協助文字整理、版面設計與 HTML 初稿; 所有內容均經作者審核與修訂。
智慧生活科技專業社群|micro:bit × Q-Robot × 風力發電 STEAM 教育

2026年1月22日 星期四

[Q霸小車] 第三堂AI鏡頭初體驗



範例一、辦識前進、後退、左轉、右轉、停止


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
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)

def on_forever():
    PlanetX_AILens.camera_image()
    if PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.FORWARD):
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.BACK):
        basic.show_leds("""
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.STOP):
        basic.show_leds("""
            . . . . .
            . # # # .
            . # # # .
            . # # # .
            . . . . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNLEFT):
        basic.show_leds("""
            . . # . .
            . # . . .
            # # # # #
            . # . . .
            . . # . .
            """)
    elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNRIGHT):
        basic.show_leds("""
            . . # . .
            . . . # .
            # # # # #
            . . . # .
            . . # . .
            """)
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
 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
188
189
190
191
192
193
194
195
def on_button_pressed_a():
    global mode
    mode = (mode + 1) % 5
    basic.show_number(mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_b():
    global mode
    if mode > 0:
        mode = mode - 1
    else:
        mode = 4
    basic.show_number(mode)
input.on_button_pressed(Button.B, on_button_pressed_b)

mode = 0
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)
mode = 0
basic.show_number(mode)
car = 0

def on_forever():
    global car
    PlanetX_AILens.camera_image()
    if mode == 0:
        if PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.FORWARD):
            basic.show_leds("""
                . . # . .
                . # # # .
                # . # . #
                . . # . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.BACK):
            basic.show_leds("""
                . . # . .
                . . # . .
                # . # . #
                . # # # .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.STOP):
            basic.show_leds("""
                . . . . .
                . # # # .
                . # # # .
                . # # # .
                . . . . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNLEFT):
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNRIGHT):
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                """)
    elif mode == 1:
        if PlanetX_AILens.other_card(PlanetX_AILens.otherCards.MOUSE):
            basic.show_string("Mouse")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.MICROBIT):
            basic.show_string("micro\"bit")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.RULER):
            basic.show_string("Ruler")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CAT):
            basic.show_string("Cat")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.PEAR):
            basic.show_string("Pear")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.SHIP):
            basic.show_string("Ship")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.APPLE):
            basic.show_string("Apple")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CAR):
            basic.show_string("Car")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.PEN):
            basic.show_string("Pen")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.DOG):
            basic.show_string("Dog")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.UMBRELLA):
            basic.show_string("Umbrella")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.AIRPLANE):
            basic.show_string("Airplane")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CLOCK):
            basic.show_string("clock")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.GRAPE):
            basic.show_string("Grape")
        elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.CUP):
            basic.show_string("Cup")
    elif mode == 2:
        if PlanetX_AILens.number_card(PlanetX_AILens.numberCards.ZERO):
            basic.show_number(0)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.ONE):
            basic.show_number(1)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.TWO):
            basic.show_number(2)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.THREE):
            basic.show_number(3)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.FOUR):
            basic.show_number(4)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.FIVE):
            basic.show_number(5)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.SIX):
            basic.show_number(6)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.SEVEN):
            basic.show_number(7)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.EIGHT):
            basic.show_number(8)
        elif PlanetX_AILens.number_card(PlanetX_AILens.numberCards.NINE):
            basic.show_number(9)
    elif mode == 3:
        if PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.A):
            basic.show_string("A")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.B):
            basic.show_string("B")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.C):
            basic.show_string("C")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.D):
            basic.show_string("D")
        elif PlanetX_AILens.letter_card(PlanetX_AILens.letterCards.E):
            basic.show_string("E")
    elif mode == 4:
        if PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.FORWARD):
            car = 1
            basic.show_leds("""
                . . # . .
                . # # # .
                # . # . #
                . . # . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.BACK):
            car = 2
            basic.show_leds("""
                . . # . .
                . . # . .
                # . # . #
                . # # # .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.STOP):
            car = 0
            basic.show_leds("""
                . . . . .
                . # # # .
                . # # # .
                . # # # .
                . . . . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNLEFT):
            car = 3
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                """)
        elif PlanetX_AILens.traffic_card(PlanetX_AILens.trafficCards.TURNRIGHT):
            car = 4
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                """)
    basic.pause(100)
basic.forever(on_forever)

def on_forever2():
    global car
    if car == 0:
        cuteBot.stopcar()
    elif car == 1:
        cuteBot.motors(30, 30)
    elif car == 2:
        cuteBot.motors(-30, -30)
    elif car == 3:
        cuteBot.motors(-30, 30)
        basic.pause(250)
        car = 0
    elif car == 4:
        cuteBot.motors(30, -30)
        basic.pause(250)
        car = 0
basic.forever(on_forever2)

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)

[樹藝AI]時尚兔開車用AI找農作物

 


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
m4Speed = 0
m1Speed = 0
lastError = 0
motorSpeed = 0
error = 0
BASE_SPEED = 60
Kp = 0.05
Kd = 0.06
CutebotPro.pwm_cruise_control(0, 0)
PlanetX_AILens.init_module()
PlanetX_AILens.switchfunc(PlanetX_AILens.FuncList.CARD)
radio.set_group(88)

def on_forever():
    PlanetX_AILens.camera_image()
    if PlanetX_AILens.other_card(PlanetX_AILens.otherCards.GRAPE):
        basic.show_string("G")
        radio.send_string("Grape")
    elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.APPLE):
        basic.show_string("A")
        radio.send_string("Apple")
    elif PlanetX_AILens.other_card(PlanetX_AILens.otherCards.PEAR):
        basic.show_string("P")
        radio.send_string("Pear")
    basic.pause(200)
basic.forever(on_forever)

def on_forever2():
    global error, motorSpeed, lastError, m1Speed, m4Speed
    error = CutebotPro.get_offset()
    motorSpeed = Kp * error + Kd * (error - lastError)
    lastError = error
    m1Speed = BASE_SPEED + motorSpeed
    m4Speed = BASE_SPEED - motorSpeed
    if m1Speed < 10:
        m1Speed = 0
    elif m1Speed < 30:
        m1Speed = 10
    elif m1Speed > 40:
        m1Speed = 40
    elif m1Speed > 60:
        m1Speed = 60
    if m4Speed < 10:
        m4Speed = 0
    elif m4Speed < 30:
        m4Speed = 10
    elif m4Speed > 40:
        m4Speed = 40
    elif m4Speed > 60:
        m4Speed = 60
    CutebotPro.pwm_cruise_control(m1Speed, m4Speed)
basic.forever(on_forever2)


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)

[Q霸小車] 第一堂 Q霸小車九種能力

 


nakecode:


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
def on_received_number(receivedNumber):
    global mode
    basic.show_number(receivedNumber)
    mode = receivedNumber
radio.on_received_number(on_received_number)

def on_button_pressed_a():
    global mode
    if select == 0:
        if mode == 1:
            cuteBot.forward()
        elif mode == 6:
            for index in range(5):
                right.show_color(neopixel.colors(NeoPixelColors.YELLOW))
                basic.pause(500)
                cuteBot.color_light(cuteBot.RGBLights.RGB_L, 0xffff00)
                basic.pause(500)
                right.show_color(neopixel.colors(NeoPixelColors.BLACK))
                cuteBot.closeheadlights()
                basic.pause(500)
    else:
        mode = (mode + 1) % 11
        basic.show_number(mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

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

def on_button_pressed_b():
    global select, init
    if select == 0:
        if mode == 1:
            cuteBot.backforward()
        elif mode == 6:
            for index2 in range(5):
                left.show_color(neopixel.colors(NeoPixelColors.YELLOW))
                basic.pause(500)
                cuteBot.color_light(cuteBot.RGBLights.RGB_R, 0xffff00)
                basic.pause(500)
                right.show_color(neopixel.colors(NeoPixelColors.BLACK))
                cuteBot.closeheadlights()
                basic.pause(500)
    else:
        select = 0
        init = 0
        basic.show_icon(IconNames.YES)
input.on_button_pressed(Button.B, on_button_pressed_b)

sonar = 0
strip: neopixel.Strip = None
right_speed = 0
left_speed = 0
init = 0
left: neopixel.Strip = None
right: neopixel.Strip = None
select = 0
mode = 0
mode = 0
select = 0
speed = 0
id2 = 1
radio.set_group(212)
basic.show_icon(IconNames.HEART)

def on_forever():
    global speed, left_speed, right_speed, strip, left, right, init, sonar
    if select == 0:
        if mode == 0:
            basic.show_number(id2)
        elif mode == 1:
            basic.show_icon(IconNames.HEART)
        elif mode == 2:
            basic.show_icon(IconNames.HEART)
            if speed > 100:
                speed = 0
            cuteBot.motors(speed, speed)
            speed += 1
        elif mode == 3:
            basic.show_icon(IconNames.HOUSE)
            cuteBot.forward()
            basic.pause(500)
            cuteBot.motors(100, 40)
            basic.pause(1000)
            cuteBot.forward()
            basic.pause(500)
            cuteBot.motors(40, 100)
            basic.pause(1000)
        elif mode == 4:
            basic.show_icon(IconNames.HEART)
            left_speed = randint(-100, 100)
            right_speed = randint(-100, 100)
            cuteBot.motors(left_speed, right_speed)
            basic.pause(1000)
        elif mode == 5:
            cuteBot.forward()
            if input.light_level() < 10:
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_L, 255, 255, 255)
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_R, 255, 255, 255)
            else:
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_R, 0, 0, 0)
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_L, 0, 0, 0)
        elif mode == 6:
            strip = neopixel.create(DigitalPin.P15, 2, NeoPixelMode.RGB)
            left = strip.range(0, 1)
            right = strip.range(1, 1)
        elif mode == 7:
            if not (cuteBot.tracking(cuteBot.TrackingState.L_R_LINE)):
                cuteBot.motors(20, 20)
            else:
                cuteBot.motors(-50, -50)
                basic.pause(300)
                cuteBot.motors(0, randint(50, 100))
                basic.pause(100)
        elif mode == 8:
            if cuteBot.tracking(cuteBot.TrackingState.L_UNLINE_R_LINE):
                cuteBot.motors(25, 10)
            elif cuteBot.tracking(cuteBot.TrackingState.L_LINE_R_UNLINE):
                cuteBot.motors(10, 25)
            elif cuteBot.tracking(cuteBot.TrackingState.L_R_LINE):
                cuteBot.motors(25, 25)
        elif mode == 9:
            if init == 0:
                init = 1
            else:
                sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
                cuteBot.motors(30, 30)
                if sonar > 2 and sonar < 10:
                    cuteBot.motors(0, -25)
                    basic.pause(1000)
                else:
                    cuteBot.motors(30, 30)
        else:
            if init == 0:
                init = 1
            else:
                sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
                cuteBot.motors(30, 30)
                if sonar > 5 and sonar < 10:
                    cuteBot.motors(0, 0)
                elif sonar < 5:
                    cuteBot.motors(-30, -30)
                else:
                    cuteBot.motors(30, 30)
basic.forever(on_forever)


2025年12月28日 星期日

[物聯網與智慧工藝] micro:bit連上MQTT Go

擴充板:【ELF014】EF IOT BIT 物聯網擴充板

一、建立新檔後,選擇"擴充"功能,再選擇"iot-environment-kit"。


二、積木程式


三、打開MQTT GO,物聯網MQTT網站,按下連線後,訂閱主題:NFU/Q-Robot/LED-mode。




2025年12月19日 星期五

Forward Education:以目標為導向的 STEAM 教育

網站: https://forwardedu.com/
以下文章是透過Notebooklm整理出來的:

此資源來自「Forward Education - Purpose-driven STEAM Education」網站,著重於推動以目標為導向的 STEAM 教育,目標是培養學生成為富有創造力的問題解決者。該網站提供全面的教育解決方案,包括程式設計與機器人套件(如氣候行動套件和智慧水培套件)、創客空間工具(例如 3D 列印機和雷射切割機),以及人工智慧的實作學習。內容亦涵蓋針對不同學習環境的課程,包括課堂、擴展學習和職業技術教育(CTE)實驗室,並提供教育者專業發展課程和豐富的教學資源,例如學習平台上的課程庫和教程。網站設計考量到全球受眾,提供多國貨幣和地區選項,顯示其產品和服務具有廣泛的國際影響力。


氣候行動教學連結:

2025年12月11日 星期四

無裝置程式教育:micro:bit第一堂-剪刀、石頭、布遊戲設計

本課程在虎尾鎮安慶國小試教。網址:https://www.facebook.com/share/v/17mBrum3fb/


 
學生端程



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
def on_received_number(receivedNumber):
    global select, mode
    select = 0
    mode = receivedNumber
radio.on_received_number(on_received_number)

def on_button_pressed_a():
    global mode
    if select == 0:
        if mode == 3:
            basic.show_icon(IconNames.HAPPY)
    else:
        mode = (mode + 1) % 7
        basic.show_number(mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_gesture_shake():
    global step, Number2
    if select == 0:
        if mode == 4:
            step += 1
            basic.show_number(step)
        elif mode == 6:
            Number2 = randint(0, 2)
            if Number2 == 0:
                basic.show_icon(IconNames.SMALL_SQUARE)
            elif Number2 == 1:
                basic.show_icon(IconNames.SQUARE)
            else:
                basic.show_icon(IconNames.SCISSORS)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)

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

def on_received_string(receivedString):
    global name
    name = receivedString
radio.on_received_string(on_received_string)

def on_button_pressed_b():
    global select
    if select == 0:
        if mode == 3:
            basic.show_icon(IconNames.SAD)
    else:
        select = 0
        basic.show_icon(IconNames.YES)
input.on_button_pressed(Button.B, on_button_pressed_b)

Number2 = 0
select = 0
step = 0
name = ""
mode = 0
id2 = 1
mode = 0
name = "Q-Robot AI"
step = 0
select = 0
basic.show_number(id2)
basic.pause(2000)
radio.set_group(212)

def on_forever():
    if select == 0:
        if mode == 0:
            basic.show_number(id2)
        elif mode == 1:
            basic.show_string(name)
        elif mode == 2:
            basic.show_icon(IconNames.HEART)
            basic.pause(500)
            basic.show_icon(IconNames.SMALL_HEART)
            basic.pause(500)
        elif mode == 5:
            if input.light_level() <= 10:
                basic.show_leds("""
                    # # # # #
                    # # # # #
                    # # # # #
                    # # # # #
                    # # # # #
                    """)
            else:
                basic.clear_screen()
basic.forever(on_forever)