Skip to content

Instantly share code, notes, and snippets.

@yasugahira0810
Created January 20, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasugahira0810/4d5f97c89c929bcb9c5d65252c4772bf to your computer and use it in GitHub Desktop.
Save yasugahira0810/4d5f97c89c929bcb9c5d65252c4772bf to your computer and use it in GitHub Desktop.
<!-- HTML Example -->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://unpkg.com/obniz@1.9.3/obniz.js" crossorigin="anonymous"></script>
</head>
<body>
<!--div id="obniz-debug"></div-->
<br>
<div style="text-align:center"><button id="Go" class="btn btn-warning" style="width:30%;height:70px;font-size:40px;">前進</button></div>
<br>
<div style="text-align:center"><button id="Left" class="btn btn-warning" style="width:30%;height:70px;font-size:40px;">左旋回</button>&emsp;&emsp;&emsp;
<button id="Right" class="btn btn-warning" style="width:30%;height:70px;font-size:40px;">右旋回</button></div>
<br>
<div style="text-align:center"><button id="Back" class="btn btn-warning" style="width:30%;height:70px;font-size:40px;">後退</button></div>
<br>
<div style="text-align:center;display: none;" id="Alert" class="bg-danger text-white">緊急停止時に押せるのは後退のみです</div>
<script>
var obniz = new Obniz("OBNIZ_ID");
obniz.display.font(null,30); //デフォルトフォント(Arial)の30px
obniz.onconnect = async function () {
var motorL = obniz.wired("DCMotor", {forward:1, back:0});
motorL.power(40);
var motorR = obniz.wired("DCMotor", {forward:3, back:2});
motorR.power(40);
var sensor = obniz.wired("GP2Y0A21YK0F", {vcc:4, gnd:5, signal:6})
var isBack = false; // 緊急停止時にBackだけ許可する為のフラグ
sensor.start(function( distance ){
console.log("distance " + distance + " mm")
if (distance > 100 && isBack == false) {
motorL.stop();
motorR.stop();
obniz.display.print("緊急停止");
obniz.display.clear();
$("#Go").prop("disabled", true);
$("#Left").prop("disabled", true);
$("#Right").prop("disabled", true);
$('#Alert').show();
} else {
$("#Go").prop("disabled", false);
$("#Left").prop("disabled", false);
$("#Right").prop("disabled", false);
$('#Alert').hide();
}
})
$("#Go").on('touchstart mousedown', function(){
motorL.move(true); //ボタンが押されたら動かす
motorR.move(true); //ボタンが押されたら動かす
obniz.display.print("前進");
})
$("#Go").on('touchend mouseup', function(){
motorL.stop(); //ボタンが離されたら止める
motorR.stop(); //ボタンが離されたら止める
obniz.display.clear();
})
$("#Back").on('touchstart mousedown', function(){
motorL.move(false); //ボタンが押されたら動かす
motorR.move(false); //ボタンが押されたら動かす
obniz.display.print("後退");
isBack = true;
})
$("#Back").on('touchend mouseup', function(){
motorL.stop(); //ボタンが離されたら止める
motorR.stop(); //ボタンが離されたら止める
obniz.display.clear();
isBack = false;
})
$("#Left").on('touchstart mousedown', function(){
motorL.move(true); //ボタンが押されたら動かす
motorR.move(false); //ボタンが押されたら動かす
obniz.display.print("左旋回");
})
$("#Left").on('touchend mouseup', function(){
motorL.stop(); //ボタンが離されたら止める
motorR.stop(); //ボタンが離されたら止める
obniz.display.clear();
})
$("#Right").on('touchstart mousedown', function(){
motorL.move(false); //ボタンが押されたら動かす
motorR.move(true); //ボタンが押されたら動かす
obniz.display.print("右旋回");
})
$("#Right").on('touchend mouseup', function(){
motorL.stop(); //ボタンが離されたら止める
motorR.stop(); //ボタンが離されたら止める
obniz.display.clear();
})
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment