Trying to create a flight yoke with the joystick.h library

Hi, I was trying to use the joystick.h to make a flight yoke and it doesn't seem to be working. Does anyone know what am I missing here or what's wrong? x-plane does recognize the arduino leonardo, the 4 axis, x, y, thottle and rudder, but moving the joystick gets no response in x-plane.

#include "Joystick.h"

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_MULTI_AXIS, 0, 0,
  true, true, false, false, false, false,
  true, true, false, false, false);

  int xAxis=A0;
  int yAxis=A1;
  int throttle=A2;
  int rudder=A3;
  void setup() 
{
  Joystick.begin();
  Joystick.setXAxisRange(-127, 127);
  Joystick.setYAxisRange(-127, 127);
  Joystick.setZAxisRange(-127, 127);
  Joystick.setThrottleRange(0, 255);
  Joystick.setRudderRange(0, 255); 
}
  void loop()
  {
        analogRead(xAxis);
        Joystick.setXAxis(xAxis);
        analogRead(yAxis);
        Joystick.setYAxis(yAxis);
        analogRead(throttle);
        Joystick.setXAxis(throttle);
        analogRead(rudder);
        Joystick.setYAxis(rudder);
        Joystick.sendState();
        delay(10);
  }

analogRead(xAxis)analogRead returns a value. Why not use it?

I am not using it properly you mean? I was trying to use analogRead(xAxis) to get the value from A0 and then use Joystick.setXAxis(xAxis) to transfer it to the values that are sent back to the computer.

Why not Joystick.setXAxis(analogRead(xAxis)) etc?

(I really don't know - I don't have a Leonardo, but your way seems a waste of an analogRead)

Well my code looks like this now.

#include "Joystick.h"

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_MULTI_AXIS, 0, 0,
  true, true, false, false, false, false,
  true, true, false, false, false);

  int xAxis=A0;
  int yAxis=A1;
  int throttle=A2;
  int rudder=A3;
  void setup() 
{
  Joystick.begin();
  Joystick.setXAxisRange(-127, 127);
  Joystick.setYAxisRange(-127, 127);
  Joystick.setZAxisRange(-127, 127);
  Joystick.setThrottleRange(0, 255);
  Joystick.setRudderRange(0, 255); 
}
  void loop()
  {
        Joystick.setXAxis(analogRead(A0));
        Joystick.setYAxis(analogRead(A1));
        Joystick.setXAxis(analogRead(A2));
        Joystick.setYAxis(analogRead(A3));
        Joystick.sendState();
        delay(10);
  }

X-plane does seem to be getting some info from the arduino. But it's not working properly, having the potentiometer on the mid position, x-plane sets the bar on the full level for axis 1 and 2, getting it to the 5v side makes the bar go to half of the way, taking it to the other side deals nothing.

(deleted)

So my code would look like

#include "Joystick.h"

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_MULTI_AXIS, 0, 0,
  true, true, false, false, false, false,
  true, true, false, false, false);

  int xAxis=A0;
  int yAxis=A1;
  int throttle=A2;
  int rudder=A3;
  void setup() 
{
  Joystick.begin();
  Joystick.setXAxisRange(0, 255);
  Joystick.setYAxisRange(0, 255);
  Joystick.setThrottleRange(0, 255);
  Joystick.setRudderRange(0, 255); 
}
  void loop()
  {
        Joystick.setXAxis(analogRead(A0)-127);
        Joystick.setYAxis(analogRead(A1)-127);
        Joystick.setXAxis(analogRead(A2)-127);
        Joystick.setYAxis(analogRead(A3)-127);
        Joystick.sendState();
        delay(10);
  }

I'm still getting the same results though, having the pot connected to A0 deals, when having the pointer closer to the GND, both axis 1 and 2 are on max on x-plane, passing the midpoint of the potentiometer, getting closer to 5v deals axis 1 lowering and axis 2 lowering glitchily.Also, same thing happens when connecting to A1.Connecting to A2 and A3 deal a decrease on axis 1 and 2 a bit glitchily.

(deleted)

I SOLVED IT.
I figured that not connecting anything to the analog inputs makes it screw up a bit, it didn't need any compensation, so +-127 wasn't needed. I could improve it a bit and add a few buttons to it, but that'd be a new problem all in all. Thx everyone. This is the code I have now and works fine.

#include "Joystick.h"

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  JOYSTICK_TYPE_MULTI_AXIS, 0, 0,
  true, true, false, false, false, false,
  true, true, false, false, false);

  int xAxis=A0;
  int yAxis=A1;
  int throttle=A2;
  int rudder=A3;
  void setup()
{
  Joystick.begin();
}
  void loop()
  {
        Joystick.setXAxis(analogRead(A0));
        Joystick.setYAxis(analogRead(A1));
        Joystick.setThrottle(analogRead(A2));
        Joystick.setRudder(analogRead(A3));
        Joystick.sendState();
        delay(10);
  }
1 Like