-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer-LED_Matrix.html
101 lines (94 loc) · 3.4 KB
/
Timer-LED_Matrix.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
<!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</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>
</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;
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");
ultrasonic.ping(cm => {
if (!isStartTimer) {
timer = ultrasonic.distance >= 30 ? 60 : ultrasonic.distance * 2;
document.getElementById("demo").innerHTML = timer;
matrix.on(timerFormat());
}
}, 500);
button.on("pressed", () => {
isStartTimer = true;
countSecond();
});
});
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;
document.getElementById("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();
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>