Skip to content

Instantly share code, notes, and snippets.

@tazjel
Forked from titangene/index.html
Created January 5, 2018 16:40
Show Gist options
  • Save tazjel/ade5019fc2c26effc92e699f0aa378f7 to your computer and use it in GitHub Desktop.
Save tazjel/ade5019fc2c26effc92e699f0aa378f7 to your computer and use it in GitHub Desktop.
Web × Arduino - Timeout (LED matrix)
<!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.js"></script>
</head>
<body>
<h1 id="demo">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;
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment