Analog joystick on the PyGamer

How do I read the analogue position for the joystick on the PyGamer? I’ve just done a quick test of dx and dy. These looked promising but testing so far (on the default scaling of 100) shows 0 in the middle (good), far left shows -2.2, -2.1, -2 and occasionally -2.3 (reasonable), half way to left is 0 (not so good). Is dx supposed to give an analogue value? Is this just an overly large deadzone? If so, should that zone be smaller and (less important) is it configurable? Or is there another way to read the analogue joystick? That hardware feature looks unique to the PyGamer at the moment?

BTW, i think there’s a broken link in docs. dx is ok on https://arcade.makecode.com/reference/controller but dy gives an error - note controller vs keys in URL.

The joystick is currently treated as digital with a hard coded dead zone. It would be good to fix dx/dy to be analog though. Or maybe introduce a new api. The current dx/dy are just scaled by frame time.

I started a PR here: https://github.com/microsoft/pxt-common-packages/pull/934 but it’s far from complete. It would be useful to get a few data points on what you see when the joystick is idle with the program attached there. I’m thinking dead zone of -50 to 50.

If you want to do something right now, you can use this program, though it will fail in the simulator:

I guess you could check if the config value is 0, and don’t do the analog read then.

I put the code onto my PyGamer:

  • first center reading -6,3
  • left is -512,6
  • top is -8,-512
  • right is 511,-4
  • down is 5,511
  • centre is -6,4

I was pushing the joystick around carefully but not being super precise. I thought I’d try the diagonals, the values aren’t what I expected, I thought they would all go to magnitude 512:

  • top left, -373,-409
  • top right 412,-467
  • bottom right 422,441
  • bottom left -341, 395

Feeling the joystick at its outer range there is a circular range of travel rather than square so this does make sense.

I’m seeing similar numbers to kjw when I test it on my PyGamer. I made some tweaks to the code, but for some reason it crashes after a seemingly random amount of time. Anything obvious that I’m doing wrong that could cause the crash?

https://makecode.com/_EWgf9XHRYErA

@auxin It would be interesting if you could post your actual numbers. These are important to inform a decision on ranges to encompass hardware variation and user variation. The latter may be significant on the diagonals.

What form does your crash take? Do you get the frown face and a number? Many of the error codes are listed on https://arcade.makecode.com/hardware/errors

I have:

  • Center -18, -29
  • Left -512, -40
  • Up -29, -512
  • Right 511, -40
  • Down 511, -20

Up-Left: -420, -473
Up-Right: 511, -380
Down-Left: -450, 294
Down-Right: 511, 320

It would be interesting to see if these values change over time due to components wearing down… :warning:

1 Like

My exact values are:
Center: 11, -10
Left: -512, -11
Right: 511, -17
Up: -3, -512
Down: 7, 511

Up-Left: -455, -438
Up-Right: 480, -481
Down-Left: -445, 440
Down-Right: 436, 445

When it crashes it is just freezing completely up. I can’t bring up the menu and it doesn’t give the frowny face either.

1 Like

BTW, these docs are broken:

1 Like

I’m seeing the same thing.

The documentation page for dy shows the content of dx in it.

When I open the block editor and right-click help on dy, it will point to the page https://arcade.makecode.com/reference/keys/dy

I sometimes got apparent hangs on a PyGamer also when trying to use the “analog in test” program, the numbers simply stopped updating. This is inconsistent, just now it failed the first time I tried it, but then hasn’t hung again for the next several attempts.

Here’s my numbers:

center: ranges around -10 to 2,14 to 17
… and now it hung again as I tried to get readings.

Is there anything we can do to diagnose this or to help get this working more reliably? I’d love to add analog joystick controls to Space Rocks 3D.

FYI, there’s a .pressureLevel() property on directional inputs that’s available after setting controller.player1.analog = true. See https://github.com/microsoft/pxt-common-packages/pull/934 for an example. I’ve tried this in Space Rocks 3D and it seems to be working great, I haven’t had it hang so far.

(I’m updating this thread since it seems to be a top result for the topic, and the feature seems to be a bit hard to find.)

2 Likes