---
# System prepended metadata

title: WebBit物聯網程式

---

###### tag:`110北中筆記` 
# WebBit物聯網程式
### 20507陳柏智

## 一、 Webduino Blockly 程式設記

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

### 1. 抽籤並朗讀姓名


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

![01抽籤並朗讀姓名](https://i.imgur.com/mvYE9WB.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 = ['andy', 'candy', 'cindy', '蛇丸', 'happy'];
result = (lists_random_item(name2, false));
document.getElementById('demo-area-01-show').innerHTML = result;
speak((['恭喜',result,'得獎'].join('')));
```

### 2. 大樂透自動選號


[02大樂透自動選號](https://blocklypro.webduino.io/#XbLnJBMO8g)

![02大樂透自動選號](https://i.imgur.com/77wLb7g.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;
}
document.getElementById('demo-area-01-show').innerHTML = result;
document.getElementById('demo-area-01-show').style.fontSize = 20 + 'px';
speak((['本期大樂透電腦選號為：',result,'，祝您中大獎'].join('')));
```

### 3.隨機組合朗讀語句


[03大樂透自動選號](https://blocklypro.webduino.io/#kwErAwLoEK)

![03大樂透自動選號](https://i.imgur.com/ZkKD8qr.png)

```javascript=var arr1;
var arr2;
var arr3;
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);
}


arr1 = ['你好，', '哈囉，', '大家好，'];
arr2 = ['我是阿偉，', '我叫阿杰，', '我是外星人，'];
arr3 = ['很勇嘛，讓我看看', '機累轟轟呼呼诶蝦會', '要不要信教'];
result = [(arr1[((math_random_int(1, 3)) - 1)]),(arr2[((math_random_int(1, 3)) - 1)]),(arr3[((math_random_int(1, 3)) - 1)])].join('');
document.getElementById('demo-area-01-show').innerHTML = result;
document.getElementById('demo-area-01-show').style.fontSize = 20 + 'px';
speak(result);
```


### 4.小時鐘


[04小時鐘](https://blocklypro.webduino.io/#R9djM8D1BK)

![04小時鐘](https://i.imgur.com/5qJdgpv.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) {
  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.getElementById('demo-area-01-show').innerHTML = time;
  await delay(1);
}

}());
```


### 5.語音報時


[05語音報時](https://blocklypro.webduino.io/#Xqv51eN9QL)

![05語音報時](https://i.imgur.com/kHNTiTx.png)

```javascript=(async function () {

var h;
var m;
var text;
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 (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();
}

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();
    });
  }
}


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;
  }
  speak((['現在時間是',text,h,'點',m,'分',s,'秒'].join('')));
});

}());
```



## 二、 WebBit 開發板基本操作

- [x] 1. 心跳撲通通
- [x] 2. 看誰按得快(按鈕開關)
- [x] 3. 流動的沙子(九軸感應器)
- [x] 4. 隨機顯示數字
- [x] 5. 溫度計
- [x] 6. 搖骰子
- [x] 7. 指北針
- [x] 8. 計步器
- [x] 9. 播放音樂(&音效語音)
- [x] 加碼演出：拳力出擊
- [x] Line 作業 1~回傳表情圖案
- [x] Line 作業 2~回傳氣象資訊
- [x] 作業1：怪獸亂跑亂轉
- [x] 作業2：鍵盤控制怪獸移動
- [x] 作業3：顯示倒數5秒
- [x] 作業4：語音客服
- [x] 作業5：抽籤選學生
- [x] 作業6：空氣品質即時資訊
- [x] 作業7：偵測亮度及溫度
- [x] 作業8：計步器

### 1. 心跳撲通通

[01心跳撲通通](https://webbit.webduino.io/blockly/#GqNzE7DklVGqg)

![01心跳撲通通](https://i.imgur.com/JSvHHYp.png)

```javascript=(async function() {

  boardReady({
    board: 'Bit',
    url: '127.0.0.1:8080',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 100, 0);
    let $nernp569 = _startLoop_();
    while (_loop_[$nernp569]) {
      _board_._bit_matrix_.setColor("00000000010000000200000003000000040000000500000006ff00000700000008ff0000090000000a0000000bff00000cff00000dff00000e0000000f0000001000000011ff000012000000130000001400000015000000160000001700000018000000");
      await delay(0.5, $nernp569);
      _board_._bit_matrix_.setColor("0000000001ff00000200000003ff00000400000005ff000006ff000007ff000008ff000009ff00000aff00000bff00000cff00000dff00000eff00000f00000010ff000011ff000012ff000013000000140000001500000016ff00001700000018000000");
      await delay(0.5, $nernp569);
      await delay(0.005, true);
    }
  });
  
}());
```


### 2. 跑馬燈(倒數321Go!)

[02跑馬燈](https://webbit.webduino.io/blockly/#Qq700ZVNjGz3B)

![02跑馬燈](https://i.imgur.com/3HjMuiQ.png)

```javascript=(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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('#ff0000');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnB_, 'pressed',
      async function() {
        _board_._bit_matrix_.setColor('#44ccff');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnsEvent_('pressed',
      async function() {
        _board_._bit_matrix_.setColor('#ffcc44');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
### 3. 流動的沙子(九軸感應器)

[03流動的沙子](https://webbit.webduino.io/blockly/#wqaWXQAYPM9yY)

![03流動的沙子](https://i.imgur.com/p3wzmrJ.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_.faceBack(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000000111010001', '#4466ff'));
    });
    mpu9250Fn_.rowForward(_board_, async function() {
      _board_._bit_matrix_.setColor("00ccffff01ccffff02ccffff03ccffff04ccffff05ccffff06ccffff07ccffff08ccffff09ccffff0a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000");
    });
    mpu9250Fn_.rowBack(_board_, async function() {
      _board_._bit_matrix_.setColor("000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000fccffff10ccffff11ccffff12ccffff13ccffff14ccffff15ccffff16ccffff17ccffff18ccffff");
    });
    mpu9250Fn_.pitchLeft(_board_, async function() {
      _board_._bit_matrix_.setColor("00ccffff01ccffff02000000030000000400000005ccffff06ccffff0700000008000000090000000accffff0bccffff0c0000000d0000000e0000000fccffff10ccffff11000000120000001300000014ccffff15ccffff160000001700000018000000");
    });
    mpu9250Fn_.pitchRight(_board_, async function() {
      _board_._bit_matrix_.setColor("00000000010000000200000003ccffff04ccffff05000000060000000700000008ccffff09ccffff0a0000000b0000000c0000000dccffff0eccffff0f000000100000001100000012ccffff13ccffff14000000150000001600000017ccffff18ccffff");
    });

  });

}());
```
### 4. 隨機顯示數字

[04隨機顯示數字](https://webbit.webduino.io/blockly/#VRmXXVa0Ljjy2)

![04隨機顯示數字](https://i.imgur.com/0EquX65.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    btnEvent_(_board_._bit_btnA_, 'pressed',
      async function() {
        _board_._bit_matrix_.setCharacter(((math_random_int(1, 9))).toString(), '#66ffff');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnEvent_(_board_._bit_btnB_, 'pressed',
      async function() {
        _board_._bit_matrix_.setColor(matrixEmoji_(emojiList_[Math.floor(Math.random() * 60)], '#ffcccc'));
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```

### 5. 溫度計

[05溫度計](https://webbit.webduino.io/blockly/#jqwKK0eMDvVqO)

![05溫度計](https://i.imgur.com/m8uT5rW.png)

```javascript=(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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(), '#ccffff', 2);

  });

}());
```

### 6. 搖骰子

[06搖骰子](https://webbit.webduino.io/blockly/#6RLrrPx9O5z3V)

![06搖骰子](https://i.imgur.com/1ksMLH2.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: 'bitd0038',
    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', '#000000'));
      }
      if (_E8_AE_8A_E6_95_B8 == 2) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000010000000', '#ff6666'));
      }
      if (_E8_AE_8A_E6_95_B8 == 3) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000000100000000101000000', '#ffcc99'));
      }
      if (_E8_AE_8A_E6_95_B8 == 4) {
        _board_._bit_matrix_.setColor(matrixEmoji_('0000001010000000101000000', '#ccccff'));
      }
      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', '#99ffff'));
      }
    });

  });

}());
```

### 7. 指北針

[07指北針](https://webbit.webduino.io/blockly/#Mq011OvaoJk30)

![07指北針](https://i.imgur.com/ZJrEyRj.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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', '#ffcccc'));
    });
    mpu9250Fn_.faceSouth(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010000100101010111000100', '#ccffff'));
    });
    mpu9250Fn_.faceWest(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010000010111110001000100', '#99ff99'));
    });
    mpu9250Fn_.faceEast(_board_, async function() {
      _board_._bit_matrix_.setColor(matrixEmoji_('0010001000111110100000100', '#ffff99'));
    });
    let $vrefg289 = _startLoop_();
    while (_loop_[$vrefg289]) {
      $demoMonster01.talk(_board_._bit_mpu9250_val_.azi);
      await delay(0.005, true);
    }


  });

}());
```
### 8. 計步器

