<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IoT Controller</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#controller {
display: flex;
}
#crossKey {
position: relative;
width: 50vmin;
height: 50vmin;
}
#controller button {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 15vmin;
height: 15vmin;
font-size: 10vmin;
padding: 0;
cursor: pointer;
}
#controller button:hover {
background-color: #E8F5E9;
}
#controller button[data-motor="flont"] {
margin: 0 auto auto;
}
#controller button[data-motor="back"] {
margin: 30vmin auto auto;
}
#controller button[data-motor="left"] {
margin: 15vmin auto auto 0;
}
#controller button[data-motor="right"] {
margin: 15vmin 0 auto auto;
}
#power {
position: relative;
top: 20vmin;
left: 5vmin;
transform: rotate(-90deg) scale(2);
width: 25vmin;
cursor: pointer;
}
</style>
</head>
<body>
<div id="obniz-debug"></div>
<main>
<h1 class="hide">IoT Controller</h1>
<nav id="controller">
<div id="crossKey">
<button data-motor="flont">↑</button>
<button data-motor="back">↓</button>
<button data-motor="left">←</button>
<button data-motor="right">→</button>
</div>
<div id="powerLever">
<input id="power" type="range" value="50" max="100" min="20">
</div>
</nav>
</main>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://unpkg.com/obniz@3.16.0/obniz.js" crossorigin="anonymous"></script>
<script>
const obniz = new Obniz("obniz-id");
obniz.onconnect = async () => {
const dcMotor1 = obniz.wired("DCMotor", {forward:0, back:1});
const dcMotor2 = obniz.wired("DCMotor", {forward:2, back:3});
const $power = $('#power');
//const servo = obniz.wired("ServoMotor", {gnd:4,vcc:5,signal:6});
const init = () => {
setUI();
setPower($power.val());
};
const setPower = (val) => {
dcMotor1.power(val);
dcMotor2.power(val);
};
const setUI = () => {
const $crossKey = $('#crossKey button');
let direction;
$crossKey.on({
"mouseenter": function() {
direction = $(this).attr("data-motor");
if(direction === "flont"){
dcMotor1.forward();
dcMotor2.forward();
}else if(direction === "back"){
dcMotor1.reverse();
dcMotor2.reverse();
}else if(direction === "left"){
dcMotor1.forward();
dcMotor2.reverse();
}else if(direction === "right"){
dcMotor1.reverse();
dcMotor2.forward();
}
},
"mouseleave": function() {
dcMotor1.stop();
dcMotor2.stop();
}
});
$power.on('input change', function(){
setPower($power.val());
});
};
init();
}
</script>
</body>
</html>