// Requires Arduino Joystick Library https://github.com/MHeironimus/ArduinoJoystickLibrary
#include <Joystick.h>
Joystick_ Joystick;
int JoystickX;
int JoystickY;
int JoystickZ;
int Throttle;
int Rudder;
int Aux1;
int Aux2;
int currentButtonState0;
int lastButtonState0;
int currentButtonState1;
int lastButtonState1;
int currentButtonState2;
int lastButtonState2;
int currentButtonState3;
int lastButtonState3;
int currentButtonState4;
int lastButtonState4;
int currentButtonState5;
int lastButtonState5;
int currentButtonState6;
int lastButtonState6;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(0, 965);
Joystick.setYAxisRange(192, 522);
Joystick.setZAxisRange(143, 735);
Joystick.setRxAxisRange(158, 456);
Joystick.setRyAxisRange(158, 456);
Joystick.setThrottleRange(154, 448);
}
void loop() {
// Read Joystick
JoystickX = analogRead(A0);
JoystickY = analogRead(A1);
JoystickZ=analogRead(A8);
Throttle=analogRead(A2);
Aux1=analogRead(A9);
Aux2=analogRead(A3);
Serial.print(JoystickX);
Serial.print(" ");
Serial.print(JoystickY);
Serial.print(" ");
Serial.println(JoystickZ);
// Read Switches
int currentButtonState0 = !digitalRead(2); // Button 0
if (currentButtonState0 != lastButtonState0)
{
Joystick.setButton(0, currentButtonState0);
lastButtonState0 = currentButtonState0;
}
int currentButtonState1 = !digitalRead(3); // Button 1
if (currentButtonState1 != lastButtonState1)
{
Joystick.setButton(1, currentButtonState1);
lastButtonState1 = currentButtonState1;
}
int currentButtonState2 = !digitalRead(4); // Button 2
if (currentButtonState2 != lastButtonState2)
{
Joystick.setButton(2, currentButtonState2);
lastButtonState2 = currentButtonState2;
}
int currentButtonState3 = !digitalRead(5); // Button 3
if (currentButtonState3 != lastButtonState3)
{
Joystick.setButton(3, currentButtonState3);
lastButtonState3 = currentButtonState3;
}
int currentButtonState4 = !digitalRead(6); // Button 4
if (currentButtonState4 != lastButtonState4)
{
Joystick.setButton(4, currentButtonState4);
lastButtonState4 = currentButtonState4;
}
int currentButtonState5 = !digitalRead(7); // Button 5
if (currentButtonState5 != lastButtonState5)
{
Joystick.setButton(5, currentButtonState5);
lastButtonState5 = currentButtonState5;
}
// Output Controls
Joystick.setXAxis(JoystickX);
Joystick.setYAxis(JoystickY);
Joystick.setZAxis(JoystickZ);
Joystick.setThrottle(Throttle);
Joystick.setRxAxis(Aux1);
Joystick.setRyAxis(Aux2);
Joystick.sendState();
}
Please edit your post to add code tags, and a link to the joystick product page or user manual.
Posting instructions can be found in the "How to get the best out of this forum" post, linked at the head of every forum category.
Here you go…
You will see great code benefits when you learn to use variables in arrays. Those are collections of variables that have one name plus a number that can be another variable. One piece of code can then address any of the set just by changing the number instead of needing a piece of code for every different name.
pins 4 = A6, 6 = A7, 8 = A8, 9 = A9, 10 = A10, A0 = A0, A1 = A1, A2 = A2,
and A3 = A3.
it's mapped here: https://www.researchgate.net/figure/Arduino-Pro-Micro-connections-pinouts_fig2_338345202
of course, it's specific to the Pro Micro.
done
thanks but i wanted to know where the middle,right,left wire goes to which point if i have 3 potentiometers
thanks
@bbqmkc
sorry, I thought you needed to know what all of the analog pins are, since some are both analog and digital pins, and they show their digital pin number, which can be a bit confusing.
Np, any idea about it tho?
potentiometers?
oh, you mean for your joystick?
if so, its y,
fully left it's at around analog value 0,
fully middle it's at around analog value 512,
fully right it's at around analog value 1024.
though it may depend on the position of the joystick.
Yeah i making a flight sim, i was actually asking which wire to solder to which port on the board
if I'm thinking correctly, you should solder pin A1 to the y pin.
I just found out your joystick is a 3D joystick with pins X, Y, and Z.
mine however is 2D, with pins X, Y, and SW.
however maybe your Z pin is the SW pin.
I am so sorry to say, it USED TO BE easy to find clear explanation of every part on the Arduino Main Site but they changed it into a confusing mess!
There USED TO BE links at the top of the forum with menus that went straight to a page for each Tutorial in the IDE with explanations and diagrams and pictures that made it all clear.
But some TWERPS decided to make it as confusing as possible.
Hey @Ptillish! Do you happen to have a map to the Tutorial HELP pages that doesn't include EVERYTHING ELSE?
What is there now is actually worse than Microsoft Help!
@bbqmkc
make sure to test it before using it on the sim!
I do not want you to have any issues with your sim later on.
sorry about that, it's confusing to me too, to be honest.
I used to be able to link to the simple explanations on the Main Site but it has been "improved".
Now, it's down to Google searches.
thank you, ill try this
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.