[08計步器](https://webbit.webduino.io/blockly/#kRvQQL0jL04yQ)

![08計步器](https://i.imgur.com/hpfd5Mj.png)

```javascript=(async function() {

  var _E8_A8_88_E6_95_B8;


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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, '#ccffff');
    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), '#ccccff');
    });
    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, '#66ffff');
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });
```

### 9. 播放音樂(&音效語音)

[09播放音樂](https://webbit.webduino.io/blockly/#Jyg99abbnN1q4)

![09播放音樂](https://i.imgur.com/9eBD7JL.png)

```javascript=
(async function() {

  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() {
        buzzerPlayEvent_(_board_, 'stop');
        await delay(0.1, true);
        await buzzerPlay_(_board_, [('G5')], [(2)]);
        await buzzerPlay_(_board_, [('A5')], [(2)]);
        await buzzerPlay_(_board_, [('C6')], [(2)]);
        await buzzerPlay_(_board_, [('A5')], [(2)]);
        await buzzerPlay_(_board_, [('E6')], [(2)]);
        await buzzerPlay_(_board_, [('E6')], [(2)]);
        await buzzerPlay_(_board_, [('D5')], [(2)]);
        await buzzerPlay_(_board_, [('G5')], [(2)]);
        await buzzerPlay_(_board_, [('A5')], [(2)]);
        await buzzerPlay_(_board_, [('C6')], [(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_, ["C5", "C5", "G4", "G4", "A4", "A4", "G4", "0", "E4", "G4", "C5", "A4", "G4", "0", "0", "A4", "0", "G4", "0", "E4", "A4", "G4", "0", "E4", "0", "G4", "0", "E4", "D4", "C4", "0", "E4", "E4", "G4", "G4", "A4", "A4", "G4", "G4", "0", "D5", "0", "C5", "A4", "G4", "A4", "C5", "G4", "0", "A4", "A4", "G4", "A4", "C5", "G4", "0", "A4", "A4", "G4", "A4", "D5", "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"]);
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnsEvent_('pressed',
      async function() {
        buzzerPlayEvent_(_board_, 'stop');
        await delay(0.1, true);
        await speakAsync('Never gonna give you up never gonna let you down', ["en-US", 0.1, 0.7]);
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```

### 加碼演出：拳力出擊

[拳力出擊](https://webbit.webduino.io/blockly/#8qMzzn2MAxN3W)

![拳力出擊](https://i.imgur.com/0DMBajJ.png)


```javascript=
(async function() {

  var x;
  var arr;
  var num;
  var temp;
  var max;
  var avg;

  function math_mean(myList) {
    return myList.reduce(function(x, y) {
      return x + y;
    }) / myList.length;
  }

  function power(x) {
    if ((arr.length) <= 10) {
      arr.unshift(x);
    } else {
      temp = (arr.pop());
      arr.unshift(x);
    }
    max = Math.round((Math.max.apply(null, arr)));
    avg = Math.round((math_mean(arr)));
    if (max <= avg + 100) {
      _board_._bit_matrix_.off();
    } else if (max > avg + 100 && max <= avg + 200) {
      _board_._bit_matrix_.setColor("000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001444ccff1544ccff1644ccff1744ccff1844ccff");
    } else if (max > avg + 200 && max <= avg + 300) {
      _board_._bit_matrix_.setColor("000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f44ff441044ff441144ff441244ff441443ff441444ccff1544ccff1644ccff1744ccff1844ccff");
    } else if (max > avg + 300 && max <= avg + 400) {
      _board_._bit_matrix_.setColor("000000000100000002000000030000000400000005000000060000000700000008000000090000000affcc000bffcc000cffcc000dffcc000effcc000f44ff441044ff441144ff441244ff441443ff441444ccff1544ccff1644ccff1744ccff1844ccff");
    } else if (max > avg + 400 && max <= avg + 500) {
      _board_._bit_matrix_.setColor("000000000100000002000000030000000400000005ff660006ff660007ff660008ff660009ff66000affcc000bffcc000cffcc000dffcc000effcc000f44ff441044ff441144ff441244ff441443ff441444ccff1544ccff1644ccff1744ccff1844ccff");
    } else {
      _board_._bit_matrix_.setColor("00ff000001ff000002ff000003ff000004ff000005ff660006ff660007ff660008ff660009ff66000affcc000bffcc000cffcc000dffcc000effcc000f44ff441044ff441144ff441244ff441443ff441444ccff1544ccff1644ccff1744ccff1844ccff");
    }
  }


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    await detectInit_(_board_, {
      "acc": true
    });
    arr = [];
    num = 0;
    $demoMonster02.talk('ok');
    (async function() {
      let $dflug589 = _startLoop_();
      while (_loop_[$dflug589]) {
        await power(_board_._bit_mpu9250_val_.accX * 1);
        $demoMonster01.talk(_board_._bit_mpu9250_val_.accX);
        await delay(0.1, $dflug589);
        await delay(0.005, true);
      }
    })();


  });

}());
```
### 1.接收LINE 訊息,並回傳表情圖案

[01接收LINE 訊息,並回傳表情圖案](https://webbit.webduino.io/blockly/#XROzzd4PWwByn)

![01接收LINE 訊息,並回傳表情圖案](https://i.imgur.com/W8LCVMl.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let _chatChannel = line_channel('81906588');
    _chatChannel.on("value", async function(_e) {
      let _msg = "";
      if (_e.val()) {
        _msg = _e.val().msg;
      }
      if (_msg) {
        $demoMonster01.talk(_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);
        }
        if (_msg == '6') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 6,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '7') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 7,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '8') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 8,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '9') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 9,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '10') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 10,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '11') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 11,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '12') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 12,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '13') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 13,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '14') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 14,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '15') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 15,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '16') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 16,
            type: "sticker"
          }, _e.val().rt);
        }
        if (_msg == '17') {
          await line_reply(_e.val().uid, {
            message: " ",
            stickerPackageId: 1,
            stickerId: 17,
            type: "sticker"
          }, _e.val().rt);
        }
      }
    });

  });

}());
```

### 2.Line回報氣象資訊

[02Line回報氣象資訊](https://webbit.webduino.io/blockly/#myJ110DEVwm3d)

![02Line回報氣象資訊](https://i.imgur.com/QShxi4V.png)

```javascript=
(async function() {

  var AQI;
  var now;
  var forecast;
  var radar;


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

  });

}());
```
### 作業1：怪獸亂跑亂轉

[01怪獸亂跑亂轉](https://webbit.webduino.io/blockly/#Z3koodkpn4Y3v
)

![01怪獸亂跑亂轉](https://i.imgur.com/erMLuiZ.png)

```javascript=
(async function() {

  let $gysow986 = _startLoop_();
  while (_loop_[$gysow986]) {
    $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();
    $demoMonster02.rebound();
    $demoMonster03.rebound();
    $demoMonster04.rebound();
    await delay(0.1, $gysow986);
    await delay(0.005, true);
  }

}());
```

### 作業2:鍵盤控制怪獸移動

[02鍵盤控制怪獸移動](https://webbit.webduino.io/blockly/#Z3kooYal1mK3v)

![02鍵盤控制怪獸移動](https://i.imgur.com/ErscuRF.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 $gijmw472 = _startLoop_();
  while (_loop_[$gijmw472]) {
    $demoMonster01.rebound();
    await delay(0.005, true);
  }

}());
```

