Skip to content

Instantly share code, notes, and snippets.

@titangene
Last active January 5, 2018 16:40
Show Gist options
  • Save titangene/867b32b2f52496c6d1b906b037d37da9 to your computer and use it in GitHub Desktop.
Save titangene/867b32b2f52496c6d1b906b037d37da9 to your computer and use it in GitHub Desktop.
Web × Arduino - Timeout (LED matrix, Google Voice)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Webduino Blockly Timeout Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>
<script src="https://blockly.webduino.io/lib/runtime.min.js"></script>
<script src="./timeout_matrix_google voice.js"></script>
</head>
<body>
<h1 id="demo">123</h1>
<h1 id="voice">123</h1>
</body>
var led, button, ultrasonic, buzzer, matrix;
var timer = 0;
var isStartTimer = false;
// Ludwig van Beethoven - Für Elise ( 路德維希‧范‧貝多芬 - 獻給愛麗絲 )
var music = [
{notes:"E4",tempos:"6"},{notes:"DS4",tempos:"6"},{notes:"E4",tempos:"4"},{notes:"DS4",tempos:"4"},
{notes:"E4",tempos:"4"},{notes:"B3",tempos:"4"},{notes:"D4",tempos:"4"},{notes:"C4",tempos:"4"},
{notes:"A3",tempos:"4"},{notes:"0",tempos:"4"},{notes:"C3",tempos:"6"},{notes:"E3",tempos:"6"},
{notes:"A3",tempos:"6"},{notes:"B3",tempos:"4"},{notes:"0",tempos:"4"},{notes:"D3",tempos:"6"},
{notes:"GS3",tempos:"6"},{notes:"B3",tempos:"6"},{notes:"C4",tempos:"4"}
];
// 0, 1, 2, 3, 4, 5, 6 ,7, 8, 9
var matrixStr = ["7e81817e", "8482ff80", "c6a1918e", "66819966", "1f10ff10",
"4f898971", "7e898971", "07c1310f", "7689916e", "8e91917e"];
var timer1, timer10;
var demo = document.getElementById("demo");
var voice = document.getElementById("voice");
boardReady({device: 'wa8w'}, board => {
board.systemReset();
board.samplingInterval = 250;
led = getLed(board, 13);
button = getButton(board, 12);
buzzer = getBuzzer(board, 7);
ultrasonic = getUltrasonic(board, 9, 8);
matrix = getMax7219(board, 2, 3, 4);
matrix.animateStop();
matrix.on("0000000000000000");
speechRecognition();
ultrasonic.ping(cm => {
if (!isStartTimer) {
timer = ultrasonic.distance >= 30 ? 60 : ultrasonic.distance * 2;
demo.innerHTML = timer;
matrix.on(timerFormat());
}
}, 500);
// 因為此用 Google 語音,按鈕開關就變假的 XD
button.on("pressed", () => { });
});
function speechRecognition(){
if (!("webkitSpeechRecognition" in window))
alert("本瀏覽器不支援語音辨識,請更換瀏覽器!(Chrome 25 版以上才支援語音辨識)");
else {
window._recognition = new webkitSpeechRecognition();
window._recognition.continuous = true;
window._recognition.interimResults = true;
window._recognition.lang = "cmn-Hant-TW";
window._recognition.onstart = () => {
window._recognition.status = true;
console.log("Start recognize...");
};
window._recognition.onend = () => {
console.log("Stop recognize");
if(window._recognition.status) window._recognition.start();
};
window._recognition.onresult = (event, result) => {
result = {};
result.resultLength = event.results.length - 1;
result.resultTranscript = event.results[result.resultLength][0].transcript;
if (event.results[result.resultLength].isFinal === false) {
console.log(result.resultTranscript);
voice.innerHTML = result.resultTranscript;
if(result.resultTranscript.indexOf("計時") !== -1) {
isStartTimer = true;
window._recognition.status = false;
window._recognition.stop();
countSecond();
}
} else if (event.results[result.resultLength].isFinal === true)
console.log("final");
};
window._recognition.start();
}
}
function timerFormat() {
timer10 = timer < 10 ? 0 : Math.floor(timer / 10);
timer1 = timer < 10 ? timer : timer - timer10 * 10;
return matrixStr[timer10] + matrixStr[timer1];
}
function countSecond() {
timer -= 1;
demo.innerHTML = timer;
matrix.on(timerFormat());
if (timer <= 0) {
led.on();
// XD
matrix.on("316aaaa4a4aa6a31");
buzzer.play(buzzer_music(music).notes ,buzzer_music(music).tempos);
setTimeout(() => {
led.toggle();
speechRecognition();
isStartTimer = false;
}, 6000);
} else
setTimeout("countSecond()", 1000);
}
function buzzer_music(m) {
var musicNotes = { notes: [], tempos: [] };
if (m[0].notes.length > 1) {
for (var i = 0; i < m.length; i++) {
if (Array.isArray(m[i].notes))
musicNotes.notes = musicNotes.notes.concat(m[i].notes);
else
musicNotes.notes.push(m[i].notes);
if (Array.isArray(m[i].tempos))
musicNotes.tempos = musicNotes.tempos.concat(m[i].tempos);
else
musicNotes.tempos.push(m[i].tempos);
}
} else {
musicNotes.notes = [m[0].notes];
musicNotes.tempos = [m[0].tempos];
}
return musicNotes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment