Arduino Micro & Unity input system // incorrect input

Hey all,

I’ve been trying to get arduino micro (or any other AVR board with an ATmega32u4 controller) to work with the new unity input system.

Arduino code with joystick library. It has 1 stick and 2 buttons.

#include <Joystick.h>

//Define and Allocate Input Pins to memorable names
#define joyX A0
#define joyY A1
#define joyButton1 6
#define joyButton2 9

//Initializing Axis as Integers, at a 0 default value
int xAxis_ = 0;
int yAxis_ = 0;

//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the digital pin.
//Giving Default Values to the Buttons for later use
int lastButton1State = 0;
int lastButton2State = 0;

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 2, 0, true, true, false, false, false, false, false, false, false, false, false);

//Set Auto Send State
//Enables Auto Sending, allowing the controller to send information to the HID system, rather than waiting to be asked.
const bool initAutoSendState = true;

void setup() {
  pinMode(joyButton1, INPUT_PULLUP);
    pinMode(joyButton2, INPUT_PULLUP);
  //Start Joystick - Needed to start the Joystick function libary
  Joystick.begin();
}

void loop() {
  xAxis_ = analogRead(joyX);
  xAxis_ = map(xAxis_, 0, 1023, 0, 255);
  Joystick.setXAxis(xAxis_);


  yAxis_ = analogRead(joyY);
  yAxis_ = map(yAxis_, 0, 1023, 0, 255);
  Joystick.setYAxis(yAxis_);

  int currentButton1State = !digitalRead(joyButton1);
  if (currentButton1State != lastButton1State) {
    Joystick.setButton(0, currentButton1State);
    lastButton1State = currentButton1State;
  }
 
  int currentButton2State = !digitalRead(joyButton2);
  if (currentButton2State != lastButton2State) {
    Joystick.setButton(1, currentButton2State);
    lastButton2State = currentButton2State;
  }
  delay(10);
}

This all works fine and windows recognizes the stick and buttons. 8519162--1135958--controller.gif

However, working in unity I seem to be having trouble getting it to recognize the controller and the inputs. The default its reading is all out of place. For some reason the value sticks on (-0,71, 0,70). When I move you see the values change though.

8519162--1135961--controllerunity.gif

Moreover, you can see when listening for input on the input action mapping tool. that it can only detect up and left on the stick.

8519162--1135964--controllerunity2.gif

I have tried learning new things from this thread . However, it seems I’m with a different issue. There isn’t any C# code to peel through as this issue seems to be someplace else. But just to verify, in the character creation screen it is also shown that the controller tries to always move up and to the left. Is there maybe another way to debug outside of Unity to test if my joystick is working correctly? Or is there a way to callibrate it in Unity so to negate the already bad input? Thank you for reading and possible your help in advance. :slight_smile:

8519162--1135973--controllerunity3.gif

In a different stroke of (bad)luck, it seems that unity suddenly won’t recognize the joystick anymore. as seen in below gif. 8519180--1135976--controllerunity4.gif Refresh page if gifs stopped working or open gif in a new window.

I noticed it says “Button” under Layout for the stick (followed by x/y as axis). Shouldn’t the stick be only an axis, not buttons? Maybe that confuses Unity.

I’m not sure how much of the input code is on the C# side but just in case I thought I’d mention that you may find something by peeking inside the package. You can also copy the package to the Assets folder and remove it from Package Manager to make it editable in case you want to add some logging to it (packages are readonly by default).