-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuzzer.html
84 lines (74 loc) · 2.25 KB
/
buzzer.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
<html>
<head>
<script src="bower_components/webduino-js/dist/webduino-all.js"></script>
<h1>Buzzer Example</h1>
</head>
<body>
<button id='btn' class='btn'>Play</button>
<script>
var board, buzzer, led,
Buzzer = webduino.module.Buzzer;
board = new webduino.Arduino({
'transport': 'websocket',
'url': '192.168.43.98'
});
board.on(webduino.BoardEvent.STRING_MESSAGE,
function(event) {
var m = event.message;
console.log("STRING:", m);
});
board.on(webduino.BoardEvent.READY, function() {
console.log("Ready...");
led = new webduino.module.Led(board, board.getDigitalPin(14));
btn.addEventListener('click', function() {
led.blink(100);
buzzer = new Buzzer(board, board.getDigitalPin(12));
buzzer.play(buzzer_music([{
notes: "C7",
tempos: "8"
}, {
notes: "D7",
tempos: "8"
}, {
notes: "E7",
tempos: "8"
}]).notes, buzzer_music([{
notes: "C7",
tempos: "8"
}, {
notes: "D7",
tempos: "8"
}, {
notes: "E7",
tempos: "8"
}]).tempos);
});
});
function buzzer_music(m) {
var musicNotes = {};
musicNotes.notes = [];
musicNotes.tempos = [];
if (m[0].notes.length > 1) {
for (var i = 0; i < m.length; i++) {
if (Array.isArray(m[i].notes)) {
var cn = musicNotes.notes.concat(m[i].notes);
musicNotes.notes = cn;
} else {
musicNotes.notes.push(m[i].notes);
}
if (Array.isArray(m[i].tempos)) {
var ct = musicNotes.tempos.concat(m[i].tempos);
musicNotes.tempos = ct;
} else {
musicNotes.tempos.push(m[i].tempos);
}
}
} else {
musicNotes.notes = [m[0].notes];
musicNotes.tempos = [m[0].tempos];
}
return musicNotes;
}
</script>
</body>
</html>