-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer-LED_Matrix&Google_Voice.html
145 lines (132 loc) · 5.11 KB
/
Timer-LED_Matrix&Google_Voice.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!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>Timer - LED Matrix & Google Voice</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>
</head>
<body>
<h1 id="demo">123</h1>
<h1 id="voice">123</h1>
</body>
<script>
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;
}
</script>
</body>