---
# System prepended metadata

title: WebBit物聯網程式
tags: [110北中筆記]

---

###### tags : `110北中筆記`
# WebBit物聯網程式
### HackMD網址
https://hackmd.io/@lVpCjSq-R5S9qIcTN1lEbQ/S1ZXwqtfY
### 30115黃崇威
[TOC]

## 一、 Webduino Blockly 程式設計

- [x] 1.抽籤並朗讀姓名
- [x] 2. 大樂透自動選號
- [x] 3. 隨機組合朗讀語句
- [x] 4. 小時鐘
- [x] 5. 語音時報

### 1. 抽籤並朗讀姓名
::: info
[01 抽籤並朗讀姓名](https://blocklypro.webduino.io/#RYz5j1DWZv)

![](https://i.imgur.com/7ucc429.png)



```javascript=
var name2;
var result;

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];
  }
}


name2 = ['王大明', '蔡小英', '張小燕', '陳小美', '李小胖'];
result = (lists_random_item(name2, false));
window.alert(result);
speak((['恭喜',result,'變笨蛋'].join('')));
```
:::


### 2. 大樂透自動選號
::: success
[02 大樂透自動選號](https://blocklypro.webduino.io/#RolznboaZ2)
![02 大樂透自動選號](https://i.imgur.com/1eNPy6C.png)
```javascript=
var aRR;
var i;
var result;
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;
}
result = [];
for (j = 1; j <= 6; j++) {
  POSITION = math_random_int(1, (aRR.length));
  NUM = (aRR.splice((POSITION - 1), 1)[0]);
  result[j - 1] = NUM;
}
window.alert(result);
speak((['本期大樂透電腦選號為:',result,'祝你中大獎'].join('')));
```
:::


### 3. 隨機組合朗讀語句
::: info
[03隨機組合朗讀語句](https://blocklypro.webduino.io/#kZJPKpBY5B)
![03隨機組合朗讀語句](https://i.imgur.com/OUXAP7o.png)
```javascript=
var arr1;
var arr2;
var arr3;
var result;

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];
  }
}


arr1 = ['喔', '你'];
arr2 = ['嗨', '好'];
arr3 = ['你好', '嗎'];
result = [(lists_random_item(arr1, false)),(lists_random_item(arr2, false)),(lists_random_item(arr3, false))].join('');
window.alert(result);
speak(result);
```
:::


### 4. 小時鐘
::: success
[04小時鐘](https://blocklypro.webduino.io/#XOrVENrppw)
![04小時鐘](https://i.imgur.com/Rr60fEv.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;
}


while (!false) {
  window.alert(get_time("hms"));
  H = get_time("h");
  M = get_time("m");
  S = get_time("s");
  if (H < 10) {
    H = String(0) + String(H);
  }
  if (M < 10) {
    M = String(0) + String(M);
  }
  if (S < 10) {
    S = String(0) + String(S);
  }
  TIME = [H,':',M,':',S].join('');
  window.alert(TIME);
  await delay(1);
}

}());
```
:::


### 5. 語音時報
::: success
[05語音時報](https://blocklypro.webduino.io/#RdJWqjz2Bb)
![05語音時報](https://i.imgur.com/UINO0yA.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 (H < 10) {
    H = String(0) + String(H);
  }
  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 >= 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;
  } else if (H == 0) {
    TEXT = '半夜';
  }
  speak(TEXT);
});

true && H < 5;

}());
```
:::



## 二、Webbit 開發板基本操作
- [x] 1. 心跳撲通通
- [x] 2. 看誰按得快
- [x] 3. 流動的沙子
- [x] 4. 隨機顯示數字
- [x] 5. 溫度計
- [x] 6. 搖骰子
- [x] 7. 指北針
- [x] 8. 計步器
- [x] 9. 播放音樂(&音效語音)
- [x] Line 作業 1~回傳表情符號
- [x] Line 作業 2~回傳氣象資訊
- [x] 作業1:怪獸亂跑亂轉
- [x] 作業2:鍵盤控制怪獸移動
- [x] 作業3:顯示倒數5秒
- [x] 作業4:語音客服
- [x] 作業5:抽籤選學生
- [x] 作業6:空氣品質即時資訊
- [x] 作業7:偵測亮度及溫度
- [x] 作業8:計步器
- [x] 自己添加(1) Line(開關燈)
- [x] 自己添加(2) 製作空汙旗

### 1. 心跳撲通通
:::info
[跳動的心](https://webbit.webduino.io/blockly/?demo=default#NyWQQEjPJnWqb)
![跳動的心](https://i.imgur.com/XGoHKu4.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 $ajyxw421 = _startLoop_();
    while (_loop_[$ajyxw421]) {
      _board_._bit_matrix_.setColor("00000000010000000200000003000000040000000500000006ff00000700000008ff0000090000000a0000000bff00000cff00000dff00000e0000000f0000001000000011ff000012000000130000001400000015000000160000001700000018000000");
      await delay(1, $ajyxw421);
      _board_._bit_matrix_.setColor("0000000001ff00000200000003ff00000400000005ff000006ff00000700000008ff000009ff00000aff00000bff00000cff00000dff00000eff00000f00000010ff000011ff000012ff000013000000140000001500000016ff00001700000018000000");
      await delay(1, $ajyxw421);
      await delay(0.005, true);
    }


  });

}());
```
:::

### 2. 看誰按得快
:::success
[看誰按得快](https://webbit.webduino.io/blockly/?demo=default#YqKEEj9oNOdy4)
![](https://i.imgur.com/YObyw6L.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_('0000001010000001000101110', '#ff0000'));
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnB_, 'pressed',
      async function() {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000001000101110', '#44cc00'));
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnsEvent_('pressed',
      async function() {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000001000101110', '#ffff00'));
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::

### 3. 流動的沙子
::: info
[流動的沙子](https://webbit.webduino.io/blockly/?demo=default#VRmXXVak08oy2)
![](https://i.imgur.com/lGK818m.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_, {
      "acc": true,
      "ang": true
    });
    mpu9250Fn_.faceFront(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000001000101110', '#ffffff'));
    });
    mpu9250Fn_.rowForward(_board_, async function() {
      _board_._bit_matrix_.setColor("00ffffff01ffffff02ffffff03ffffff04ffffff05ffffff06ffffff07ffffff08ffffff09ffffff0a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000");
    });
    mpu9250Fn_.rowBack(_board_, async function() {
      _board_._bit_matrix_.setColor("000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000fffffff10ffffff11ffffff12ffffff13ffffff14ffffff15ffffff16ffffff17ffffff18ffffff");
    });
    mpu9250Fn_.pitchRight(_board_, async function() {
      _board_._bit_matrix_.setColor("00000000010000000200000003ffffff04ffffff05000000060000000700000008ffffff09ffffff0a0000000b0000000c0000000dffffff0effffff0f000000100000001100000012ffffff13ffffff14000000150000001600000017ffffff18ffffff");
    });
    mpu9250Fn_.pitchLeft(_board_, async function() {
      _board_._bit_matrix_.setColor("00ffffff01ffffff02000000030000000400000005ffffff06ffffff0700000008000000090000000affffff0bffffff0c0000000d0000000e0000000fffffff10ffffff11000000120000001300000014ffffff15ffffff160000001700000018000000");
    });

  });

}());
```
:::

### 4. 隨機顯示數字
::: success
[隨機顯示數字](https://webbit.webduino.io/blockly/?demo=default#MqAllO5xDO5RY)
![](https://i.imgur.com/uSv9aeH.png)
``` 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',
    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_.setCharacter(((math_random_int(0, 9))).toString(), '#ff0000');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::

### 5. 溫度計
::: info
[溫度計](https://webbit.webduino.io/blockly/?demo=default#VRmXXVP7rOoy2)
![](https://i.imgur.com/tdNATfV.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_, {
      "temp": true
    });
    $demoMonster01.talk((_board_._bit_detected_val_.temp));
    _board_._bit_matrix_.setString(((_board_._bit_detected_val_.temp)).toString(), '#ff0000', 2);

  });

}());
```
:::

### 6. 搖骰子
::: success
[搖骰子](https://webbit.webduino.io/blockly/?demo=default#NyWQQEwwK2vqb)
![](https://i.imgur.com/QzyN9F6.png)

``` 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: '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
    });
    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', '#ff0000'));
      }
      if (_E8_AE_8A_E6_95_B8 == 2) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000010000000', '#ff0000'));
      }
      if (_E8_AE_8A_E6_95_B8 == 3) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000101000000', '#ff0000'));
      }
      if (_E8_AE_8A_E6_95_B8 == 4) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000000101000000', '#ff0000'));
      }
      if (_E8_AE_8A_E6_95_B8 == 5) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000001010001000101000000', '#ff0000'));
      }
      if (_E8_AE_8A_E6_95_B8 == 6) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0101000000010100000001010', '#ff0000'));
      }
    });

  });

}());
```
:::

### 7. 指北針
:::info
[指北針](https://webbit.webduino.io/blockly/?demo=default#EqdnnM1Ll1By6)
![](https://i.imgur.com/qLG92oP.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_, {
      "azi": true
    });
    mpu9250Fn_.faceNorth(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010001110101010010000100', '#ff0000'));
    });
    mpu9250Fn_.faceWest(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010000010111110001000100', '#ff0000'));
    });
    mpu9250Fn_.faceSouth(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010000100101010111000100', '#ff0000'));
    });
    mpu9250Fn_.faceEast(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010001000111110100000100', '#ff0000'));
    });
    $demoMonster01.talk(_board_._bit_mpu9250_val_.azi);

  });

}());
```
:::

### 8. 計步器
::: success
[計步器](https://webbit.webduino.io/blockly/?demo=default#XROzz6BY5rByn)
![](https://i.imgur.com/nb9qsf0.png)


``` javascript=
(async function() {

  var _E8_A8_88_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_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() {
        $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_]
    );
  });

}());
```
:::

### 9. 播放音樂(&音效語音)
::: info
[播放音樂(&音效語音)](https://webbit.webduino.io/blockly/?demo=default#ayxAAexZp5ERM)
![](https://i.imgur.com/74BnTaX.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() {
        buzzerPlayEvent_(_board_, 'stop');
        await delay(0.1, true);
        await buzzerPlay_(_board_, [('G5')], [(2)]);
        await buzzerPlay_(_board_, [('E5')], [(2)]);
        await buzzerPlay_(_board_, [('E5')], [(2)]);
        await buzzerPlay_(_board_, [('F5')], [(2)]);
        await buzzerPlay_(_board_, [('D5')], [(2)]);
        await buzzerPlay_(_board_, [('D5')], [(2)]);
        await buzzerPlay_(_board_, [('C5')], [(2)]);
        $sound.play('dog-01');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnB_, 'pressed',
      async function() {
        buzzerPlayEvent_(_board_, 'stop');
        await delay(0.1, 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"]);
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::

### Line作業 1~回傳表情符號
 ::: success
 [Line(傳表情符號)](https://webbit.webduino.io/blockly/#6yYGJrlk20pqw) 
 ![](https://i.imgur.com/OFNFJgI.png)
 ![](https://i.imgur.com/k0i0E0P.jpg)
``` javascript
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let _chatChannel = line_channel('185517354');
    _chatChannel.on("value", async function(_e) {
      let _msg = "";
      if (_e.val()) {
        _msg = _e.val().msg;
      }
      if (_msg) {
        if (_msg == 'surprise') {
          $demoMonster01.talk(_msg);
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 3,
            type: "sticker"
          }, _e.val().rt);
        } else {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 2,
            type: "sticker"
          }, _e.val().rt);
        }
      }
    });

  });

}());
```
 :::
 ### Line 作業 2~回傳表情符號
 ::: info
 [Line回報氣象資訊](https://webbit.webduino.io/blockly/#7RprMXP8v9z3r) 
 ![](https://i.imgur.com/pNw3Qsk.png)
 ``` javascript
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let _chatChannel = line_channel('185517354');
    _chatChannel.on("value", async function(_e) {
      let _msg = "";
      if (_e.val()) {
        _msg = _e.val().msg;
      }
      if (_msg) {
        await getWeather('aqi');
        if (_msg == 'AQI' || _msg == 'aqi') {
          await line_reply(_e.val().uid, getWeatherAqi('臺南', 'all'), _e.val().rt);
        }
        await getWeather('observe');
        if (_msg == 'now' || _msg == 'NOW') {
          await line_reply(_e.val().uid, getWeatherAqi('臺南', 'all'), _e.val().rt);
        }
        await getWeather('forecast');
        if (_msg == 'forecast' || _msg == 'FORECAST') {
          await line_reply(_e.val().uid, getWeatherForecast('臺南市', '0'), _e.val().rt);
        }
        await getWeather('radar');
        if (_msg == 'RADAR' || _msg == 'radar') {
          await line_reply(_e.val().uid, getWeatherRadar(), _e.val().rt);
        }
      }
    });

  });

}());
 ```
 :::
### 作業1: 怪獸亂跑亂轉
::: success
[怪獸亂跑亂轉](https://webbit.webduino.io/blockly/?demo=default#GqNzzaZ74OXqg)
![](https://i.imgur.com/n3Uusyo.png)
``` javascript=
    (async function() {

  let $iagvv143 = _startLoop_();
  while (_loop_[$iagvv143]) {
    $demoMonster01.move('random', 20);
    $demoMonster02.move('random', 20);
    $demoMonster03.move('random', 20);
    $demoMonster04.move('random', 20);
    $demoMonster01.rotate('left', 30);
    $demoMonster02.rotate('left', 30);
    $demoMonster03.rotate('left', 30);
    $demoMonster04.rotate('left', 30);
    $demoMonster01.rebound();
    $demoMonster02.rebound();
    $demoMonster03.rebound();
    $demoMonster04.rebound();
    await delay(0.1, $iagvv143);
    await delay(0.005, true);
  }

}());
```
:::

### 作業2:鍵盤控制怪獸移動
::: info
[鍵盤控制怪獸移動](https://webbit.webduino.io/blockly/?demo=default#7RprrowQGPV3r)
![](https://i.imgur.com/qlXQMgi.png)
``` javascript=
(async function() {

  keyboardEvent();


  keyDownCode['code38'] = async function() {
    $demoMonster01.move('top', 10);
  };
  keyDownCode['code40'] = async function() {
    $demoMonster01.move('bottom', 10);
  };
  keyDownCode['code37'] = async function() {
    $demoMonster01.move('left', 10);
  };
  keyDownCode['code39'] = async function() {
    $demoMonster01.move('right', 10);
  };
  let $tyvqc571 = _startLoop_();
  while (_loop_[$tyvqc571]) {
    $demoMonster01.rebound();
    await delay(0.005, true);
  }

}());
```
:::

### 作業3:顯示倒數5秒
::: success
[顯示倒數5秒](https://webbit.webduino.io/blockly/?demo=default#JyeOO5PXdzKqV)
![](https://i.imgur.com/Rvf8Z3S.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 $vujwu618 = _startLoop_();
        for (var i = 5; i >= 1; i -= 1) {
          if (!_loop_[$vujwu618]) {
            break;
          }
          _board_._bit_matrix_.setCharacter(i, '#ff0000');
          await delay(1, $vujwu618);
          await delay(0.001, true);
        }

        let $fywty369 = _startLoop_();
        for (let count = 0; count < 10; count++) {
          if (!_loop_[$fywty369]) {
            break;
          }
          _board_._bit_matrix_.setColor(matrixEmoji_('0000000000001000000000000', '#ffff00'));
          await delay(0.1, $fywty369);
          _board_._bit_matrix_.setColor(matrixEmoji_('0000001110011100111000000', '#ffff44'));
          await delay(0.1, $fywty369);
          await delay(0.001, true);

        }
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::

### 作業4:語音客服
::: info
[語音客服](https://webbit.webduino.io/blockly/?demo=default#YqKEEbYo4vmy4)
![](https://i.imgur.com/RjMB4jR.png)
``` javascript=
(async function() {

  let $ufsge918 = _startLoop_();
  while (_loop_[$ufsge918]) {
    $demoMonster01.talk('請問您幾歲?');
    await speakAsync('請問您幾歲?', ["zh-TW", 1, 1]);
    await textInput();
    if ((thisInputVal) < 7) {
      $demoMonster01.talk('您可以免費入場');
      await speakAsync('您可以免費入場', ["zh-TW", 1, 1]);
      await delay(5, $ufsge918);
    } else {
      $demoMonster01.talk('請購票入場');
      await speakAsync('請購票入場', ["zh-TW", 1, 1]);
      await delay(5, $ufsge918);
    }
    await delay(0.005, true);
  }

}());
```
:::

### 作業5:抽籤選學生
::: success
[抽籤選學生](https://webbit.webduino.io/blockly/?demo=default#wqaWWQAn2XYyY)
![](https://i.imgur.com/PZja8iw.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 = ['Curry', 'Irving', 'Kobe', 'Jordan', 'Lebron', 'Harden'];
$demoMonster01.click(function() {
  $demoMonster01.talk((lists_random_item(_E5_90_8D_E5_96_AE, false)));
});
```
:::

### 作業6:空氣品質即時資訊
::: info
[空氣品質即時資訊](https://webbit.webduino.io/blockly/#JyeOO5PP7l0qV)
![](https://i.imgur.com/W53qs9L.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 $oaqre312 = _startLoop_();
    while (_loop_[$oaqre312]) {
      await getWeather('aqi');
      AQI = getWeatherAqi('臺南', 'AQI');
      $demoMonster01.talk(AQI);
      if (AQI <= 50) {
        _board_._bit_matrix_.setColor('#44ff44');
        $demoMonster02.talk('良好');
      } else if (AQI <= 100) {
        _board_._bit_matrix_.setColor('#ffff44');
        $demoMonster02.talk('普通');
      } else if (AQI <= 150) {
        _board_._bit_matrix_.setColor('#ffcc44');
        $demoMonster02.talk('對敏感族群不健康');
      } else {
        _board_._bit_matrix_.setColor('#ff0000');
        $demoMonster02.talk('對所有族群不健康');
      }
      await delay(300, $oaqre312);
      await delay(0.005, true);
    }


  });

}());
```
:::

### 作業7:偵測亮度及溫度
::: success
[偵測亮度及溫度](https://webbit.webduino.io/blockly/#ARzvvYDYGMeR2)
![](https://i.imgur.com/E0ARrFV.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 $aujwo505 = _startLoop_();
    while (_loop_[$aujwo505]) {
      _E4_BA_AE_E5_BA_A6 = math_mean([(_board_._bit_detected_val_.left), (_board_._bit_detected_val_.right)]);
      $demoMonster01.talk((['現在溫度', (_board_._bit_detected_val_.temp), '。C'].join('')));
      $demoMonster02.talk((['平均亮度', _E4_BA_AE_E5_BA_A6, '流明'].join('')));
      if (_E4_BA_AE_E5_BA_A6 < 200) {
        _board_._bit_matrix_.setColor('#ffff00');
      } else {
        _board_._bit_matrix_.off();
      }
      await delay(0.005, true);
    }


  });

}());
```
:::

