Skip to content

Instantly share code, notes, and snippets.

@pimpelsang
Last active October 30, 2021 19:46
Show Gist options
  • Select an option

  • Save pimpelsang/c0d71e8151ad28f4b87f286b45784ceb to your computer and use it in GitHub Desktop.

Select an option

Save pimpelsang/c0d71e8151ad28f4b87f286b45784ceb to your computer and use it in GitHub Desktop.
wedo universal two motor remote
<html>
<head>
<title>two motors car remote</title>
<meta name="mobile-web-app-capable" content="yes">
<link rel="shortcut icon" href="
" type="favicon/ico" />
<script src="https://cdn.jsdelivr.net/npm/node-poweredup@latest/dist/browser/poweredup.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=" crossorigin="anonymous"></script>
<style type="text/css">
#connect {
width: 50%;
height: 50%;
font-size: 50pt;
padding: 0px;
width:100%
}
#controls {
display: none;
}
#controls button {
position: fixed;
top: 0%;
width: 50%;
height: 50%;
font-size: 50pt;
padding: 0px;
}
#controls button.backward {
top:50%;
}
#controls button.forward2 {
height: 100%;
width: 25%;
left:50%;
}
#controls button.backward2 {
height: 100%;
width: 25%;
left:75%;
}
</style>
</head>
<body style="margin: 0px;">
<h3 style="text-align: center; padding: 20px;">WEDO2 two motors remote </h3>
<button id="connect">connect</button>
<div id="controls">
<button class="forward" data-speed='100'>⬆️</button>
<button class="backward" data-speed='-100'>⬇️</button>
<button class="forward2" data-speed='-100' data-second='true'>⬅️</button>
<button class="backward2" data-speed='100' data-second='true'>➡️</button>
</div>
<script>
if (!PoweredUP.isWebBluetooth) alert("Your browser does not support the Web Bluetooth.");
let hub, motorA, motorB;
const poweredUP = new PoweredUP.PoweredUP();
poweredUP.on("discover", async (_hub) => {
hub = _hub;
await hub.connect();
motorA = await hub.waitForDeviceAtPort("A");
if (motorA.type != PoweredUP.Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR) {
alert('No motor attached to port A');
}
motorB = await hub.waitForDeviceAtPort("B");
if (motorB.type != PoweredUP.Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR) {
alert('No motor attached to port B');
}
$('#controls').show();
hub.on("disconnect", () => {
$('#connect').show();
$('#controls').hide();
});
});
$('#connect').click(() => {
poweredUP.scan();
$('#connect').hide();
});
$('#controls > button')
.on('mousedown touchstart', (e)=> {
e.preventDefault();
const data = $(e.target).data();
(data.second ? motorB : motorA).setPower(data.speed);
})
.on('mouseup touchend',(e)=> {
e.preventDefault();
const data = $(e.target).data();
(data.second ? motorB : motorA).setPower(0);
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment