配合 Mu Vision Sensor 做出人體辨識紅綠燈
日常生活中大部份的紅綠燈都是自動倒數計時運作的 , 在按下「行人過路掣」時才會加速換燈的速度 ,
這種方法在行人使用率較低或深夜時分的道路上會稍為延誤到司機們的時間 (需等候紅燈) ,
因此我們使用了 Mu Vision AI sensor 偵測行人路上有沒有行人 , 假如有行人時才開始進入綠燈的倒數 ;
沒有行人的時候則會保持著紅燈 , 讓車輛能暢通行駛。
所需物料:
- 12" M/M Jumper Wires x 4
- Mu Vision Connecter x 1
- 紅綠燈模型 x 1 [以 Mu Vision AI Sensor 取代 Switch]
電線接駁方法:
- 紅燈 : P15 , P16
- 綠燈 : P13 , P14
- Mu Vision Sensor 的 「V」 --> I/O board 的 3.3V OUT 「+」
- Mu Vision Sensor 的 「G」 --> I/O board 的 3.3V OUT 「-」
- Mu Vision Sensor 的 「SDA」 --> I/O board 的 「SDA」
- Mu Vision Sensor 的 「SCL」 --> I/O board 的 「SCL」
編程教學 :
設定 Mu Vision Sensor :

initialize MU00 port I2C --> 啟動 Mu Vision sensor 的編程 , 必須使用這個積木才能進行各種編程
「MU00」 enable alogirthm Body Detect --> 這次我們需檢測人類
將這兩組編程放至 「On Start」 , 最後補上「MU00 high FPS mode on」加強 sensor 的效能
開始時的設定[紅燈] :

「set index to 0」是設定倒數時間 , write Pin P15 to 1 , 16 to 0 是開啟紅燈(插線上的設定) ;
然後 show string "R" , 在 micro:bit LED 上顯示 R 表示現在是紅燈。
SET Variable :

到 Coding Blocks 中找出「Variables」然後創建一個全新的 variables --> 「Make a Variable...」

輸入 variable 的名稱便可完成創建。
檢測有沒有人 :

這次選用了 while true 以無間斷重複整個編程 , 「if MU00 detected Body Detect」 是偵測到人類的意思 ,
「for index from 0 to 5 」 --> show number 5 - index 是倒數的編程 , pause 1000ms(1秒)。
for loop :

這裡使用 for loop 來進行倒數 , 它的意思是每一次進行這個編程「index」的數值會 + 1 (由0開始) ,
因此「show number 5 - index ] 便可以做到倒數的功能 : 第一個 for --> 5 - 0 , 第二個 for --> 5 - 1 , i.e [5 , 4]
開啟綠燈 :

倒數完成後 , micro:bit LED show string "G" , 顯示 "G" 代表現在是綠燈時間 ,
因此 write PIN P15/16 to 0 (關紅燈) , P13 to 1 , P14 to 0 (開綠燈)
綠燈結束後回到紅燈 :

show string "R" 表示現在是紅燈時間 , P15 to 1 , P13/14/16 to 0 [關綠燈開紅燈]
整個編程 :
示範 :
可先預備「Body Object」跟「非人體」的圖示卡作為測試對象。
向 Mu Vision AI Sensor 展示圖示卡 , 由於不是「Body Object」因此會保持紅綠 , micro:bit LED 亦會保留"R"
向 Mu Vision Sensor 展示 fischer man 或真實的人體 , micro:bit 的 LED 會進入倒數最後轉成綠燈