### 作業3:顯示倒數5秒

[03顯示倒數5秒](https://webbit.webduino.io/blockly/#6yYGGDMZlDbqw
)

![03顯示倒數5秒](https://i.imgur.com/hZUpQII.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 $aqxlt654 = _startLoop_();
        for (var i = 5; i >= 1; i -= 1) {
          if (!_loop_[$aqxlt654]) {
            break;
          }
          _board_._bit_matrix_.setCharacter(i, '#ccffff');
          await delay(1, $aqxlt654);
          await delay(0.001, true);
        }

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

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

}());
```
### 作業4:語音客服

[04語音客服](https://webbit.webduino.io/blockly/#ayxAA2dkPgNRM
)

![04語音客服](https://i.imgur.com/GfQwNrh.png)

```javascript=
(async function() {

  let $gryah516 = _startLoop_();
  while (_loop_[$gryah516]) {
    $demoMonster01.talk('請問您幾歲？');
    await speakAsync('請問您幾歲？', ["zh-TW", 1, 1]);
    await textInput();
    if ((thisInputVal) >= 7 && (thisInputVal) < 13) {
      $demoMonster01.talk('您可以享受購票8折優惠');
      await speakAsync('您可以享受購票8折優惠', ["zh-TW", 1, 1]);
    } else {
      if ((thisInputVal) < 7 || (thisInputVal) > 60) {
        $demoMonster01.talk('您可以免費入場');
        await speakAsync('您可以免費入場', ["zh-TW", 1, 1]);
      } else {
        $demoMonster01.talk('請購票入場');
        await speakAsync('請購票入場', ["zh-TW", 0.1, 2]);
      }
    }
    await delay(0.005, true);
  }

}());
```
### 作業5:抽籤選學生
[05抽籤選學生](https://webbit.webduino.io/blockly/#jqwKKEom6O7qO)

![05抽籤選學生](https://i.imgur.com/HvJlQOA.png)

```javascript=
(async function() {

  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 = ['阿偉', '杰哥', '彬彬', '美琪', '淑慧阿姨', '阿嬤', '小男孩', '小女孩'];
  $demoMonster01.hover(async function() {
    $demoMonster01.talk((lists_random_item(_E5_90_8D_E5_96_AE, false)));
    _stopAllLoop_();
  }, async function() {
    let $ahyik167 = _startLoop_();
    while (_loop_[$ahyik167]) {
      $demoMonster01.talk((lists_random_item(_E5_90_8D_E5_96_AE, false)));
      await delay(1, $ahyik167);
      await delay(0.005, true);
    }

  });

}());
```
### 作業6:空氣品質即時資訊

[06空氣品質即時資訊](https://webbit.webduino.io/blockly/#7qZrrDEbeGDqO
)

![06空氣品質即時資訊](https://i.imgur.com/lXvgeY6.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 $bsnpc971 = _startLoop_();
    while (_loop_[$bsnpc971]) {
      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('#ff9900');
        $demoMonster01.talk('對過敏族群不健康');
      } else if (AQI <= 200) {
        _board_._bit_matrix_.setColor('#ff0000');
        $demoMonster01.talk('對所有族群不健康');
      } else if (AQI <= 300) {
        _board_._bit_matrix_.setColor('#cc44cc');
        $demoMonster01.talk('非常不健康');
      } else {
        _board_._bit_matrix_.setColor('#664466');
        $demoMonster01.talk('危害');
      }
      await delay(300, $bsnpc971);
      await delay(0.005, true);
    }


  });

}());
```
### 作業7:偵測亮度及溫度

[07偵測亮度及溫度](https://webbit.webduino.io/blockly/#XROzz8PzLebyn
)

![07偵測亮度及溫度](https://i.imgur.com/84Z4Gvr.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 $oymgj686 = _startLoop_();
    while (_loop_[$oymgj686]) {
      _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), '℃'].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:計步器

[08計步器](https://webbit.webduino.io/blockly/#ayxAA2K4XBARM
)

![08計步器](https://i.imgur.com/ZRvmgZP.png)

```javascript=
(async function() {

  var _E8_A8_88_E6_95_B8;


  boardReady({
    board: 'Bit',
    url: '127.0.0.1:8080',
    multi: true
  }, 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, '#ccffff');
    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), '#ccccff');
    });
    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, '#66ffff');
      }, [_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

### 01 線上控制測試Bit

[01線上控制測試Bit](https://fustyles.github.io/webduino/MyFirmata_bit.html?bitd0038)

![01線上控制測試Bit](https://i.imgur.com/BzshFkz.png)
### 02 跑馬燈(倒數321Go!)

[02跑馬燈](https://webbit.webduino.io/blockly/#Mq011vv55lD30
)

![02跑馬燈](https://i.imgur.com/jow3Xrv.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    _board_._bit_matrix_.setCharacter('5', '#66ff99');
    await delay(1, true); //delay
    _board_._bit_matrix_.setCharacter('4', '#ffff44');
    await delay(1, true); //delay
    _board_._bit_matrix_.setCharacter('3', '#ffff66');
    await delay(1, true); //delay
    _board_._bit_matrix_.setCharacter('2', '#ff9966');
    await delay(1, true); //delay
    _board_._bit_matrix_.setCharacter('1', '#ff6666');
    await delay(1, true); //delay
    await _board_._bit_matrix_.setStringOnce('GOGOGOGOGO', '#99ffff', 2);

  });

}());
```
### 03 燈號報數

[03燈號報數](https://webbit.webduino.io/blockly/#YqKEEee1zoLy4
)

![03燈號報數](https://i.imgur.com/NEcG4e7.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $lcnln314 = _startLoop_();
    while (_loop_[$lcnln314]) {
      let $dyhoc12 = _startLoop_();
      for (var i = 1; i <= 25; i += 1) {
        if (!_loop_[$dyhoc12]) {
          break;
        }
        _board_._bit_matrix_.setColor((i - 1), (colour_random()));
        await delay(0.5, $dyhoc12);
        await delay(0.001, true);
      }

      await delay(0.005, true);
    }


  });

}());
```
### 04 剪刀石頭布

[04剪刀石頭布](https://webbit.webduino.io/blockly/#VRmXXPPdKEoy2
)

![04剪刀石頭布](https://i.imgur.com/dTrN1Yc.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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_('1000101010001001101111011', '#99ffff'));
      }, [_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', '#660000'));
      }, [_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', '#99ff99'));
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
    btnsEvent_('released',
      async function() {
        _board_._bit_matrix_.off();
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
### 05 環能控光燈

[05環能控光燈](https://webbit.webduino.io/blockly/#ARzvvOokENVR2
)

![05環能控光燈](https://i.imgur.com/blLvySz.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    await detectInit_(_board_, {
      "left": true
    });
    let $aaujf332 = _startLoop_();
    while (_loop_[$aaujf332]) {
      $demoMonster01.talk((_board_._bit_detected_val_.left));
      if ((_board_._bit_detected_val_.left) < 100) {
        _board_._bit_matrix_.setColor('#ffffff');
        _board_._bit_matrix_.brightness(10);
      } 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 電子鬧鐘
[06電子鬧鐘](https://webbit.webduino.io/blockly/#V3EmmA8jX2B3l
)

![06電子鬧鐘](https://i.imgur.com/SPHOcCq.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    $demoMonster01.reset();
    $demoMonster02.reset();
    $demoMonster03.reset();
    $demoMonster04.reset();
    let $xfsqj643 = _startLoop_();
    while (_loop_[$xfsqj643]) {
      if (get_time("hms") == '10:38:00') {
        $demoMonster01.talk('我來了');
        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"]);
      } else {
        $demoMonster01.talk(([get_date("ymd"), ("<br/>"), get_time("hms")].join('')));
      }
      await delay(1, $xfsqj643);
      await delay(0.005, true);
    }


  });

}());
```
### 07 空氣品質AQI

