# Microbit as Computer Mouse (HID/Physical Computing)

Hello,

I’m wondering if anyone can help me out.
I’m trying to make a microbit v2 become a computer mouse/control a computer mouse on an iPad.

I’m following this tutorial: https://bsiever.github.io/microbit-pxt-blehid/
and this video: https://youtu.be/n4J5GN72N_4

And I copied the code and instructions directly from both and there appears to be a connection between the microbit and ipad, and the cursor shoots up and touching the ipad screen with my finger becomes challenging so I know something’s happening, but I cannot use the microbit to control the cursor.

1 Like

Hi @knope — welcome to the forum and sorry for the delay in replying!

I just tried the original example (my code) on an iPad and it worked pretty well, which surprised me!
I’ve taken a look at your code and spotted a few small problems.

There are a few things going on in the original code. There’s the `0.25*(TCC-Y)` part. The `TCC-Y` is identifying how far the accelerometer on the micro:bit has moved since it was last checked. The 0.25 just scales this number down so the mouse will move a little slower. Using 1.0 is almost impossible to control because it moves so fast. 0.25 isn’t bad. 0.01 is slow and you aren’t able to move the mouse across the entire iPad screen. That part of your code seems ok, but you might want to test different numbers when it’s working better.

Accelerometer motion can be really “jerky”, so I decided to smooth it out by averaging each accelerometer update with the prior update. The last two lines should be equations that look like `x = 0.5*x + 0.5*newX`. Basically it’s averaging the old value and the new value. The two coefficients (the 0.5s) should add to 1. You are using 0.25 and 0.25, which will make the mouse quickly drift to the origin. I didn’t just do `(x+newX)/2` because I tried several variations of weights. Using something like 0 and 1 will cause the cursor to jitter a bit. Using 0.9 and 0.1 will smooth out the motion a little more than 0.5 and 0.5.

It looks like you may also be updating values inconsistently. Your example does something like: `Y = 0.25+X ...`. It’s changing the `Y` based on the old `X`. We’ve got two sets of coordinates to deal with: Accelerometer x and y (on the micro:bit) and the virtual mouse’s x and y. It’s best to think of these somewhat like units and avoid “mixing” units from the same device. Something that’s based on the micro:bit x probably should never be combined with something based on the micro:bit y. (Although, if you’re really consistent you can use micro:bit x as mouse y and micro:bit y as mouse x. You may have to turn the micro:bit sideways to get it to behave as expected).

Let me know if that helps,
Bill

1 Like

@bsiever, thank you for your help!
After reading over your input, I played around and got things to mostly work: https://makecode.microbit.org/_CmEEL4RVAJmm

I think it could be better, like it’s a little finicky, but it’s okay. Click and Drag seems to be hard for me/my code. My goal is to draw with the microbit as the on-screen cursor. I think it’s just tricky.

Great — I’m glad you’ve been able to make progress. Do you really want the middle button to almost always “pressed”?

I might suggest: A) Trying `middle pressed` as `false` and/or B) adding a `true` in the `hold buttons`.

Bill

@knope Here’s a variation you might want to try: https://makecode.microbit.org/_Jyb6KsLw0bxy
It uses A to toggle whether it’s in drag/draw mode or not.

Bill