### 作業8:計步器
::: info
[計步器](https://webbit.webduino.io/blockly/#MqAllP5g9dpRY)
![](https://i.imgur.com/tc4CJkZ.png)
```javascript=
(async function() {

  var _E8_A8_88_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_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_]
    );
  });

}());
```
:::
### 自己添加(1) Line(開關燈)
 ::: success
 [Line(開關燈)](https://webbit.webduino.io/blockly/#Vyj67kOm8K0yY) 
 ![](https://i.imgur.com/U5EDfGf.png)
 ``` javascript
 (async function() {

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let _chatChannel = line_channel('185517354');
    _chatChannel.on("value", async function(_e) {
      let _msg = "";
      if (_e.val()) {
        _msg = _e.val().msg;
      }
      if (_msg) {
        $demoMonster01.talk(_msg);
        if (_msg == '開燈') {
          _board_._bit_matrix_.setColor('#ffffff');
        }
        if (_msg == '關燈') {
          _board_._bit_matrix_.off();
        }
      }
    });

  });

}());
 ```
 :::
 
 ### 自己添加(2) 製作空汙旗
 ::: info
 [製作空污旗](https://webbit.webduino.io/blockly/#rybb1oMAKpWyk) 
 ![](https://i.imgur.com/IKQnS8M.png)
``` javascript
(async function() {

  var AQI;
  var _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF;
  var _E9_A1_8F_E8_89_B2;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $nulcs819 = _startLoop_();
    while (_loop_[$nulcs819]) {
      await getWeather('aqi');
      AQI = getWeatherAqi('臺南', 'AQI');
      if (AQI <= 50) {
        _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF = 'Good';
        _E9_A1_8F_E8_89_B2 = '#33cc00';
      } else if (AQI > 50 && AQI <= 100) {
        _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF = 'Moderate';
        _E9_A1_8F_E8_89_B2 = '#ffcc00';
      } else if (AQI > 100 && AQI <= 150) {
        _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF = 'Unhealthy for sensitive groups';
        _E9_A1_8F_E8_89_B2 = '#ff6600';
      } else if (AQI > 150 && AQI <= 200) {
        _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF = 'Unhealthy';
        _E9_A1_8F_E8_89_B2 = '#ff0000';
      } else if (AQI > 200 && AQI <= 300) {
        _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF = 'Very unhealthy';
        _E9_A1_8F_E8_89_B2 = '#6600cc';
      } else {
        _E5_B0_8D_E8_BA_AB_E9_AB_94_E7_9A_84_E5_BD_B1_E9_9F_BF = 'Hazardous';
        _E9_A1_8F_E8_89_B2 = '#993300';
      }
      _board_._bit_matrix_.setColor(_E9_A1_8F_E8_89_B2);
      await delay(30, $nulcs819);
      await delay(0.005, true);
    }


  });

}());
 ```
 :::

## 三、 Web:Bit 實作練習題
- [x] 01 線上控制測試Bit
- [x] 02 跑馬燈
- [x] 03 燈號報數
- [x] 04 剪刀石頭布
- [x] 05 環能控光燈
- [x] 06 電子鬧鐘
- [x] 07 空氣品質AQI

 ### 01 線上控制測試Bit
 :::success
 ![](https://i.imgur.com/khQiyU5.png)
 :::

 ### 02 跑馬燈
 ::: info
 [01 跑馬燈](https://webbit.webduino.io/blockly/#rybbb5jo5vmyk)
 ![](https://i.imgur.com/c7JGGXZ.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', '#ffff00');
    await delay(1, true); //delay
    _board_._bit_matrix_.setCharacter('2', '#ffff00');
    await delay(1, true); //delay
    _board_._bit_matrix_.setCharacter('1', '#ffff00');
    await delay(1, true); //delay
    await _board_._bit_matrix_.setStringOnce('Go!', '#ff0000', 2);

  });

}());
 ```
 :::
 
 ### 03 燈號報數
 ::: success
 [02 燈號報數](https://webbit.webduino.io/blockly/#YqKEEDYEMQny4)
 ![](https://i.imgur.com/4spBpjZ.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 $aevgq128 = _startLoop_();
    for (var i = 1; i <= 25; i += 1) {
      if (!_loop_[$aevgq128]) {
        break;
      }
      _board_._bit_matrix_.setColor((i - 1), (colour_random()));
      await delay(0.2, $aevgq128);
      await delay(0.001, true);
    }


  });

}());
 ```
:::
 ### 04 剪刀石頭布
 ::: info
 [03 剪刀石頭布](https://webbit.webduino.io/blockly/#py6LLQ6b0GXqZ)
 ![](https://i.imgur.com/jdDpQzl.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', '#ffff00'));
      }, [_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', '#44ff44'));
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnsEvent_('released',
      async function() {
        _board_._bit_matrix_.off();
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
``` 
:::
 ### 05 環能控光燈
 ::: success
 [04 環能控光燈](https://webbit.webduino.io/blockly/#VRmXXY8nJwGy2)
 ![](https://i.imgur.com/8Gq3aIz.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 $unwvf550 = _startLoop_();
    while (_loop_[$unwvf550]) {
      $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);
    }


  });

}());
```
:::
 ### 06 電子鬧鐘
 ::: info
 [05 電子鬧鐘](https://webbit.webduino.io/blockly/#8qMzzZ2OZLZ3W)
![](https://i.imgur.com/KMoUTUp.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 $qdnkc102 = _startLoop_();
    while (_loop_[$qdnkc102]) {
      if (get_time("hms") == '06:30:00') {
        $demoMonster01.talk('起床囉!');
        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"]);
      } else {
        $demoMonster01.talk(([get_date("ymd"), ("<br/>"), get_time("hms")].join('')));
      }
      await delay(1, $qdnkc102);
      await delay(0.005, true);
    }


  });

}());
```
:::
 ### 07 空氣品質AQI
 ::: success
 [06 空氣品質AQI](https://webbit.webduino.io/blockly/#myJ11AE6W7Z3d)
 ![](https://i.imgur.com/SfoYXv5.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'));
    }

  });

}());
 ```
 :::
 
 ## WebBit: MoonCar登月小車
 - [x] 01 魔幻LED
 - [x] 02 測試前進、後退、停止
 - [x] 03 紅外線遙控
 - [x] 04 利用另一塊板子遙控
 - [x] 05 手機LINE控制
 - [x] 06 顏色偵測
 - [x] 07 避障功能
 - [x] 08 循跡自走

### 01 魔幻LED
:::info
[魔幻LED](https://webbit.webduino.io/blockly/#ayxAALNABLlRM)
![](https://i.imgur.com/tF5Sfnp.png)
``` javascript
(async function() {

  var color;
  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: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 2);
    let $ovrku557 = _startLoop_();
    while (_loop_[$ovrku557]) {
      color = colour_random();
      let $mrqgi303 = _startLoop_();
      for (var i = 1; i <= 8; i += 1) {
        if (!_loop_[$mrqgi303]) {
          break;
        }
        WS2812.init(board).setColor(i - 1, color);
        await delay(0.001, true);
      }

      await delay(0.2, $ovrku557);
      await delay(0.005, true);
    }


  });

}());
```
:::
### 02 測試前進、後退、停止
::: success
[測試前進、後退、停止](https://webbit.webduino.io/blockly/#7Rprr8QMj103r)
![](https://i.imgur.com/cEsWJ5s.png)
``` javascript
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    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_]
    );
  });

}());
```
:::
### 03 紅外線遙控
:::info
[紅外線遙控](https://webbit.webduino.io/blockly/#Jy1aa0bWPgpyl)
![](https://i.imgur.com/csIEjoA.png)
``` javascript
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    IrRecv.init(board, 1).receive(async value => {
      $demoMonster01.talk((value || ''));
    }, () => {});
    IrRecv.init(board, 1).receive(async value => {
      if ((value || '') == '00ff629d' || (value || '') == '3d9ae3f7') {
        CarTracker.init(board).setAllSpeed(40);
        CarTracker.init(board).action(1);
      }
      if ((value || '') == '00ffa857' || (value || '') == '1bc0157b') {
        CarTracker.init(board).setAllSpeed(40);
        CarTracker.init(board).action(4);
      }
      if ((value || '') == '00ff22dd' || (value || '') == '8c22657b') {
        CarTracker.init(board).setAllSpeed(40);
        CarTracker.init(board).action(2);
      }
      if ((value || '') == '00ffc23d' || (value || '') == '449e79f') {
        CarTracker.init(board).setAllSpeed(40);
        CarTracker.init(board).action(3);
      }
      if ((value || '') == '00ff02fd' || (value || '') == '488f3cbb') {
        CarTracker.init(board).setAllSpeed(40);
        CarTracker.init(board).action(0);
      }
    }, () => {});

  });

}());
```
:::
### 04 利用另一塊板子遙控
::: success
[利用另一塊板子遙控](https://webbit.webduino.io/blockly/#Mq011LWBGNX30)
![](https://i.imgur.com/o2G1s4s.png)
``` javascript
var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    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: 'bit86660',
    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);
      }
    });

  });

}());
```
:::
### 05 手機LINE控制
:::info
[手機LINE控制](https://webbit.webduino.io/blockly/#YqKEEQG9xgmy4)
![](https://i.imgur.com/vtTuUCk.png)
``` javascript
boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let _chatChannel = line_channel('52A07415');
    _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);
        }
      }
    });

  });

}());
```
:::
### 06 顏色偵測
::: success
[顏色偵測](https://webbit.webduino.io/blockly/#nqnoozdXKDG3Z)
![](https://i.imgur.com/n7TUfAg.png)
```javascript=
boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    let _board_ = await boardInit_(board, 250, 0);
    let $duptt555 = _startLoop_();
    while (_loop_[$duptt555]) {
      if ((TCS34725.init(board)._id[0] == 1)) {
        _board_._bit_matrix_.setCharacter('B', '#000099');
      } else if ((TCS34725.init(board)._id[0] == 2)) {
        _board_._bit_matrix_.setCharacter('G', '#009900');
      } else if ((TCS34725.init(board)._id[0] == 3)) {
        _board_._bit_matrix_.setCharacter('g', '#44ccff');
      } 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', '#6600cc');
      } else if ((TCS34725.init(board)._id[0] == 6)) {
        _board_._bit_matrix_.setCharacter('Y', '#ffff44');
      } else {
        _board_._bit_matrix_.off();
      }
      await delay(0.005, true);
    }


  });

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    let _board_ = await boardInit_(board, 250, 0);
    let $atwgr997 = _startLoop_();
    while (_loop_[$atwgr997]) {
      if ((TCS34725.init(board)._id[0] == 2)) {
        $demoMonster01.talk('前進');
        _board_._bit_matrix_.setColor('#44ff44');
        CarTracker.init(board).action(1);
      } else if ((TCS34725.init(board)._id[0] == 4)) {
        $demoMonster01.talk('停止');
        _board_._bit_matrix_.setColor('#ff0000');
        CarTracker.init(board).action(0);
      }
      await delay(0.3, $atwgr997);
      await delay(0.005, true);
    }


  });

}());
```
:::
### 07 避障功能
:::info
[避障功能](https://webbit.webduino.io/blockly/#8RPzzM27Om73b)
![](https://i.imgur.com/q73EuJS.png)
```javascript=
(async function() {

  var _E5_B7_A6_E5_8F_B3_E8_BD_89;

  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: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $dvbhy265 = _startLoop_();
    while (_loop_[$dvbhy265]) {
      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), $dvbhy265);
        _E5_B7_A6_E5_8F_B3_E8_BD_89 = math_random_int(0, 1);
        if (_E5_B7_A6_E5_8F_B3_E8_BD_89) {
          CarTracker.init(board).action(2);
        } else {
          CarTracker.init(board).action(3);
        }
        await delay(math_random_int(0.5, 1), $dvbhy265);
      }
      await delay(0.005, true);
    }


  });

}());
```
:::
### 08 循跡自走
 ::: success
 [循跡自走](https://webbit.webduino.io/blockly/#jqwKKVYnv2gqO)
 ![](https://i.imgur.com/nVlv4cc.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    CarTracker.init(board).setAllSpeed(70);
    CarTracker.init(board).track('000', 11, 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();

  });

}());
```
 :::
## 智慧插座
- [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. 按鈕控制
::: info
[按鈕控制](https://webbit.webduino.io/blockly/#XROzzgBVJOdyn)
![](https://i.imgur.com/7BtYp6f.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    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: 'wg1007/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: 'wg1007/set',
          message: ('OFF').toString()
        });
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::

### 2. 揮手控制
:::success
[揮手控制](https://webbit.webduino.io/blockly/#VR9kmXjPkln31)
![](https://i.imgur.com/zI5kcNb.png)
``` javascript
(async function() {

  var _E8_AE_8A_E6_95_B8;
  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    url: '127.0.0.1:8080',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 100, 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: 'wg1003/set',
      message: ('OFF').toString()
    });
    let $pehgc969 = _startLoop_();
    while (_loop_[$pehgc969]) {
      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: 'wg1003/set',
          message: ('ON').toString()
        });
        _board_._bit_matrix_.setColor('#ffffff');
      }
      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: 'wg1003/set',
          message: ('OFF').toString()
        });
        _board_._bit_matrix_.setColor('#000000');
      }
      await delay(0.005, true);
    }


  });

}());
```
:::

### 3. 搖晃控制
::: info
[搖晃控制](https://webbit.webduino.io/blockly/#Jy1aajKD8K4yl)
![](https://i.imgur.com/U0TK29R.png)
``` javascript
(async function() {

  var webduinoBroadcastor;
  var _E8_AE_8A_E6_95_B8;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 2);
    await detectInit_(_board_, {
      "gyr": true
    });


    if (!webduinoBroadcastor) {
      webduinoBroadcastor = new webduino.module.mqttClient();
      await webduinoBroadcastor.connect();
    }

    webduinoBroadcastor.send({
      topic: 'wg1007/set',
      message: ('OFF').toString()
    });
    _E8_AE_8A_E6_95_B8 = false;
    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: 'wg1007/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: 'wg1007/set',
          message: ('OFF').toString()
        });
        _board_._bit_matrix_.setColor('#000000');
      }
    });

  });

}());
```
:::

### 4. 語音控制
::: success
[語音控制](https://webbit.webduino.io/blockly/#ayxAADxdl82RM)
![](https://i.imgur.com/YHaEZa5.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    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: 'wg1007/set',
      message: ('OFF').toString()
    });
    let $knbic185 = _startLoop_();
    while (_loop_[$knbic185]) {
      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: 'wg1007/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: 'wg1007/set',
          message: ('OFF').toString()
        });
      }
      await delay(0.005, true);
    }


  });

}());
```
:::

### 5. 翻蓋控制
::: info
[翻蓋控制](https://webbit.webduino.io/blockly/#Vyj66DPd4N0yY)
![](https://i.imgur.com/ILEwdbu.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    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: 'wg1007/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: 'wg1007/set',
        message: ('OFF').toString()
      });
      _board_._bit_matrix_.setColor('#000000');
    });

  });

}());
```
:::

### 6. 視訊偵測
::: success
[視訊偵測](https://webbit.webduino.io/blockly/#Qq700wkooA13B)
![](https://i.imgur.com/ZKV6qa4.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bit86660',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    $demoMonster01.moveTo(200, 200);
    $demoMonster02.moveTo(400, 200);
    $demoMonster03.sizeTo(30);
    $demoMonster04.sizeTo(30);
    $demoMonster01.talk('摸我開燈');
    $demoMonster02.talk('摸我關燈');
    let $mddmw206 = _startLoop_();
    while (_loop_[$mddmw206]) {
      if (await videoDetect__.isTouch('demoMonster01', 'any')) {


        if (!webduinoBroadcastor) {
          webduinoBroadcastor = new webduino.module.mqttClient();
          await webduinoBroadcastor.connect();
        }

        webduinoBroadcastor.send({
          topic: 'wg1003/set',
          message: ('ON').toString()
        });
        _board_._bit_matrix_.setColor('#ffffff');
        $demoMonster03.talk('開燈');
        await delay(1, $mddmw206);
        $demoMonster03.talk('');
      } else if (await videoDetect__.isTouch('demoMonster02', 'any')) {


        if (!webduinoBroadcastor) {
          webduinoBroadcastor = new webduino.module.mqttClient();
          await webduinoBroadcastor.connect();
        }

        webduinoBroadcastor.send({
          topic: 'wg1003/set',
          message: ('OFF').toString()
        });
        _board_._bit_matrix_.setColor('#000000');
        $demoMonster04.talk('關燈');
        await delay(1, $mddmw206);
        $demoMonster04.talk('');
      }
      await delay(0.005, true);
    }


  });

}());
```
:::

### 7. App Inventor
::: info
[App Inventor](https://webbit.webduino.io/blockly/#xqVMMbEw1M1R7)
![](https://i.imgur.com/3QTVqfy.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  $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()
    });
    $demoMonster03.talk('開');
    await delay(1, true); //delay
    $demoMonster03.talk('');
  });
  $demoMonster02.click(async function() {


    if (!webduinoBroadcastor) {
      webduinoBroadcastor = new webduino.module.mqttClient();
      await webduinoBroadcastor.connect();
    }

    webduinoBroadcastor.send({
      topic: 'wg1003/set',
      message: ('OFF').toString()
    });
    $demoMonster04.talk('關');
    await delay(1, true); //delay
    $demoMonster04.talk('');
  });

}());
```
:::

### 8. LINE 控制
:::success
[LINE 控制](https://webbit.webduino.io/blockly/#Vyj678EDpzZyY) 
![](https://i.imgur.com/aQODwvE.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  let _chatChannel = line_channel('185517354');
  _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
::: info
[Google Sheet](https://webbit.webduino.io/blockly/#r344nva2jkj3d)
![](https://i.imgur.com/H8nwvUD.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    url: '127.0.0.1:8080',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 100, 0);
    _board_._bit_matrix_.setColor('#ff0000');
    sheetInit('https://docs.google.com/spreadsheets/d/1DVTzv7ubIi-YrqRiHUF-nZ92uRVw6aH-sktoLQ6Fo3I/edit?usp=sharing', '工作表1');
    let $hmxcv610 = _startLoop_();
    while (_loop_[$hmxcv610]) {
      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()
        });
      } else if (_mySheet_.data.cell['a1'] == '關燈') {


        if (!webduinoBroadcastor) {
          webduinoBroadcastor = new webduino.module.mqttClient();
          await webduinoBroadcastor.connect();
        }

        webduinoBroadcastor.send({
          topic: 'wg1003/set',
          message: ('OFF').toString()
        });
      }
      await delay(0.005, true);
    }


  });

}());
```
:::

