GND - 0
+5V - 1
VRx - 2
VRy - 3
SW - 4
ジョイスティックで操作<br />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://unpkg.com/obniz@3.x/obniz.js" crossorigin="anonymous" ></script>
<style>
table {
border-collapse: collapse; /* セルの線を1本にする */
}
.c {
width: 40px;
height: 30px;
border: 2px solid #AAAAAA;
text-align: center;
cellspacing: 0px;
}
</style>
obniz ID:<input type="text" id="oid" style="width: 100px;"><br />
<button onClick="init();">実行</button><br />
<table>
<tr>
<td class="c"></td>
<td class="c"><span id="ym">0</span></td>
<td class="c"></td>
</tr>
<tr>
<td class="c"><span id="xm">0</span></td>
<td class="c">●</td>
<td class="c"><span id="xp">0</span></td>
</tr>
<tr>
<td class="c"></td>
<td class="c"><span id="yp">0</span></td>
<td class="c"></td>
</tr>
</table>
<script>
var obniz;
var joystick;
function init() {
obniz = new Obniz($("#oid").val());
obniz.onconnect = async function () {
joystick = obniz.wired("JoyStick", {gnd:0, sw:4, y:3, x:2, vcc:1});
setInterval(function() {
main();
}, 200);
};
}
async function main() {
var x = Math.round(await joystick.getXWait() * 10);
var y = Math.round(await joystick.getYWait() * 10);
if (x < 0) {
$("#xm").html(x);
$("#xp").html("");
} else if (x > 0) {
$("#xm").html("");
$("#xp").html(x);
} else {
$("#xm").html("");
$("#xp").html("");
}
if (y < 0) {
$("#ym").html(y);
$("#yp").html("");
} else if (y > 0) {
$("#ym").html("");
$("#yp").html(y);
} else {
$("#ym").html("");
$("#yp").html("");
}
}
</script>