[07空氣品質AQI](https://webbit.webduino.io/blockly/#py6LLQ6kwe7qZ
)

![07空氣品質AQI](https://i.imgur.com/FBjrV2j.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 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

[01魔幻LED](https://webbit.webduino.io/blockly/#b3Gjjl71wGjqA)

![01魔幻LED](https://i.imgur.com/DNtYewX.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',
    url: '127.0.0.1:8080',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 100, 0);
    let $nyxwc485 = _startLoop_();
    while (_loop_[$nyxwc485]) {
      color = colour_random();
      let $qysvt356 = _startLoop_();
      for (var i = 1; i <= 8; i += 1) {
        if (!_loop_[$qysvt356]) {
          break;
        }
        WS2812.init(board).setColor(i - 1, color);
        await delay(0.001, true);
      }

      await delay(0.5, $nyxwc485);
      await delay(0.005, true);
    }


  });

}());
```
### 2：測試前進、後退、停止
[02測試前進、後退、停止](https://webbit.webduino.io/blockly/#Qq700JwoYAZ3B
)


![02測試前進、後退、停止](https://i.imgur.com/91JxRpI.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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：紅外線遙控

[03紅外線遙控](https://webbit.webduino.io/blockly/#wRBvvJd56wXRM)

![03紅外線遙控](https://i.imgur.com/IlPrTaD.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    url: '127.0.0.1:8080',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 100, 0);
    IrRecv.init(board, 1).receive(async value => {
      $demoMonster01.talk((value || ''));
      if ((value || '') == '00ff629d' || (value || '') == '3d9ae3f7') {
        CarTracker.init(board).action(1);
        CarTracker.init(board).setAllSpeed(55);
      } else if ((value || '') == '00ffa857' || (value || '') == '1bc0157b') {
        CarTracker.init(board).action(4);
        CarTracker.init(board).setAllSpeed(55);
      } else if ((value || '') == '00ffc23d' || (value || '') == '8c22657b') {
        CarTracker.init(board).action(8);
        CarTracker.init(board).setAllSpeed(55);
      } else if ((value || '') == '00ff22dd' || (value || '') == '449e79f') {
        CarTracker.init(board).action(7);
        CarTracker.init(board).setAllSpeed(55);
      } else {
        CarTracker.init(board).setAllSpeed(55);
        CarTracker.init(board).action(0);
      }
    }, () => {});

  });

}());
```
### 4：利用另一塊板子遙控

[04利用另一塊板子遙控](https://webbit.webduino.io/blockly/#rybbbYMrGrnyk)

![04利用另一塊板子遙控](https://i.imgur.com/REBf2XV.png)

```javascript=
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    let _board_ = await boardInit_(board, 250, 2);

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

    await webduinoBroadcastor.onMessage('car', 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);
      }
    });

  });

  boardReady({
    board: 'Bit',
    device: 'bit50735',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    let _board_ = await boardInit_(board, 250, 2);
    btnEvent_(_board_._bit_btnA_, 'pressed',
      async function() {

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

        webduinoBroadcastor.send({
          topic: 'car',
          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: 'car',
          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: 'car',
          message: ('c').toString()
        });
      }, [_board_._bit_btnA_, _board_._bit_btnB_]
    );
  });

}());
```
### 5：手機LINE控制

[05手機LINE控制](https://webbit.webduino.io/blockly/#VRmXXxa48Wvy2)

![05手機LINE控制](https://i.imgur.com/AuukGbI.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let _chatChannel = line_channel('81906588');
    _chatChannel.on("value", async function(_e) {
      let _msg = "";
      if (_e.val()) {
        _msg = _e.val().msg;
      }
      if (_msg) {
        if (_msg == '8') {
          CarTracker.init(board).action(1);
          await line_reply(_e.val().uid, '前進', _e.val().rt);
        }
        if (_msg == '2') {
          CarTracker.init(board).action(4);
          await line_reply(_e.val().uid, '後退', _e.val().rt);
        }
        if (_msg == '4') {
          CarTracker.init(board).action(2);
          await line_reply(_e.val().uid, '原地左轉', _e.val().rt);
        }
        if (_msg == '6') {
          CarTracker.init(board).action(3);
          await line_reply(_e.val().uid, '原地右轉', _e.val().rt);
        }
        if (_msg == '5') {
          CarTracker.init(board).action(0);
          await line_reply(_e.val().uid, '停止', _e.val().rt);
        }
      }
    });

  });

}());
```
### 6：顏色偵測

[06顏色偵測](https://webbit.webduino.io/blockly/#6RLrr1vAzob3V)

![06顏色偵測](https://i.imgur.com/IXMDdXV.png)

```javascript=
(async function() {

  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $qrate580 = _startLoop_();
    while (_loop_[$qrate580]) {
      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] == 4)) {
        _board_._bit_matrix_.setCharacter('R', '#ff0000');
      } else if ((TCS34725.init(board)._id[0] == 5)) {
        _board_._bit_matrix_.setCharacter('P', '#6644ff');
      } else if ((TCS34725.init(board)._id[0] == 6)) {
        _board_._bit_matrix_.setCharacter('Y', '#ffff00');
      } else {
        _board_._bit_matrix_.off();
      }
      await delay(0.005, true);
    }


  });

}());
```
#### 6╴1 顏色偵測簡單應用

[6╴1顏色偵測簡單應用](https://webbit.webduino.io/blockly/#GqNzzNbM5mQqg)

![6╴1顏色偵測簡單應用](https://i.imgur.com/YdVlOyh.png)

```javascript=
(async function() {

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


  });

}());
```
### 7：避障功能

* 1. 避障規則一

    [7╴1避障規則一](https://webbit.webduino.io/blockly/#JyeOOYMX7pZqV)

    ![7╴1避障規則一](https://i.imgur.com/4ZhHWKI.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $vurjs297 = _startLoop_();
    while (_loop_[$vurjs297]) {
      if (((await Ultrasonic.init(board)).distance || 0) >= 20) {
        CarTracker.init(board).action(1);
      } else {
        _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), $vurjs297);
      await delay(0.005, true);
    }


    });

    }());
    ```
* 2. 避障規則二

    [7╴2避障規則二](https://webbit.webduino.io/blockly/#ARzvvAb2wVjR2)
    
    ![7╴2避障規則二](https://i.imgur.com/ZA82gam.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $gswkk811 = _startLoop_();
    while (_loop_[$gswkk811]) {
      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), $gswkk811);
        _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), $gswkk811);
      }
      await delay(0.005, true);
    }


    });

    }());
    ```
### 8：循跡自走

[08循跡自走](https://webbit.webduino.io/blockly/#N3266lKZDBQ39)

![08循跡自走](https://i.imgur.com/iHgCQOc.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    let $gqvvh171 = _startLoop_();
    while (_loop_[$gqvvh171]) {
      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), $gqvvh171);
        _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), $gqvvh171);
      }
      await delay(0.005, true);
    }


  });

}());
```
## 五、Web:Bit 智慧插座 Plus

- [x] 1.按鈕控制
- [x] 2.揮手控制
- [x] 3.搖晃控制
- [x] 4.語音控制
- [x] 5.翻蓋控制
- [x] 7.App Inventor
- [x] 8.LINE 控制
- [x] 9.Google Sheet
- [x] 10.插座回傳 Web:Bit

### 1：按鈕控制

[01按鈕控制](https://webbit.webduino.io/blockly/#Jy1aaOpZW8dyl)

![01按鈕控制](https://i.imgur.com/dFsuUAy.png)


```javascript=
(async function() {

  var webduinoBroadcastor;


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

}());
```
### 2.：揮手控制
[02揮手控制](https://webbit.webduino.io/blockly/#Mq0115nl0b730)

![02揮手控制](https://i.imgur.com/KqGe7UT.png)

```javascript=
(async function() {

  var _E8_AE_8A_E6_95_B8;
  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    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: 'wg1006/set',
      message: ('OFF').toString()
    });
    let $vsiaf140 = _startLoop_();
    while (_loop_[$vsiaf140]) {
      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: 'wg1006/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: 'wg1006/set',
          message: ('OFF').toString()
        });
        _board_._bit_matrix_.setColor('#000000');
      }
      await delay(0.005, true);
    }


  });

}());
```
### 3：搖晃控制

[03搖晃控制](https://webbit.webduino.io/blockly/#VRmXXE8Bj8Qy2
)

![03搖晃控制](https://i.imgur.com/7QVaFw5.png)

```javascript=
(async function() {

  var webduinoBroadcastor;
  var _E8_AE_8A_E6_95_B8;


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


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

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

  });

}());
```
### 4：語音控制

[04語音控制](https://webbit.webduino.io/blockly/#Jyg99EKdb5mq4)

![04語音控制](https://i.imgur.com/32F9SEF.png)

```javascript=
(async function() {

  var webduinoBroadcastor;


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


  });

}());
```
### 5：翻蓋控制

[05翻蓋控制](https://webbit.webduino.io/blockly/#XROzzY4kNDayn
)

![05翻蓋控制](https://i.imgur.com/KRNyVly.png)

```javascript=
(async function() {

  var webduinoBroadcastor;


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

  });

}());
```

### 7：App Inventor

[07App Inventor](https://webbit.webduino.io/blockly/#8RPzzG2Q8gw3b)

![07App Inventor](https://i.imgur.com/MnT0yHU.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: 'wg1006/set',
      message: ('ON').toString()
    });
    $demoMonster03.talk('開');
    await delay(1, true); //delay
    $demoMonster03.talk('');
  });
  $demoMonster01.click(async function() {


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

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

}());
```
### 8：LINE 控制

[08LINE控制](https://webbit.webduino.io/blockly/#a35VV6a85NXym)

![08LINE控制](https://i.imgur.com/eSL8bcd.png)

```javascript=
(async function() {

  var webduinoBroadcastor;


  let _chatChannel = line_channel('81906588');
  _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: 'wg1006/set',
          message: ('ON').toString()
        });
      } else if (_msg == '關燈') {


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

        webduinoBroadcastor.send({
          topic: 'wg1006/set',
          message: ('OFF').toString()
        });
      }
    }
  });

}());
```
### 9：Google Sheet

[Google Sheet](https://webbit.webduino.io/blockly/#VRmXMA8dlNDy2)

![Google Sheet](https://i.imgur.com/dTRWbsZ.png)


```javascript=
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    _board_._bit_matrix_.setColor('#ff0000');
    sheetInit('https://docs.google.com/spreadsheets/d/18qnwla7MY7Ifg4NkcYwuwbwEgu6F6dXaMGTia956OcA/edit?usp=sharing', '工作表1');
    let $mscmb259 = _startLoop_();
    while (_loop_[$mscmb259]) {
      await sheetReadData();
      if (_mySheet_.data.cell['a1'] == '開燈') {


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

        webduinoBroadcastor.send({
          topic: 'wg1006/set',
          message: ('ON').toString()
        });
      }
      if (_mySheet_.data.cell['a2'] == '關燈') {


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

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


  });

}());
```

### 10：插座回傳 Web:Bit

[10插座回傳至Web:Bit](https://webbit.webduino.io/blockly/#ayxAAbV9AkJRM)

![10插座回傳至Web:Bit](https://i.imgur.com/6WWUpJB.png)

```javascript=
(async function() {

  var webduinoBroadcastor;


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    _board_._bit_matrix_.setColor('#000000');

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

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

  });

}());
```

## 六、Web:Bit 創意設計

- [x] 1.英文朗讀機
- [x] 2.多功能語言學習機

### 1. 英文朗讀機


[01英文朗讀機](https://webbit.webduino.io/blockly/#Mq011LnEOpv30)

![01英文朗讀機](https://i.imgur.com/C7I4jH4.png)

```javascript=
(async function() {

  var _E8_8B_B1_E6_96_87;


  boardReady({
    board: 'Bit',
    device: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    $demoMonster01.reset();
    $demoMonster02.reset();
    $demoMonster03.reset();
    $demoMonster04.reset();
    sheetInit('https://docs.google.com/spreadsheets/d/1HTMVHajkAWZhZvnpCYn7kG1PI2IlUXpE3qIm77EYpjg/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), 'a3');
        await sheetReadData();
        _E8_8B_B1_E6_96_87 = _mySheet_.data.cell['b3'];
        $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_]
    );
  });

}());
```

### 2.多功能語言學習機

[02多功能語言學習機/初級600](https://webbit.webduino.io/blockly/#8qMzzr2kmrD3W)

![02多功能語言學習機/初級600](https://i.imgur.com/8tgT5jU.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: 'bitd0038',
    transport: 'mqtt',
    multi: true
  }, async function(board) {
    window._board_ = await boardInit_(board, 250, 0);
    $demoMonster01.reset();
    $demoMonster02.reset();
    $demoMonster03.reset();
    $demoMonster04.reset();
    sheetInit('https://docs.google.com/spreadsheets/d/1HTMVHajkAWZhZvnpCYn7kG1PI2IlUXpE3qIm77EYpjg/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(_E4_B8_AD_E6_96_87);
        $demoMonster02.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_]
    );
    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('預備，開始唸');
        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] 安裝擴充積木

![安裝擴充積木](https://i.imgur.com/JRPLSBq.png)

## 八、期末專題

- [x] 題目
- [x] 簡介
- [x] 程式碼擷圖
- [x] 作品成果

* 題目：改善個人言論行為品質機

* 簡介：在一些特定場所或活動，會要求要保持個人的言論行為，然而無法及時紀錄。因此透過「改善個人言論行為品質機」，可以偵測每一個人的言論，讓使用者方便紀錄與掌控。使用者可事先在Google試算表設定仇恨言論。若當事者的言論中包含仇恨言論時，Web:Bit板子會閃爍燈光，緊接著電腦會發出音效，螢幕會跳出提醒，最後該時間會被記錄在Google試算表，方便使用者日後檢視及了解。透過此機器將可規範個人言論，有助於維持及改善特殊場合的風氣及面貌。

* 程式碼擷圖： 
![程式碼擷圖](https://i.imgur.com/Ee8WHt3.png)


* 參考資料：
    * [Web:Bit 教育手冊](https://webbit.webduino.io/tutorials/doc/zh-tw/education/index.html)
    * [Web:Bit 擴充功能：Google 試算表](https://webbit.webduino.io/tutorials/doc/zh-tw/education/extension/google-spreadsheet.html)