# WebBit物聯網程式 ### 30204林奇炫 ###### tags: ˋ110北中筆記ˋ ## 一.Blockly程式練習 - [x] 1. 抽籤並朗讀姓名 - [x] 2. 大樂透自動選號 - [x] 3. 隨機組合朗讀語句 - [x] 4. 小時鐘 - [x] 5. 語音報時 ### 1. 抽籤並朗讀姓名 [1. 抽籤並朗讀姓名](https://blocklypro.webduino.io/#k8pmenbLb6) ![1. 抽籤並朗讀姓名](https://i.imgur.com/hixvf2O.png) ```javascript= var name2; var result; function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } name2 = ['方廷原', '林家佑', '林奇炫', '莊弘昇', '許臣溪']; result = (name2[((math_random_int(1, 5)) - 1)]); window.alert(result); speak((['恭喜',result,'獲獎'].join(''))); ``` ### 2. 大樂透自動選號 [2. 大樂透自動選號](https://blocklypro.webduino.io/#k4oaMn16gP) ![2. 大樂透自動選號](https://i.imgur.com/PlyTwyn.png) ```javascript= var arr; var i; var resultt; var j; var position; var num; function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } arr = []; for (i = 1; i <= 49; i++) { arr[i - 1] = i; } resultt = []; for (j = 1; j <= 6; j++) { position = math_random_int(1, (arr.length)); num = (arr.splice((position - 1), 1)[0]); resultt[j - 1] = num; } window.alert(resultt); speak((['本期大樂透中獎號碼為:',resultt,',祝您中大獎'].join(''))); ``` ### 3. 隨機組合朗讀語句 [3. 隨機組合朗讀語句](https://blocklypro.webduino.io/#XBrBqzD1dY) ![3. 隨機組合朗讀語句](https://i.imgur.com/u5zOlgO.png) ```javascript= var arr1; var arr2; var arr3; var re; function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } arr1 = ['1', '2', '3']; arr2 = ['4', '5', '6']; arr3 = ['7', '8', '9']; re = [(arr1[((math_random_int(1, 3)) - 1)]),(arr2[((math_random_int(1, 3)) - 1)]),(arr3[((math_random_int(1, 3)) - 1)])].join(''); window.alert(re); speak(re); ``` ### 4. 小時鐘 [4. 小時鐘](https://blocklypro.webduino.io/#7g11vGgrAn) ![4. 小時鐘](https://i.imgur.com/6sMj7UN.png) ```javascript= (async function () { var h; var m; var s; var time; function get_time(t) { var varTime = new Date(), varHours = varTime.getHours(), varMinutes = varTime.getMinutes(), varSeconds = varTime.getSeconds(); var varNow; if (t == "hms") { varNow = varHours + ":" + varMinutes + ":" + varSeconds; } else if (t == "h") { varNow = varHours; } else if (t == "m") { varNow = varMinutes; } else if (t == "s") { varNow = varSeconds; } return varNow; } async function show() { h = get_time("h"); m = get_time("m"); s = get_time("s"); if (h < 10) { h = String(10) + String(h); } if (m < 10) { m = String(0) + String(m); } if (s < 10) { s = String(0) + String(s); } time = [h,':',m,':',s].join(''); document.getElementById('demo-area-01-show').innerHTML = time; await delay(1); show(); } show(); }()); ``` ### 5. 語音報時 [5. 語音報時](https://blocklypro.webduino.io/#71jjpmzbv2) ![5. 語音報時](https://i.imgur.com/QxZpdRt.png) ```javascript= (async function () { var h; var m; var text; var s; var time; function getElement(dom) { var element = document.querySelector(dom); return element; } function controllerBtnEvent(c, e, callback) { if (e !== 'click') { var _u = navigator.userAgent; if (_u.indexOf('Android') > -1 || _u.indexOf('iPhone') > -1 || _u.indexOf('iPad') > -1) { c.addEventListener(e[1], async function () { callback(); }); } else { c.addEventListener(e[0], async function () { callback(); }); } } else { c.addEventListener('click', async function () { callback(); }); } } function get_time(t) { var varTime = new Date(), varHours = varTime.getHours(), varMinutes = varTime.getMinutes(), varSeconds = varTime.getSeconds(); var varNow; if (t == "hms") { varNow = varHours + ":" + varMinutes + ":" + varSeconds; } else if (t == "h") { varNow = varHours; } else if (t == "m") { varNow = varMinutes; } else if (t == "s") { varNow = varSeconds; } return varNow; } async function show() { h = get_time("h"); m = get_time("m"); s = get_time("s"); if (m < 10) { m = String(0) + String(m); } if (s < 10) { s = String(0) + String(s); } time = [h,':',m,':',s].join(''); document.querySelector("#demo-area-09 .btn-show").innerHTML = time; await delay(1); show(); } show(); controllerBtnEvent(getElement('#demo-area-09 .btn-num1'),'click', async function () { speak((['現在時間是',h,'點',m,'分',s,'秒'].join(''))); }); controllerBtnEvent(getElement('#demo-area-09 .btn-num2'),'click', async function () { if (h > 0 && h < 5) { text = '凌晨'; } else if (h == 0) { text = '半夜'; } else if (h >= 5 && h < 12) { text = '上午'; } else if (h == 12) { text = '中午'; } else if (h > 12 && h < 18) { text = '下午'; h = h - 12; } else if (h >= 18 && h <= 23) { text = '晚上'; h = h - 12; } }); }()); ``` ## 二.WebBit開發版基本操作 - [x] 1. 跳動的心 - [x] 2. 跑馬燈(倒數321Go!) - [x] 03流動的沙子(九軸感應器) - [x] 04隨機顯示數字 - [x] 05溫度計 - [x] 06搖骰子 - [x] 07指北針 - [x] 08計步器 - [x] 09播放音樂(&音效語音) ### 01. 跳動的心 [1. 跳動的心](https://webbit.webduino.io/blockly/?demo=default) ![](https://i.imgur.com/eKv8CgR.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); let $atflm391 = _startLoop_(); while (_loop_[$atflm391]) { _board_._bit_matrix_.setColor("00000000010000000200000003000000040000000500000006ff00000700000008ff0000090000000a0000000bff00000cff00000dff00000e0000000f0000001000000011ff000012000000130000001400000015000000160000001700000018000000"); await delay(0.5, $atflm391); _board_._bit_matrix_.setColor("0000000001ff00000200000003ff00000400000005ff000006ff000007ff000008ff000009ff00000aff00000bff00000cff00000dff00000eff00000f00000010ff000011ff000012ff000013000000140000001500000016ff00001700000018000000"); await delay(0.5, $atflm391); await delay(0.005, true); } }); }()); ``` ### 02看誰按得快(按鈕開關) [02看誰按得快(按鈕開關)](https://webbit.webduino.io/blockly/#YqKEEe9Ya2Ly4) ![](https://i.imgur.com/P9YKcFm.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0010000010111110001000100', '#ffffff')); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0010001000111110100000100', '#99ffff')); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('pressed', async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0000000100011101111100000', '#ffff66')); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 03流動的沙子(九軸感應器) [03流動的沙子(九軸感應器)](https://webbit.webduino.io/blockly/#wRBvvPPJa0lRM) ![](https://i.imgur.com/4dm4MQZ.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "gyr": true, "ang": true }); mpu9250Fn_.peace(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0000001010001000101000000', '#00cccc')); }); mpu9250Fn_.rowBack(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000000101000000', '#ffcc99')); }); mpu9250Fn_.pitchRight(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000101000000', '#cc9944')); }); mpu9250Fn_.rowForward(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000010000000', '#ccccff')); }); mpu9250Fn_.pitchLeft(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0000000000001000000000000', '#6644ff')); }); }); }()); ``` ### 04隨機顯示數字 [04隨機顯示數字](https://webbit.webduino.io/blockly/#Mq011vv0rJE30) ![](https://i.imgur.com/6BhWfol.jpg) ```javascript= (async function() { function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } boardReady({ board: 'Bit', url: '127.0.0.1:8080', multi: true }, async function(board) { window._board_ = await boardInit_(board, 100, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { _board_._bit_matrix_.setCharacter(((math_random_int(0, 9))).toString(), '#99ffff'); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 05溫度計 [05溫度計](https://webbit.webduino.io/blockly/#V3EmmPPoMvo) ![](https://i.imgur.com/KAnKYY0.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "temp": true }); $demoMonster01.talk((_board_._bit_detected_val_.temp)); _board_._bit_matrix_.setString(((_board_._bit_detected_val_.temp)).toString(), '#ffcc00', 2); }); }()); ``` ### 06搖骰子 [06搖骰子](https://webbit.webduino.io/blockly/#r3444KKYVvo3d) ![](https://i.imgur.com/y2EbkUe.jpg) ```javascript= (async function() { var _E8_AE_8A_E6_95_B8; function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "gyr": true }); mpu9250Fn_.shake(_board_, async function() { _E8_AE_8A_E6_95_B8 = math_random_int(1, 6); if (_E8_AE_8A_E6_95_B8 == 1) { _board_._bit_matrix_.setColor(matrixEmoji_('0000000000001000000000000', '#99ff99')); } if (_E8_AE_8A_E6_95_B8 == 2) { _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000010000000', '#99ff99')); } if (_E8_AE_8A_E6_95_B8 == 3) { _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000101000000', '#99ff99')); } if (_E8_AE_8A_E6_95_B8 == 4) { _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000000101000000', '#99ff99')); } if (_E8_AE_8A_E6_95_B8 == 5) { _board_._bit_matrix_.setColor(matrixEmoji_('0000001010001000101000000', '#99ff99')); } if (_E8_AE_8A_E6_95_B8 == 6) { _board_._bit_matrix_.setColor(matrixEmoji_('0101000000010100000001010', '#99ff99')); } }); }); }()); ``` ### 07指北針 [07指北針](https://webbit.webduino.io/blockly/#GqNzzbb9mejqg) ![](https://i.imgur.com/fHGOzoe.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "azi": true }); mpu9250Fn_.faceNorth(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0010001110101010010000100', '#66ff99')); }); mpu9250Fn_.faceSouth(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0010000100101010111000100', '#66ff99')); }); mpu9250Fn_.faceEast(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0010000010111110001000100', '#66ff99')); }); mpu9250Fn_.faceWest(_board_, async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0010001000111110100000100', '#66ff99')); }); let $wjqmj354 = _startLoop_(); while (_loop_[$wjqmj354]) { $demoMonster01.talk(_board_._bit_mpu9250_val_.azi); await delay(0.005, true); } }); }()); ``` ### 08計步器 [08計步器](https://webbit.webduino.io/blockly/#lRQYY9Lrj0ORG) ![](https://i.imgur.com/blSNeQn.jpg) ```javascript= (async function() { var _E8_A8_88_E6_95_B8; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "gyr": true }); _E8_A8_88_E6_95_B8 = 0; $demoMonster01.talk(_E8_A8_88_E6_95_B8); _board_._bit_matrix_.setCharacter(_E8_A8_88_E6_95_B8, '#ff0000'); mpu9250Fn_.shake(_board_, async function() { _E8_A8_88_E6_95_B8 = _E8_A8_88_E6_95_B8 + 1; $demoMonster01.talk(_E8_A8_88_E6_95_B8); _board_._bit_matrix_.setCharacter((_E8_A8_88_E6_95_B8 % 10), '#ff0000'); }); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { $demoMonster01.talk(_E8_A8_88_E6_95_B8); await _board_._bit_matrix_.setStringOnce(_E8_A8_88_E6_95_B8, '#ff0000', 2); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('pressed', async function() { _E8_A8_88_E6_95_B8 = 0; $demoMonster01.talk(_E8_A8_88_E6_95_B8); _board_._bit_matrix_.setCharacter(_E8_A8_88_E6_95_B8, '#ff0000'); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 09播放音樂(&音效語音) [09播放音樂(&音效語音)](https://webbit.webduino.io/blockly/#7RprrVGJwJb3r) ![](https://i.imgur.com/UjNyVw3.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { buzzerPlayEvent_(_board_, 'stop'); await delay(0.1, true); await buzzerPlay_(_board_, [('E5')], [(2)]); await buzzerPlay_(_board_, [('C5')], [(2)]); await buzzerPlay_(_board_, [('G4')], [(2)]); await buzzerPlay_(_board_, [('C5')], [(2)]); await buzzerPlay_(_board_, [('D5')], [(2)]); await buzzerPlay_(_board_, [('G5')], [(1)]); await buzzerPlay_(_board_, [('D5')], [(2)]); await buzzerPlay_(_board_, [('E5')], [(2)]); await buzzerPlay_(_board_, [('D5')], [(2)]); await buzzerPlay_(_board_, [('G4')], [(2)]); await buzzerPlay_(_board_, [('C5')], [(2)]); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { buzzerPlayEvent_(_board_, 'stop'); await delay(0.1, true); await buzzerPlay_(_board_, ["E5", "E5", "E5", "0", "E5", "E5", "E5", "0", "E5", "G5", "C5", "D5", "E5", "0", "F5", "F5", "F5", "F5", "F5", "E5", "E5", "0", "E5", "D5", "D5", "E5", "D5", "G5", "0", "G4", "E5", "D5", "C5", "G4", "0", "G4", "E5", "D5", "C5", "A4", "0", "A4", "F5", "E5", "D5", "B4", "0", "G5", "G5", "F5", "D5", "E5", "C5", "0", "G4", "E5", "D5", "C5", "G4", "0", "G4", "E5", "D5", "C5", "A4", "0", "A4", "F5", "E5", "D5", "G5", "G5", "G5", "G5", "A5", "G5", "F5", "D5", "C5"], ["8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "6", "8", "8", "6", "8", "8", "8", "8", "8", "8", "8", "6", "8", "8", "8", "8", "4", "8", "6", "8", "8", "8", "8", "5", "5", "8", "8", "8", "8", "5", "5", "8", "8", "8", "8", "5", "5", "8", "8", "8", "8", "5", "5", "5", "8", "8", "8", "8", "5", "5", "8", "8", "8", "8", "5", "5", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8"]); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### Line1 回傳表情符號 [ Line1 回傳表情符號](https://webbit.webduino.io/blockly/#Qq70Bdd027x3B) ![](https://i.imgur.com/IhpYe6K.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); let _chatChannel = line_channel('81904522'); _chatChannel.on("value", async function(_e) { let _msg = ""; if (_e.val()) { _msg = _e.val().msg; } if (_msg) { if (_msg == '1') { await line_reply(_e.val().uid, { message: " ", stickerPackageId: 1, stickerId: 1, type: "sticker" }, _e.val().rt); } if (_msg == '2') { await line_reply(_e.val().uid, { message: " ", stickerPackageId: 1, stickerId: 2, type: "sticker" }, _e.val().rt); } if (_msg == '3') { await line_reply(_e.val().uid, { message: " ", stickerPackageId: 1, stickerId: 3, type: "sticker" }, _e.val().rt); } if (_msg == '4') { await line_reply(_e.val().uid, { message: " ", stickerPackageId: 1, stickerId: 4, type: "sticker" }, _e.val().rt); } if (_msg == '5') { await line_reply(_e.val().uid, { message: " ", stickerPackageId: 1, stickerId: 5, type: "sticker" }, _e.val().rt); } } }); }); }()); ``` ### Line2 回傳氣象資訊 [Line2 回傳氣象資訊](https://webbit.webduino.io/blockly/#nqnoMXXXe473Z) ![](https://i.imgur.com/y9iB1K8.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); let _chatChannel = line_channel('81904522'); _chatChannel.on("value", async function(_e) { let _msg = ""; if (_e.val()) { _msg = _e.val().msg; } if (_msg) { await getWeather('aqi'); if (_msg == 'AQI') { await line_reply(_e.val().uid, getWeatherAqi('臺南', 'all'), _e.val().rt); } await getWeather('observe'); if (_msg == 'NOW') { await line_reply(_e.val().uid, getWeatherObserve('臺南', '0'), _e.val().rt); } await getWeather('forecast'); if (_msg == 'forecast') { await line_reply(_e.val().uid, getWeatherForecast('臺南市', '0'), _e.val().rt); } await getWeather('radar'); if (_msg == 'radar') { await line_reply(_e.val().uid, getWeatherRadar(), _e.val().rt); } await getWeather('reservoir'); if (_msg == '水庫') { await line_reply(_e.val().uid, getWeatherReservoir('石門水庫', 'all'), _e.val().rt); } await getWeather('quake'); if (_msg == '地震') { await line_reply(_e.val().uid, getWeatherQuake('0'), _e.val().rt); } } }); }); }()); ``` ## 二-2.作業練習 - [x] 1.怪獸亂跑亂轉 - [x] 2.鍵盤控制怪獸移動 - [x] 3.顯示倒數5秒 - [x] 4.語音客服 - [x] 5.抽籤選學生 - [x] 6.空氣品質即時資訊 - [x] 7.偵測亮度及溫度 - [x] 8.計步器 ### 1.怪獸亂跑亂轉 [1.怪獸亂跑亂轉](https://webbit.webduino.io/blockly/#a35VVQaJooXym) ![](https://i.imgur.com/5sUWwbb.jpg) ```javascript= (async function() { let $mwuwf457 = _startLoop_(); while (_loop_[$mwuwf457]) { $demoMonster01.move('random', 30); $demoMonster02.move('random', 30); $demoMonster03.move('random', 30); $demoMonster04.move('random', 30); $demoMonster01.rotate('left', 30); $demoMonster02.rotate('left', 30); $demoMonster03.rotate('left', 30); $demoMonster04.rotate('left', 30); $demoMonster01.rebound(); await delay(0.1, $mwuwf457); await delay(0.005, true); } }()); ``` ### 2.鍵盤控制怪獸移動 [2.鍵盤控制怪獸移動](https://webbit.webduino.io/blockly/#JyeOOzPx9GdqV) ![](https://i.imgur.com/9ZnsEyb.png) ```javascript= (async function() { keyboardEvent(); keyDownCode['code38'] = async function() { $demoMonster01.move('top', 500); }; keyDownCode['code40'] = async function() { $demoMonster01.move('bottom', 500); }; keyDownCode['code37'] = async function() { $demoMonster01.move('left', 500); }; keyDownCode['code39'] = async function() { $demoMonster01.move('right', 500); }; let $ehdis290 = _startLoop_(); while (_loop_[$ehdis290]) { $demoMonster01.rebound(); await delay(0.005, true); } }()); ``` ### 3.顯示倒數5秒 [ 3.顯示倒數5秒](https://webbit.webduino.io/blockly/#EqdnnY7enzvy6) ![](https://i.imgur.com/JpIzOVR.png) ```javascript= (async function() { var i; boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); $demoMonster01.talk('請按模擬器按鈕A'); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { let $nsxty323 = _startLoop_(); for (var i = 5; i >= 0; i -= 1) { if (!_loop_[$nsxty323]) { break; } _board_._bit_matrix_.setCharacter(i, '#ffffff'); await delay(1, $nsxty323); await delay(0.001, true); } let $pvqek602 = _startLoop_(); for (let count = 0; count < 5; count++) { if (!_loop_[$pvqek602]) { break; } _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#ffff00')); await delay(0.1, $pvqek602); _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#44ff44')); await delay(0.1, $pvqek602); _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#ff0000')); await delay(0.1, $pvqek602); _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#4444ff')); await delay(0.1, $pvqek602); _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#ff99ff')); await delay(0.1, $pvqek602); _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#cc44cc')); await delay(0.1, $pvqek602); _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#ff6600')); await delay(0.1, $pvqek602); await delay(0.001, true); } }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 4.語音客服 [4.語音客服](https://webbit.webduino.io/blockly/#8qMzzNWnBWd3W) ![](https://i.imgur.com/b9NGaVK.png) ```javascript= (async function() { let $nrstl46 = _startLoop_(); while (_loop_[$nrstl46]) { $demoMonster01.talk('幾歲拉'); await speakAsync('幾歲拉', ["zh-TW", 1, 1]); await textInput(); if ((thisInputVal) <= 16) { $demoMonster01.talk('安安你好呀'); await speakAsync('安安你好呀', ["zh-TW", 1, 1]); } else { $demoMonster01.talk('滾拉'); await speakAsync('滾拉', ["zh-TW", 1, 1]); } await delay(0.005, true); } }()); ``` ### 5.抽籤選學生 [ 5.抽籤選學生](https://webbit.webduino.io/blockly/#ayxAAwx1JWQRM) ![](https://i.imgur.com/xV5iEci.png) ```javascript= var _E5_90_8D_E5_96_AE; function lists_random_item(list, remove) { let x = Math.floor(Math.random() * list.length); if (remove) { return list.splice(x, 1)[0]; } else { return list[x]; } } _E5_90_8D_E5_96_AE = ['Iphone', '莊鴻昇', '陳群祥', '許晨曦', '腳踏車', '大卡車']; $demoMonster01.click(function() { $demoMonster01.talk((lists_random_item(_E5_90_8D_E5_96_AE, false))); }); ``` ### 6.空氣品質即時資訊 [6.空氣品質即時資訊](https://webbit.webduino.io/blockly/#MqAllNP686LRY) ![](https://i.imgur.com/c7Lu2fg.png) ```javascript= (async function() { var AQI; boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); let $yrids613 = _startLoop_(); while (_loop_[$yrids613]) { await getWeather('aqi'); AQI = getWeatherAqi('臺南', 'AQI'); $demoMonster01.talk(AQI); if (AQI <= 50) { _board_._bit_matrix_.setColor('#44ff44'); $demoMonster01.talk('良好'); } else if (AQI <= 100) { _board_._bit_matrix_.setColor('#ffff00'); $demoMonster01.talk('普通'); } else if (AQI <= 150) { _board_._bit_matrix_.setColor('#ffcc44'); $demoMonster01.talk('對敏感族群不健康'); } else { _board_._bit_matrix_.setColor('#ff0000'); $demoMonster01.talk('對所有族群不健康'); } await delay(0.005, true); } }); }()); ``` ### 7.偵測亮度及溫度 [7.偵測亮度及溫度](https://webbit.webduino.io/blockly/#xqVMMYDwP4vR7) ![](https://i.imgur.com/TmMvLEw.png) ```javascript= (async function() { var _E4_BA_AE_E5_BA_A6; function math_mean(myList) { return myList.reduce(function(x, y) { return x + y; }) / myList.length; } boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); await detectInit_(_board_, { "temp": true, "left": true, "right": true }); let $wammx980 = _startLoop_(); while (_loop_[$wammx980]) { _E4_BA_AE_E5_BA_A6 = math_mean([(_board_._bit_detected_val_.right), (_board_._bit_detected_val_.left)]); $demoMonster01.talk((String('現在溫度') + String(_board_._bit_detected_val_.temp))); $demoMonster02.talk((['平均亮度', _E4_BA_AE_E5_BA_A6, '流明'].join(''))); if (_E4_BA_AE_E5_BA_A6 < 200) { _board_._bit_matrix_.setColor('#ff0000'); } else { _board_._bit_matrix_.off(); } await delay(0.005, true); } }); }()); ``` ### 8.計步器 [8.計步器](https://webbit.webduino.io/blockly/#XROzzXBox4dyn) ![](https://i.imgur.com/AHFvGkl.png) ```javascript= (async function() { var _E8_A8_88_E6_95_B8; var _E8_AE_8A_E6_95_B8; boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); await detectInit_(_board_, { "gyr": true }); _E8_A8_88_E6_95_B8 = 0; $demoMonster01.talk(_E8_A8_88_E6_95_B8); _board_._bit_matrix_.setCharacter(_E8_A8_88_E6_95_B8, '#ff0000'); mpu9250Fn_.shake(_board_, async function() { _E8_AE_8A_E6_95_B8 = _E8_AE_8A_E6_95_B8 + 1; $demoMonster01.talk(_E8_A8_88_E6_95_B8); _board_._bit_matrix_.setCharacter((_E8_A8_88_E6_95_B8 % 10), '#ff0000'); }); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { $demoMonster01.talk(_E8_A8_88_E6_95_B8); await _board_._bit_matrix_.setStringOnce(_E8_A8_88_E6_95_B8, '#ff0000', 2); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('pressed', async function() { _E8_A8_88_E6_95_B8 = 0; $demoMonster01.talk(_E8_A8_88_E6_95_B8); _board_._bit_matrix_.setCharacter(_E8_A8_88_E6_95_B8, '#ff0000'); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ## 三. Web:bit實作練習題 - [x] 1.線上控制測試Bit - [x] 2.跑馬燈(倒數321Go!) - [x] 3.燈號報數 - [x] 4.剪刀石頭布 - [x] 5.環能控光燈 - [x] 6.電子鬧鐘 - [x] 7.空氣品質AQI ### 1.線上控制測試Bit ![](https://i.imgur.com/jE6FBX2.png) ### 2.跑馬燈(倒數321Go!) [2.跑馬燈(倒數321Go!)](https://webbit.webduino.io/blockly/#6RLrrGNQVW23V) ![](https://i.imgur.com/C3Sv8iy.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); _board_._bit_matrix_.setCharacter('3', '#4466ff'); await delay(1, true); //delay _board_._bit_matrix_.setCharacter('2', '#4466ff'); await delay(1, true); //delay _board_._bit_matrix_.setCharacter('1', '#4466ff'); await delay(1, true); //delay await _board_._bit_matrix_.setStringOnce('go!', '#ff0000', 2); }); }()); ``` ### 3.燈號報數 [3.燈號報數](https://webbit.webduino.io/blockly/#EqdnnEZQGQPy6) ![](https://i.imgur.com/wrUqTj5.png) ```javascript= (async function() { var i; function colour_random() { var num = Math.floor(Math.random() * Math.pow(2, 24)); return '#' + ('00000' + num.toString(16)).substr(-6); } boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); let $njakf798 = _startLoop_(); for (var i = 1; i <= 25; i += 1) { if (!_loop_[$njakf798]) { break; } _board_._bit_matrix_.setColor((i - 1), (colour_random())); await delay(0.25, $njakf798); await delay(0.001, true); } }); }()); ``` ### 4.剪刀石頭布 [4.剪刀石頭布](https://webbit.webduino.io/blockly/#xqVMMEb5moMR7) ![](https://i.imgur.com/9s7CaoZ.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { _board_._bit_matrix_.setColor(matrixEmoji_('1000101010001001101111011', '#ff0000')); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnA_, 'released', async function() { _board_._bit_matrix_.off(); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { _board_._bit_matrix_.setColor(matrixEmoji_('0111011111111110111001110', '#ff0000')); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'released', async function() { _board_._bit_matrix_.off(); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('pressed', async function() { _board_._bit_matrix_.setColor(matrixEmoji_('1010101110111110111010101', '#ff0000')); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('released', async function() { _board_._bit_matrix_.off(); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 5.環能控光燈 [5.環能控光燈](https://webbit.webduino.io/blockly/#8RPzzGVaaN93b) ![](https://i.imgur.com/exXisdF.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); await detectInit_(_board_, { "left": true }); let $fmnbs287 = _startLoop_(); while (_loop_[$fmnbs287]) { $demoMonster01.talk((_board_._bit_detected_val_.left)); if ((_board_._bit_detected_val_.left) < 100) { _board_._bit_matrix_.setColor('#ffffff'); _board_._bit_matrix_.brightness(15); } else if ((_board_._bit_detected_val_.left) < 150) { _board_._bit_matrix_.setColor('#ffffff'); _board_._bit_matrix_.brightness(5); } else { _board_._bit_matrix_.off(); } await delay(0.005, true); } }); }()); ``` ### 6.電子鬧鐘 [6.電子鬧鐘](https://webbit.webduino.io/blockly/#7qZrr2OK5QbqO) ![](https://i.imgur.com/PK1jKQC.png) ```javascript= (async function() { function get_time(t) { var varTime = new Date(), varHours = varTime.getHours(), varMinutes = varTime.getMinutes(), varSeconds = varTime.getSeconds(); var varNow; if (varHours < 10) { varHours = '0' + varHours; } if (varMinutes < 10) { varMinutes = '0' + varMinutes; } if (varSeconds < 10) { varSeconds = '0' + varSeconds; } if (t == "hms") { varNow = varHours + ":" + varMinutes + ":" + varSeconds; } else if (t == "h") { varNow = varHours * 1; } else if (t == "m") { varNow = varMinutes * 1; } else if (t == "s") { varNow = varSeconds * 1; } return varNow; } function get_date(t) { var varDay = new Date(), varYear = varDay.getFullYear(), varMonth = varDay.getMonth() + 1, varDate = varDay.getDate(); var varNow; if (t == "ymd") { varNow = varYear + "/" + varMonth + "/" + varDate; } else if (t == "mdy") { varNow = varMonth + "/" + varDate + "/" + varYear; } else if (t == "dmy") { varNow = varDate + "/" + varMonth + "/" + varYear; } else if (t == "y") { varNow = varYear; } else if (t == "m") { varNow = varMonth; } else if (t == "d") { varNow = varDate; } return varNow; } boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); $demoMonster01.reset(); $demoMonster02.reset(); $demoMonster03.reset(); $demoMonster04.reset(); let $vddwi59 = _startLoop_(); while (_loop_[$vddwi59]) { if (get_time("hms") == '06:30:00') { $demoMonster01.talk('起床囉!'); await buzzerPlay_(_board_, ["c4", "e4", "e4", "0", "e4", "g4", "g4", "0", "d4", "f4", "f4", "0", "a4", "b4", "b4", "0", "c4", "d4", "e4", "c4", "e4", "c4", "e4", "0", "d4", "e4", "f4", "f4", "e4", "d4", "f4", "0", "e4", "f4", "g4", "e4", "g4", "e4", "g4", "0", "f4", "g4", "a4", "a4", "g4", "f4", "a4", "0", "g4", "c4", "d4", "e4", "f4", "g4", "a4", "0", "a4", "d4", "e4", "f4", "g4", "a4", "b4", "0", "b4", "e4", "f4", "g4", "a4", "b4", "c5", "0", "c5", "b4", "a4", "f4", "b4", "g4", "c5"], ["6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6"]); } else { $demoMonster01.talk(([get_date("ymd"), ("<br/>"), get_time("hms")].join(''))); } await delay(1, $vddwi59); await delay(0.005, true); } }); }()); ``` ### 7.空氣品質AQI [7.空氣品質AQI](https://webbit.webduino.io/blockly/#jqwKKW4bvwVqO) ![](https://i.imgur.com/nEDYwQV.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); await getWeather('aqi'); $demoMonster01.talk(getWeatherAqi('臺南', 'all')); if (getWeatherAqi('臺南', 'AQI') >= 0 && getWeatherAqi('臺南', 'AQI') < 50) { _board_._bit_matrix_.setColor(matrixEmoji_('0101011111111110111000100', '#44ff44')); } else if (getWeatherAqi('臺南', 'AQI') >= 51 && getWeatherAqi('臺南', 'AQI') < 100) { _board_._bit_matrix_.setColor(matrixEmoji_('0101011111111110111000100', '#ffff00')); } else { _board_._bit_matrix_.setColor(matrixEmoji_('0101011111111110111000100', '#ff0000')); } }); }()); ``` ## 四. Web:Bit MoonCar - [x] 1 魔幻LED - [x] 2 測試前進、後退、停止 - [x] 3 紅外線遙控 - [x] 4 利用另一塊板子遙控 - [x] 5 手機LINE控制 - [x] 6 顏色偵測 - [x] 7 避障功能 - [x] 8 循跡自走 ### 1 魔幻LED [ 1 魔幻LED](https://webbit.webduino.io/blockly/#wRBvvGKAeMeRM) ![](https://i.imgur.com/ulkdVB6.png) ```javascript= (async function() { var _E5_92_96_E6_A8_82; var i; function colour_random() { var num = Math.floor(Math.random() * Math.pow(2, 24)); return '#' + ('00000' + num.toString(16)).substr(-6); } boardReady({ board: 'Bit', device: 'Web:Bit', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); let $nnnbv652 = _startLoop_(); while (_loop_[$nnnbv652]) { _E5_92_96_E6_A8_82 = colour_random(); let $iprlj487 = _startLoop_(); for (var i = 1; i <= 8; i += 1) { if (!_loop_[$iprlj487]) { break; } WS2812.init(board).setColor(i - 1, _E5_92_96_E6_A8_82); await delay(0.001, true); } await delay(0.2, $nnnbv652); await delay(0.005, true); } }); }()); ``` ### 2 測試前進、後退、停止 [2 測試前進、後退、停止](https://webbit.webduino.io/blockly/#r3444amArpG3d) ![](https://i.imgur.com/4Osm4Fr.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { CarTracker.init(board).action(1); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { CarTracker.init(board).action(4); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('pressed', async function() { CarTracker.init(board).action(0); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 3 紅外線遙控 [3 紅外線遙控](https://webbit.webduino.io/blockly/#8qMzzgW2NJQ3W) ![](https://i.imgur.com/mPMqZiO.png) ```javascript= (async function() { keyboardEvent(); boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); keyDownCode['code65'] = async function() { _board_._bit_matrix_.setColor("000000000100000002ffffff0300000004000000050000000600000007ffffff08000000090000000a0000000bffffff0cffffff0dffffff0e0000000f00000010ffffff11ffffff12ffffff130000001400000015ffffff16ffffff17ffffff18000000"); }; keyDownCode['code83'] = async function() { _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#ffffff')); }; keyDownCode['code68'] = async function() { _board_._bit_matrix_.off(); }; var _keyDownCode70_ = false; keyDownCode['code70'] = async function() { if (!_keyDownCode70_) { _keyDownCode70_ = true; await buzzerPlay_(_board_, ["E7", "E7", "0", "E7", "0", "C7", "E7", "0", "G7", "0", "0", "0", "G6", "0", "0", "0", "C7", "0", "0", "G6", "0", "0", "E6", "0", "0", "A6", "0", "B6", "0", "AS6", "A6", "0", "G6", "E7", "0", "G7", "A7", "0", "F7", "G7", "0", "E7", "0", "C7", "D7", "B6", "0", "0", "C7", "0", "0", "G6", "0", "0", "E6", "0", "0", "A6", "0", "B6", "0", "AS6", "A6", "0", "G6", "E7", "0", "G7", "A7", "0", "F7", "G7", "0", "E7", "0", "C7", "D7", "B6", "0", "0"], ["8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8"]); _keyDownCode70_ = false; } }; var _keyDownCode90_ = false; keyDownCode['code90'] = async function() { if (!_keyDownCode90_) { _keyDownCode90_ = true; buzzerPlayEvent_(_board_, 'stop'); await delay(0.1, true); _keyDownCode90_ = false; } }; var _keyDownCode88_ = false; keyDownCode['code88'] = async function() { if (!_keyDownCode88_) { _keyDownCode88_ = true; await buzzerPlay_(_board_, ["c4", "e4", "e4", "0", "e4", "g4", "g4", "0", "d4", "f4", "f4", "0", "a4", "b4", "b4", "0", "c4", "d4", "e4", "c4", "e4", "c4", "e4", "0", "d4", "e4", "f4", "f4", "e4", "d4", "f4", "0", "e4", "f4", "g4", "e4", "g4", "e4", "g4", "0", "f4", "g4", "a4", "a4", "g4", "f4", "a4", "0", "g4", "c4", "d4", "e4", "f4", "g4", "a4", "0", "a4", "d4", "e4", "f4", "g4", "a4", "b4", "0", "b4", "e4", "f4", "g4", "a4", "b4", "c5", "0", "c5", "b4", "a4", "f4", "b4", "g4", "c5"], ["6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6"]); _keyDownCode88_ = false; } }; }); boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); keyDownCode['code38'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(1); }; keyDownCode['code40'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(4); }; keyDownCode['code37'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(2); }; keyDownCode['code39'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(3); }; keyDownCode['code81'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(7); }; keyDownCode['code87'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(8); }; keyDownCode['code69'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(9); }; keyDownCode['code82'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(10); }; keyDownCode['code32'] = async function() { CarTracker.init(board).setAllSpeed(100); CarTracker.init(board).action(0); }; }); }()); ``` ### 4 利用另一塊板子遙控 [4 利用另一塊板子遙控](https://webbit.webduino.io/blockly/#6RLrr8NK2EZ3V) ![](https://i.imgur.com/6Cerqcg.png) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bitd057e', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wenyu', message: ('a').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wenyu', message: ('b').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnsEvent_('pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wenyu', message: ('c').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } await webduinoBroadcastor.onMessage('wenyu', async (message) => { if (message == 'a') { CarTracker.init(board).action(1); } else if (message == 'b') { CarTracker.init(board).action(4); } else { CarTracker.init(board).action(0); } }); }); }()); ``` ### 5 手機LINE控制 [5 手機LINE控制](https://webbit.webduino.io/blockly/#a35VVbO16dgym) ![](https://i.imgur.com/MZ9iyFs.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); let _chatChannel = line_channel('81911404'); _chatChannel.on("value", async function(_e) { let _msg = ""; if (_e.val()) { _msg = _e.val().msg; } if (_msg) { if (_msg == '1') { CarTracker.init(board).action(1); await line_reply(_e.val().uid, '前進', _e.val().rt); } else if (_msg == '2') { CarTracker.init(board).action(4); await line_reply(_e.val().uid, '後退', _e.val().rt); } else if (_msg == '3') { CarTracker.init(board).action(2); await line_reply(_e.val().uid, '原地左轉', _e.val().rt); } else if (_msg == '4') { CarTracker.init(board).action(3); await line_reply(_e.val().uid, '原地右轉', _e.val().rt); } else if (_msg == '5') { CarTracker.init(board).action(0); await line_reply(_e.val().uid, '停止', _e.val().rt); } } }); }); }()); ``` ### 6 顏色偵測 [6 顏色偵測](https://webbit.webduino.io/blockly/#XROzz4YmEBdyn) ![](https://i.imgur.com/UJmXg2l.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); let $xjboa11 = _startLoop_(); while (_loop_[$xjboa11]) { if ((TCS34725.init(board)._id[0] == 3)) { _board_._bit_matrix_.setCharacter('B', '#44ccff'); } else if ((TCS34725.init(board)._id[0] == 2)) { _board_._bit_matrix_.setCharacter('G', '#44ff44'); } else if ((TCS34725.init(board)._id[0] == 1)) { _board_._bit_matrix_.setCharacter('g', '#4444ff'); } else if ((TCS34725.init(board)._id[0] == 4)) { _board_._bit_matrix_.setCharacter('R', '#ff0000'); } else if ((TCS34725.init(board)._id[0] == 5)) { _board_._bit_matrix_.setCharacter('P', '#cc44cc'); } else if ((TCS34725.init(board)._id[0] == 6)) { _board_._bit_matrix_.setCharacter('Y', '#ffff00'); } await delay(0.005, true); } }); }()); ``` ### 7 避障功能 [7 避障功能](https://webbit.webduino.io/blockly/#7RprrGE4mGB3r) ![](https://i.imgur.com/Hc8jGxz.png) ```javascript= (async function() { var _E5_B7_A6_E5_8F_B3; function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); let $jdktm335 = _startLoop_(); while (_loop_[$jdktm335]) { if (((await Ultrasonic.init(board)).distance || 0) >= 10) { CarTracker.init(board).action(1); } else { CarTracker.init(board).action(4); await delay(math_random_int(0.5, 1), $jdktm335); _E5_B7_A6_E5_8F_B3 = math_random_int(0, 1); if (_E5_B7_A6_E5_8F_B3) { CarTracker.init(board).action(2); } else { CarTracker.init(board).action(3); } await delay(math_random_int(0.5, 1), $jdktm335); } await delay(0.005, true); } }); boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); let $ujmbo415 = _startLoop_(); while (_loop_[$ujmbo415]) { if (((await Ultrasonic.init(board)).distance || 0) >= 20) { CarTracker.init(board).action(1); } else { _E5_B7_A6_E5_8F_B3 = math_random_int(0, 1); if (_E5_B7_A6_E5_8F_B3) { CarTracker.init(board).action(2); } else { CarTracker.init(board).action(3); } await delay(math_random_int(0.5, 1), $ujmbo415); } await delay(0.005, true); } }); }()); ``` ### 8 循跡自走 [8 循跡自走](https://webbit.webduino.io/blockly/#Jy1aa0p77gwyl) ![](https://i.imgur.com/74d45RA.png) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); CarTracker.init(board).setAllSpeed(70); CarTracker.init(board).track('000', 0, async function() { $demoMonster01.talk('跑出軌跡囉!'); }); CarTracker.init(board).track('001', 8, async function() { $demoMonster01.talk('右轉修正!'); }); CarTracker.init(board).track('100', 7, async function() { $demoMonster01.talk('左轉修正!'); }); CarTracker.init(board).track('101', 1, async function() { $demoMonster01.talk('向前行!'); }); CarTracker.init(board).on(); }); }()); ``` ## 五.智慧插座Plus - [x] 1. 按鈕控制 - [x] 2. 揮手控制 - [x] 3. 搖晃控制 - [x] 4. 語音控制 - [x] 5. 翻蓋控制 - [x] 6. 視訊偵測 - [x] 7. App Inventor - [x] 8. LINE 控制 - [x] 9. Google Sheet - [x] 10. 插座回傳 Web:Bit ### 1. 按鈕控制 [1. 按鈕控制](https://webbit.webduino.io/blockly/#b3Gjj726xEVqA) ![](https://i.imgur.com/Oi64P4G.png) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('ON').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('OFF').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ### 2. 揮手控制 [2. 揮手控制](https://webbit.webduino.io/blockly/#VRmXX8L2VOGy2) ![](https://i.imgur.com/vEKHB8b.png) ```javascript= (async function() { var _E8_AE_8A_E6_95_B8; var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "left": true, "right": true }); _E8_AE_8A_E6_95_B8 = false; if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('OFF').toString() }); let $xhnhx133 = _startLoop_(); while (_loop_[$xhnhx133]) { if ((_board_._bit_detected_val_.left) < 100) { _E8_AE_8A_E6_95_B8 = true; } else if (_E8_AE_8A_E6_95_B8 && (_board_._bit_detected_val_.right) < 100) { _E8_AE_8A_E6_95_B8 = false; if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('ON').toString() }); _board_._bit_matrix_.setColor('#ffffff'); } if ((_board_._bit_detected_val_.right) < 100) { _E8_AE_8A_E6_95_B8 = true; } else if (_E8_AE_8A_E6_95_B8 && (_board_._bit_detected_val_.left) < 100) { _E8_AE_8A_E6_95_B8 = false; if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('OFF').toString() }); _board_._bit_matrix_.setColor('#000000'); } await delay(0.005, true); } }); }()); ``` ### 3. 搖晃控制 [3. 搖晃控制](https://webbit.webduino.io/blockly/#b3Gjj72zL2rqA) ![](https://i.imgur.com/8uJGyI1.jpg) ```javascript= (async function() { var _E8_AE_8A_E6_95_B8; var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "gyr": true }); let $onkqk499 = _startLoop_(); while (_loop_[$onkqk499]) { _E8_AE_8A_E6_95_B8 = false; if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('OFF').toString() }); mpu9250Fn_.shake(_board_, async function() { if (_E8_AE_8A_E6_95_B8 == false) { _E8_AE_8A_E6_95_B8 = true; if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('ON').toString() }); _board_._bit_matrix_.setColor('#ff0000'); } else if (_E8_AE_8A_E6_95_B8 == true) { _E8_AE_8A_E6_95_B8 = false; if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1009/set', message: ('OFF').toString() }); _board_._bit_matrix_.setColor('#000000'); } }); await delay(0.005, true); } }); }()); ``` ### 4. 語音控制 [4. 語音控制](https://webbit.webduino.io/blockly/#N3265mKNv9539) ![](https://i.imgur.com/BM2E98V.jpg) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); let $hyjix759 = _startLoop_(); while (_loop_[$hyjix759]) { await speechRecognition('cmn-Hant-TW'); $demoMonster01.talk((speechValue_)); if ((speechValue_).indexOf('開') != -1) { _board_._bit_matrix_.setColor('#ffffff'); if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('ON').toString() }); } else if ((speechValue_).indexOf('關') != -1) { _board_._bit_matrix_.setColor('#000000'); if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); } await delay(0.005, true); } }); }()); ``` ### 5. 翻蓋控制 [5. 翻蓋控制](https://webbit.webduino.io/blockly/#Jy1anmKn080yl) ![](https://i.imgur.com/duAgMOC.jpg) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "acc": true }); mpu9250Fn_.faceFront(_board_, async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('ON').toString() }); _board_._bit_matrix_.setColor('#ffffff'); }); mpu9250Fn_.faceBack(_board_, async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); _board_._bit_matrix_.setColor('#000000'); }); }); }()); ``` ### 7. App Inventor [ 7. App Inventor](https://webbit.webduino.io/blockly/#wRBv8AP481dRM) ![](https://i.imgur.com/M7wftFk.jpg) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); $demoMonster01.talk('點擊開燈'); $demoMonster02.talk('點擊關燈'); $demoMonster01.click(async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('ON').toString() }); $demoMonster01.talk('開'); }); $demoMonster02.click(async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); $demoMonster02.talk('關'); }); }); }()); ``` ### 8. LINE 控制 [8. LINE 控制](https://webbit.webduino.io/blockly/#Z3koQldeedK3v) ![](https://i.imgur.com/6oOStrm.jpg) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); let _chatChannel = line_channel('81911404'); _chatChannel.on("value", async function(_e) { let _msg = ""; if (_e.val()) { _msg = _e.val().msg; } if (_msg) { if (_msg == '開燈') { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('ON').toString() }); } else if (_msg == '關燈') { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); } } }); }); }()); ``` ### 9. Google Sheet [9. Google Sheet](https://webbit.webduino.io/blockly/#XROzO6BornVyn) ![](https://i.imgur.com/lSngDrN.jpg) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); _board_._bit_matrix_.setColor('#ff0000'); let $abqvv313 = _startLoop_(); while (_loop_[$abqvv313]) { sheetInit('https://docs.google.com/spreadsheets/d/1bb2XvAjSUAYowJLkOCtU0MWXKeBXu26ku0t2iidy1Ss/edit?usp=sharing', '工作表1'); await sheetReadData(); if (_mySheet_.data.cell['a1'] == '開燈') { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('ON').toString() }); _board_._bit_matrix_.setColor('#ffffff'); } else if (_mySheet_.data.cell['a1'] == '關燈') { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); _board_._bit_matrix_.setColor('#000000'); } await delay(0.005, true); } }); }()); ``` ### 10. 插座回傳 Web:Bit [10. 插座回傳 Web:Bit](https://webbit.webduino.io/blockly/#a35VpjoagDNym) ![](https://i.imgur.com/PAbm0RY.jpg) ```javascript= (async function() { var webduinoBroadcastor; boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); _board_._bit_matrix_.setColor('#000000'); if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } await webduinoBroadcastor.onMessage('wg1003/state', async (message) => { if (message == 'ON') { _board_._bit_matrix_.setColor('#44ff44'); } else if (message == 'OFF') { _board_._bit_matrix_.setColor('#ff0000'); } }); }); boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { let _board_ = await boardInit_(board, 250, 0); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('ON').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { if (!webduinoBroadcastor) { webduinoBroadcastor = new webduino.module.mqttClient(); await webduinoBroadcastor.connect(); } webduinoBroadcastor.send({ topic: 'wg1003/set', message: ('OFF').toString() }); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ``` ## 六. Web:Bit創意設計 - [x] 1.英文朗讀機 - [x] 2.多功能語言學習機 ### 1.英文朗讀機 [1.英文朗讀機](https://webbit.webduino.io/blockly/#NyWQZlBd95vqb) ![](https://i.imgur.com/dMLAvan.jpg) ```javascript= (async function() { var _E8_8B_B1_E6_96_87; var _E6_97_A5_E6_96_87; keyboardEvent(); boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); $demoMonster01.reset(); $demoMonster02.reset(); $demoMonster03.reset(); $demoMonster04.reset(); sheetInit('https://docs.google.com/spreadsheets/d/1AHqpHT-NkmD6Znfb_1BYzTmyzFJLBJdzPtgvbV-r2E0/edit?usp=sharing', '工作表1'); $demoMonster01.talk('按A翻譯英文 按B翻譯日文'); var _keyDownCode65_ = false; keyDownCode['code65'] = async function() { if (!_keyDownCode65_) { _keyDownCode65_ = true; $demoMonster01.talk('請輸入中文字'); await textInput(); await sheetWriteData('auto', (thisInputVal), 'a2'); await sheetReadData(); _E8_8B_B1_E6_96_87 = _mySheet_.data.cell['b2']; $demoMonster01.talk(''); $demoMonster03.talk(_E8_8B_B1_E6_96_87); await speakAsync(_E8_8B_B1_E6_96_87, ["en-US", 1, 1]); _keyDownCode65_ = false; } }; var _keyDownCode66_ = false; keyDownCode['code66'] = async function() { if (!_keyDownCode66_) { _keyDownCode66_ = true; $demoMonster03.talk(''); $demoMonster01.talk('請輸入中文字'); await textInput(); await sheetWriteData('auto', (thisInputVal), 'a2'); await sheetReadData(); _E6_97_A5_E6_96_87 = _mySheet_.data.cell['c2']; $demoMonster03.talk(_E6_97_A5_E6_96_87); await speakAsync(_E6_97_A5_E6_96_87, ["ja-JP", 1, 1]); _keyDownCode66_ = false; } }; }); }()); ``` ### 2.多功能語言學習機 [2.多功能語言學習機](https://webbit.webduino.io/blockly/#xqVM2zEWjkMR7) ![](https://i.imgur.com/Fzc9asj.jpg) ```javascript= (async function() { var _E5_88_97; var _E8_8B_B1_E6_96_87; var _E4_B8_AD_E6_96_87; function math_random_int(a, b) { if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } return Math.floor(Math.random() * (b - a + 1) + a); } keyboardEvent(); boardReady({ board: 'Bit', device: 'Webduino Bit', multi: true, transport: 'message', window: window.top.frames[0] }, async function(board) { window._board_ = await boardInit_(board, 100, 0); $demoMonster01.reset(); $demoMonster02.reset(); $demoMonster03.reset(); $demoMonster04.reset(); sheetInit('https://docs.google.com/spreadsheets/d/1JkE7gDsM5AYQ2ZmCmUREmdaVyOO1zh4_Hw5WUGI55RY/edit?usp=sharing', '工作表1'); await sheetReadData(); var _keyDownCode65_ = false; keyDownCode['code65'] = async function() { if (!_keyDownCode65_) { _keyDownCode65_ = true; _E5_88_97 = math_random_int(1, _mySheet_.data.lastRow); _E8_8B_B1_E6_96_87 = _mySheet_.data.cell[(String('a') + String(_E5_88_97))]; _E4_B8_AD_E6_96_87 = _mySheet_.data.cell[(String('b') + String(_E5_88_97))]; $demoMonster01.talk(_E8_8B_B1_E6_96_87); $demoMonster02.talk(_E4_B8_AD_E6_96_87); await speakAsync(_E8_8B_B1_E6_96_87, ["en-US", 1, 1]); _keyDownCode65_ = false; } }; var _keyDownCode66_ = false; keyDownCode['code66'] = async function() { if (!_keyDownCode66_) { _keyDownCode66_ = true; await speakAsync(_E8_8B_B1_E6_96_87, ["en-US", 1, 1]); _keyDownCode66_ = false; } }; var _keyDownCode67_ = false; keyDownCode['code67'] = async function() { if (!_keyDownCode67_) { _keyDownCode67_ = true; $demoMonster04.talk('321 開始'); await speechRecognition('en-US'); $demoMonster03.talk((speechValue_)); if ((speechValue_) == _E8_8B_B1_E6_96_87) { $demoMonster04.talk('水拉'); $sound.play('sound-01'); } else { $demoMonster04.talk('唉'); $sound.play('death-01'); } _keyDownCode67_ = false; } }; }); }()); ``` ## 七.法蘭斯自訂積木 ### 安裝 [ 安裝](https://webbit.webduino.io/blockly/#XqDvZ8ednX6y5) ![ 安裝](https://i.imgur.com/ySfyUIW.jpg) ## 八.期末專題 ### 亮度偵測器 [](https://webbit.webduino.io/blockly/#wRBv8PP29AXRM) ![](https://i.imgur.com/g8Ttmha.jpg) ```javascript= (async function() { boardReady({ board: 'Bit', device: 'bite9b8b', transport: 'mqtt', multi: true }, async function(board) { window._board_ = await boardInit_(board, 250, 0); await detectInit_(_board_, { "right": true }); btnEvent_(_board_._bit_btnA_, 'pressed', async function() { if ((_board_._bit_detected_val_.right) <= 300) { _board_._bit_matrix_.setColor('#ffffff'); } else if (300 < (_board_._bit_detected_val_.right) && (_board_._bit_detected_val_.right) < 600) { _board_._bit_matrix_.setColor('#44ff44'); } else if (600 <= (_board_._bit_detected_val_.right)) { _board_._bit_matrix_.setColor('#ff0000'); } }, [_board_._bit_btnA_, _board_._bit_btnB_] ); btnEvent_(_board_._bit_btnB_, 'pressed', async function() { _board_._bit_matrix_.setColor('#000000'); }, [_board_._bit_btnA_, _board_._bit_btnB_] ); }); }()); ```