Skip to content

Instantly share code, notes, and snippets.

@graste
Forked from pimpelsang/index.html
Created December 13, 2025 14:40
Show Gist options
  • Select an option

  • Save graste/e5f6b15e832372c9421f19cd0849c5d2 to your computer and use it in GitHub Desktop.

Select an option

Save graste/e5f6b15e832372c9421f19cd0849c5d2 to your computer and use it in GitHub Desktop.
Example code for wedo 2.0 car remote
<html>
<head>
<title>two motors tank remote</title>
<script src="https://cdn.jsdelivr.net/npm/node-poweredup@latest/dist/browser/poweredup.js"></script>
<style type="text/css">
#controls {
display: none;
}
button{
position: fixed;
width: 49%;
height: 49%;
font-size: 50pt;
padding: 0px;
}
#backward {
top:50%;
}
#left {
top:50%;
width: 25%;
left:50%;
}
#right {
top:50%;
width: 25%;
left:75%;
}
</style>
</head>
<body>
<button onclick="connect()" id="connect">connect</button>
<div id="controls">
<button id="forward">⬆️</button>
<button id="backward">⬇️</button>
<button id="left">⬅️</button>
<button id="right">➡️</button>
</div>
<script>
if (!PoweredUP.isWebBluetooth) {
alert("Your browser does not support the Web Bluetooth.");
}
const poweredUP = new PoweredUP.PoweredUP();
poweredUP.on("discover", onDiscover);
let hub;
function connect() {
poweredUP.scan();
document.getElementById('connect').style.display='none';
}
let motorA, motorB;
async function onDiscover(newHub) {
hub = newHub;
console.log(`Discovered ${hub.name}!`);
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');
}
document.getElementById('controls').style.display='block';
console.log('Motors ready')
}
// buttons and are they down
let buttons = {
forward: false,
backward: false,
right: false,
left: false
};
for (const button in buttons) {
let el = document.getElementById(button)
el.addEventListener('touchstart', function(e) {
e.preventDefault();
buttons[button] = true;
// console.log(`${button} pressed`);
update();
})
el.addEventListener('touchend', function(e) {
e.preventDefault();
buttons[button] = false;
// console.log(`${button} released`);
update();
});
}
async function update() {
if (!motorA || !motorB) return;
let a = 0, b = 0;
// simple car mode
if (buttons.right) {
console.log("driving right");
b = 1;
} else if (buttons.left) {
console.log("driving left");
a = 1;
} else if (buttons.forward) {
console.log("driving forward");
a = 1;
b = 1;
} else if (buttons.backward) {
console.log("driving back");
a = -1;
b = -1;
} else {
console.log("stop driving");
}
await motorA.setPower(a * 80);
await motorB.setPower(b * 80);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment