# Microbit
https://zh.wikipedia.org/wiki/Micro_Bit

可以去網路訂購,至少要有microbit板子和一條USB連接線,價格大概800上下
### Scratch
**<font color="green">開發環境:</font>** https://makecode.microbit.org/
裡面有許多已經完成的範本,選擇積木,點進去有步驟教學,也可以自行新增空白專案
由於網站教學已經滿完整了,可以請讀者不妨嘗試看看,很好上手的
**<font color="green">燒錄教學:</font>** https://youtu.be/fyyvM0ctzEg
**<font color="green">廣播模組:</font>** https://youtu.be/0MFas5CqLzE
### Python
**<font color="green">開發環境:</font>** https://python.microbit.org/v/2
**<font color="green">線上模擬:</font>** https://create.withcode.uk/
**<font color="green">基礎語法:</font>** https://hackmd.io/@Z1m8PSYMTwuAjFZXID1mJw/SyR9DqlNd
**<font color="green">Microbit文件:</font>** https://microbit-micropython.readthedocs.io/en/v2-docs/
程式架構
```python
from microbit import * # 將microbit套件裡的東西全部匯入進來
while True: # 無窮迴圈使microbit可以持續執行
# do something
...
```
範例程式
```python
from microbit import *
while True:
display.scroll('Hello, World!') # 由右往左亮,跑出Hello, World!
display.show(Image.HEART) # 顯示內建愛心形狀LED
sleep(2000) # 等待2秒
```
<font color="blue">常用函式</font>
* 文字顯示
```python
display.scroll(字串)
```
* 特定形狀顯示
```python
display.show(Image.形狀)
```
更多圖案見: https://microbit-micropython.readthedocs.io/en/v2-docs/tutorials/images.html
* 延遲
```python
sleep(時間) # 單位為毫秒(ms)
```
* 清空 LED
```python
display.clear()
```
##### <font color="red">Buttons</font>
microbit上有兩顆按鈕 A(左)與 B(右),在micropython裡分別以下面表示
| button_a | button_b |
| -------- | -------- |
它們底下有 3 個方法
| 方法 | 說明 |
| -------- | -------- |
| is_pressed() | 檢查當下是否被按住,有回傳 True,反之為 False |
| was_pressed() | 自啟動或上次呼叫此方法是否被按下(由上而下) |
| get_presses() | 回傳總按次數,次數會重置為零 |
<font color="purple">※ is_pressed() 若一直按著會一直判斷為 True,而 was_pressed() 只看按下的那一刻,會回傳 True</font>
```python
from microbit import *
while True:
if button_a.is_pressed() and button_b.is_pressed(): # A、B同時按要先判斷
# do something
...
sleep(300) # 需要給些延遲時間,因實際同時按還是會有微小的時間差,透過延遲可消除這問題
elif button_a.is_pressed(): # 當 A 被按下
# do something
...
elif button_b.is_pressed(): # 當 B 被按下
# do something
...
```
##### <font color="red">Gestures</font>
micropython 有提供關於加速度的模組,其中一項就是microbit板子的各種姿勢
| up | down | left | right | face up | face down |
| -------- | -------- | -------- | -------- | -------- | -------- |
| 直立正方向 | 直立倒著 | 往左 | 往右 | 朝上 | 朝下 |
| freefall | 3g | 6g | 8g | shake |
| -------- | -------- | -------- | -------- | -------- |
| 自由落體 | 3倍重力加速度 | 6倍重力加速度 | 8倍重力加速度 | 晃動 |
```python
from microbit import *
while True:
gesture = accelerometer.current_gesture()
if gesture == "face up":
display.show(Image.HAPPY)
else:
display.show(Image.ANGRY)
```
##### <font color="red">LED</font>
讓 LED 亮的方法
```python
display.set_pixel(x, y, brightness) # brightness 0~9 9為最亮
```

```python
from microbit import *
sleep_time = 300
led_value = 9
while True:
x0 = 0
y0 = 0
for xi in range(5):
x = x0 + xi
for yi in range(5):
y = y0 + yi
display.set_pixel(x%5, y%5, led_value)
sleep(sleep_time)
display.clear()
sleep(sleep_time)
```
上面可自行運行看看
***
##### <font color="red">LED 自定義函式</font>
讓亮燈圖形實現的更容易
```python
from microbit import *
def show_led(no, bright):
x = no % 5
y = no // 5
display.set_pixel(x, y, bright)
leds = [0, 1, 2, 3, 4, 9, 14, 19, 24, 23, 22, 21, 20, 15, 10, 5]
delay = 300
while True:
for led in leds:
show_led(led, 9)
sleep(delay)
display.clear()
sleep(delay)
```