### 10. 插座回傳 Web:Bit
:::success
[插座回傳 Web:Bit](https://webbit.webduino.io/blockly/#8qMzD5G2DlB3W)
![](https://i.imgur.com/kmexYpI.png)
``` javascript
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    url: '127.0.0.1:8080',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 100, 0);
    _board_._bit_matrix_.setColor('#440000');
    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_]
    );
    if (!webduinoBroadcastor) {
      webduinoBroadcastor = new webduino.module.mqttClient();
      await webduinoBroadcastor.connect();
    }

    await webduinoBroadcastor.onMessage('wg1003/set', async (message) => {
      if (message == 'ON') {
        _board_._bit_matrix_.setColor('#ff0000');
      } else if (message == 'OFF') {
        _board_._bit_matrix_.setColor('#ffcc66');
      }
    });

  });

}());
```
:::

## 翻譯機
 - [x] 01英文朗讀機
 - [x] 02多功能語言學習機
### 01英文朗讀機
:::info
[翻譯機](https://webbit.webduino.io/blockly/#ayxAAVbVngJRM)
![](https://i.imgur.com/7UTtTFT.png)
``` javascript
(async function() {

  var _E8_8B_B1_E6_96_87;


  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/1VTPhpBTHlTxOgnzLx2glQrCb36us2c7h4bCyD2LXMjQ/edit?usp=sharing', '翻譯機');
    $demoMonster01.talk('按A說英文，按B翻譯成英文');
    btnEvent_(_board_._bit_btnA_, 'pressed',
      async function() {
        $demoMonster01.talk('請輸入英文字！');
        await textInput();
        $demoMonster01.talk((thisInputVal));
        await speakAsync((thisInputVal), ["en-US", 1, 1]);
        await delay(1, true); //delay
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnB_, 'pressed',
      async function() {
        $demoMonster01.talk('請輸入中文字?');
        await textInput();
        await sheetWriteData('auto', (thisInputVal), 'a2');
        await sheetReadData();
        _E8_8B_B1_E6_96_87 = _mySheet_.data.cell['b2'];
        $demoMonster03.talk(_E8_8B_B1_E6_96_87);
        await speakAsync(_E8_8B_B1_E6_96_87, ["en-US", 1, 1]);
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::
### 02多功能語言學習機
::: success
[翻譯機](https://webbit.webduino.io/blockly/#7RprrGxXm1M3r)
![](https://i.imgur.com/lEFwB9n.png)
``` 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);
  }


  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/1VTPhpBTHlTxOgnzLx2glQrCb36us2c7h4bCyD2LXMjQ/edit?usp=sharing', '初級600');
    await sheetReadData();
    btnsEvent_('pressed',
      async function() {
        _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]);
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnA_, 'pressed',
      async function() {
        await speakAsync(_E8_8B_B1_E6_96_87, ["en-US", 1, 1]);
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnB_, 'pressed',
      async function() {
        $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');
        }
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
:::

## 法蘭斯自訂積木
 - [x] 法蘭斯自訂積木
::: info
![](https://i.imgur.com/2TjonvA.png)
![](https://i.imgur.com/g8oeDFm.png)
:::































