Any Arduino enthusiasts or C+ coders out there willing to help with a project (Star Citizen related)

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
So I'm looking for some help, I'm trying to mount an analog stick to my flight stick. I got the analog sticks in today, a Ps2 analog stick with breakout board and a PSP analog stick. I got the PSP stick because I didn't know how bulky the Ps2 stick would be, turns out pretty bulky (I still want to use it but it's going to take a while to make a proper mount for it on the joystick).

I found a tutorial online but I can't get the code to compile, I'm not a coder (willing to learn) so I don't know what the problem might be. The only thing I can think of was the tutorial was meant for Sparkfun Pro Micro and I'm using an Arduino Nano. I would think the code would still compile but just not work on my microcontroller.

I do realize this is not a small Arduino project. From what I've seen around there are 2 files that need to be modified or rewritten in the Arduio IDE, the HID.ccp and USBAPI.h files (have no clue what those files do but I think these files make the joystick and Arduino communicate back to the PC as a controller)

Here's the tutorial link: http://www.instructables.com/id/Add-a-little-two-analog-axis-thumb-joystick-to-you/?ALLSTEPS
If there's anyone out there willing to help let me know!
 

Slansickness

Space Marshal
Mar 14, 2015
92
163
2,360
RSI Handle
Slantsickness
I did a little digging. The Arduino Nano seems to lack the ability to be used as a USB HID. In case you are unfamiliar, this is a USB Human Interface Device, a generic term used for a keyboard, mouse, joystick, or thumbstick. The Nano uses an ATmega328 or ATmega168 chip, while the Sparkfun Pro uses an ATmega32U4. Close, but the 32U4 has integrated USB which can be reprogrammed as a HID, while the micro has a second chip to interface the USB to the microcontroller, and that chip cannot be reconfigured to act as a HID. This is also why your code wont compile.

Unfortunately, your best option is to get a different Arduino. The SparkFun Pro Micro of course would work, as would the Arduino Micro or the larger Leonardo. Also the Teensy 2.0. Really, just about anything that has the ATmega32U4.
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
Right on I really appreciate it! I just ordered the Sparkfun Pro Micro, I have an electronics store close by but their sold out. Guess I'll have a few more days. Thanks again Slansickness.

I'll update the thread once I get it all up and running. Dual sticks is great but I want a little more accuracy when it comes to attacking or defending myself (I want to be able to use my gimbals with precision).
 

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
How do you have your sticks mapped? I'm planning on doing something similar, let me know how everything goes!
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
I got 2 T16000ms. I plan on having the analog stick mounted on the right flight stick where my thumb sits and using it for Aim (but you should be able to map it to whatever you want)


(Left Stick)
X-Strafe left and right
Y-Strafe up and down
Z-Strafe forward and backward
Button1 (Trigger)-Boost
Button2-Toggle Freelook (Normally have this as Spacebrake, trying some new stuff and you can essentially do the same thing by boosting and throttling down)
Button3-Toggle Decoupled Mode
Button4-Reset Aim
Hat Buttons-Freelook
(Buttons on the Left)
Button5-Eject
Button8-Match Target Velocity
(Buttons on the Right)
Button12-Cycle Friendly Targets
Button13-Cycle Hostile Targets
Button14-Target Nearest Hostile
Button11-Launch Countermeasure
Button16-Change Countermeasure
Throttle Slider-Throttle (for long distance flights, I usually just use the strafe forward/back for better flight precision)

(Right Stick)
X-Roll
Y-Pitch
Z-Yaw
Button1 (Trigger)-Fire 1
Button2-Fire 2
Button3-Fire 3
Button4-Missile Lock and Fire
Hat Buttons-Aim (Aim, and Freelook is the new stuff I was trying, and I'm even more determined to get the analog stick to work now)
Button7-Autoland Mode
Button8-Landing System
Button9-Landing Target Lock On
Button11-Force Re-Spawn (EVA)
Button16-Scoreboard


I would much rather use those hat buttons for something like targeting and matching velocity. I also plan on having all my Power, Shields and HUD controlled through Roccat Power Grid (I have a nexus 5 and 7 (the 7 needs to be repaired at the moment)) And when they get some type of in-game voice chat system going up I'll probably start using Voice Attack again, for opening a line of communications to other ships.
 

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
I'll be interested to hear how you like aiming with an analog stick. I'm planning on stuffing the guts from a mini trackball mouse into my joystick once I've gotten it working, for the same reason you're adding the analog stick.
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
Ok guys got it all wired up but now I ran into some more issues. The Y axis seems to be registering funny. When I push up on the stick it doesn't go completely to the top and when I push down it cuts off the bottom and registers up top. Anyone have any ideas?
 

Attachments

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
Do you have a multimeter handy so that you can check check what the voltages are? if it's anything like the xbox controller I'm using(and you've got it hooked up to 5V), it will be ~2.5V when the stick is centered, ~5V at one end and ~0V at the other. If it looks like that is reading right, then you can use "Serial.println(<whatever variable the voltage is being read to>);" in your code and open the serial monitor to see if the arduino is reading the voltage right. I'm guessing it's got a 10 bit analog input, so the serial monitor will show: 0=0V, 2048=2.5V, and 4096=5V.

If the voltage is coming out of the stick is shifted, you should be able to shift it back in the code, by adding or subtracting an amount so that the the arduino ends up seeing 2048 while it is centered. You would just need to make sure that you don't end up with numbers less than 0, or greater than 4096. I think the snippet below would work, but everyone should correct me if I'm wrong

say it is offset up by 1.25V, so it reads 3072 at center(shifted up by 1024 bits)

Y=analogRead(19);
Y=Y-1024;
if(Y<0){
Y=4096-Y;
}

Good luck!
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
Finally got my hands on a multimeter, and I'm getting some weird #s. It's pretty much doing what the tracking screen looks like its doing. One side of the axis goes to 0 and the other side will start to rise and then shoot to close to 0. Sometimes I can get it to read 4. As soon as it hits about 2.8 it'll go to 0.

Here's the code used in the ino. I couldn't get the debug mode working for some reason.

Code:
ThumbState_t thumbSt;

const bool DEBUG = false;  // set to true to debug the raw values

int xPin = A2;
int yPin = A3;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5;  // smaller values will be set to 0

void setup(){
     pinMode(xPin, INPUT);
     pinMode(yPin, INPUT);

     if(DEBUG) {
         Serial.begin(9600);
     }

    // calculate neutral position
    xZero = analogRead(xPin);
    yZero = analogRead(yPin);

    thumbSt.xAxis = 0;
    thumbSt.yAxis = 0;
}

void loop(){
    xValue = analogRead(xPin) - xZero;
    yValue = analogRead(yPin) - yZero;

    if(abs(xValue) < deadzone) {
        xValue = 0;
    }
    if(abs(yValue) < deadzone) {
        yValue = 0;
    }

    thumbSt.xAxis = map(xValue, 400, -400, -32768, 32768);  // here the axis is inverted
    thumbSt.yAxis = map(yValue, -400, 400, -32768, 32768);

     if(DEBUG) {
         Serial.print("X: ");
        Serial.println(xValue);
         Serial.print("Y: ");
        Serial.println(yValue);
    }

    // Send to USB
    Thumbstick.setState(&thumbSt);
}
Probably should have learned to code ugh..
 

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
If the multimeter is showing the same thing that the software is, it is probably an issue with the thumbstick. I'm guessing the thumbstick has a pair of potentiometers in it, and from the picture in the link you posted it looks like there are 4 leads, is that right? How did you figure out which lead was power/ground/output? I've wired pots with power to the wiper before, and the arduino loses power because you short power to ground at one end, which could maybe explain the drop to 0V you see. Can you check the resistance between the pins you have as ground and power, that resistance should not change as you wiggle the joystick. Also, check between the signal pin(the wiper of the potentiometer) and ground, that resistance should change from some middle value at center to a max at one end and 0 at the other.


Let me know what you come up with
 

Audax Phidippus

Captain
Donor
Feb 21, 2015
79
56
250
RSI Handle
Audax_Phidippus
This is great, I was looking to try and get my Arduino to output just one axis as a throttle, but failed and gave up. I do that.
You may want to look into UnoJoy, I was told that was the easiest method for arduino to PC controller.. but again, I failed.

For anyone else thinking about getting the same result that Outlaw is, without the programming hassles, I have an alternative - if messier - method. You can buy a wired 360 controller from gamestop for about $20. Open it up, de-solder one of the thumbstick capacitors, mount it in your stick, run leads back to the pcb, plug-n-play. This way leaves you with additional options, as well.
Add the other thumbstick to your throttle, for strafing(unless you are already CH master-race)? You also have a dozen buttons available to run lines from, so you could create a button-box - in the likely event that you'll want a sh1t-ton of buttons to play the final release.
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
I might buy a few more sticks to be sure (I can always add more!) I'll try to rewire it up tomorrow just in case something is shorting out.
They got a pic posted on the instructables tutorial page showing what leads where what.

@Audax Phidippus Man that's crafty I need to go buy a new controller anyway I might do this with my old controller. Only downside would seem to be the size of the PCB, I think it's about 4 times the size of the Pro Micro (I think I might have a place to hide it though).

Another thing I'm curious about is does anyone know if it's possible to solder to flex cables/PCBs? There is a Ps Vita analog stick that is probably the best size and best feel (for the size that it is, I've owned a PSP and a Vita and the Vita analog sticks are way better) The only problem is it doesn't have solder leads like the PSP stick does it has that flex cable. I haven't seen anyone try to do it.
 

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
I have not heard of anyone soldering to flex cable, but I know that it is possible to solder to traces on regular PCBs, so give it a try!

Also, an alternative to unojoy/coding yourself would be MMJoy, its what I'm using for my project and it's got a really nice interface.
 

K1NG

Captain
Feb 21, 2015
309
180
210
RSI Handle
K1NG_Brit
Pastebin or gist me the code and I'll take a look. I'm intermediate with C++ and C# but any OOP language shares similar traits.

I'm looking at you JS!
 

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
Sorry for not replying earlier!

Code:
ThumbState_t thumbSt;

const bool DEBUG = false; // set to true to debug the raw values

int xPin = A2;
int yPin = A3;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5; // smaller values will be set to 0


void setup(){
        pinMode(xPin, INPUT);
        pinMode(yPin, INPUT);
        if(DEBUG) {
                Serial.begin(9600);
        }
        // If at the neutral position you are reading a number too large, xZero will be positive, and get subtracted in the void loop, otherwise it will be negative, and get added.
        xZero = analogRead(xPin)-2048;
        yZero = analogRead(yPin)-2048;
        thumbSt.xAxis = 0;
        thumbSt.yAxis = 0;
}

void loop(){
xValue = analogRead(xPin) - xZero;
yValue = analogRead(yPin) - yZero;

//I'm pretty sure you need to keep the values between 0-4096, but I could be wrong
if(yValue<0){
yValue=0;
}elseif(yValue>4096){
yValue=4096
}
if(xValue<0){
xValue=0;
}elseif(xValue>4096){
xValue=4096
}

//I changed the deadzone stuff to work with values 0-4096
if(xValue< 2048+deadzone && xValue>2048-deadzone) {
xValue = 2048;
}
if(yValue< 2048+deadzone && yValue>2048-deadzone) {
yValue = 2048;
}

thumbSt.xAxis = map(xValue, 400, -400, -32768, 32768); // here the axis is inverted
thumbSt.yAxis = map(yValue, -400, 400, -32768, 32768);

if(DEBUG) {
Serial.print("X: ");
Serial.println(xValue);
Serial.print("Y: ");
Serial.println(yValue);
}

// Send to USB
Thumbstick.setState(&thumbSt);
}

Let me know if that works better for you. Also, I think you might need a bigger deadzone, but psp sticks could just be way better than xbox sticks!
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
Nope no dice, I'm really starting to think it's the analog stick. I'm going to order one from two different sites and see what happens.
For some reason the code wouldn't compile, I had made a few changes on the else if parts here's what I got (it could be very possible that I canceled out what you changed, have no clue)
Code:
ThumbState_t thumbSt;

const bool DEBUG = false; // set to true to debug the raw values

int xPin = A2;
int yPin = A3;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5; // smaller values will be set to 0


void setup(){
        pinMode(xPin, INPUT);
        pinMode(yPin, INPUT);
        if(DEBUG) {
                Serial.begin(9600);
        }
        // If at the neutral position you are reading a number too large, xZero will be positive, and get subtracted in the void loop, otherwise it will be negative, and get added.
        xZero = analogRead(xPin)-2048;
        yZero = analogRead(yPin)-2048;
        thumbSt.xAxis = 0;
        thumbSt.yAxis = 0;
}

void loop(){
xValue = analogRead(xPin) - xZero;
yValue = analogRead(yPin) - yZero;

//I'm pretty sure you need to keep the values between 0-4096, but I could be wrong
if(yValue<0){
yValue=0;
}else if(yValue>4096){
yValue=4096;

}if(xValue<0){
xValue=0;
}else if(xValue>4096)
xValue=4096;


//I changed the deadzone stuff to work with values 0-4096
if(xValue< 2048+deadzone && xValue>2048-deadzone) {
xValue = 2048;
}
if(yValue< 2048+deadzone && yValue>2048-deadzone) {
yValue = 2048;
}

thumbSt.xAxis = map(xValue, 400, -400, -32768, 32768); // here the axis is inverted
thumbSt.yAxis = map(yValue, -400, 400, -32768, 32768);

if(DEBUG) {
Serial.print("X: ");
Serial.println(xValue);
Serial.print("Y: ");
Serial.println(yValue);
}

// Send to USB
Thumbstick.setState(&thumbSt);
}
I think I might start a TEST Modders Thread, making it easier to find certain stuff. We could have a catalog of mods we use (from physical mods like this joystick mod to what mods we use with our games), put together from all of us. I think it would be a cool way to see what others got going on and a good way to learn. Let me know what you guys think and maybe we can run it by Montoya.
 

honcho12

Space Marshal
Oct 12, 2014
600
372
2,410
RSI Handle
honcho12
I think that is a fantastic idea! Slansickness made those slick pedals, and I'm waiting on some 3d printed parts to get my joystick together.

You said that the multimeter was giving similar numbers to the code, so a different thumbstick sounds like a good idea. I'll see if I can get the code to compile for you.
 

OuTLaW66613

Space Marshal
Jan 9, 2015
58
49
1,710
RSI Handle
OuTLaW66613
That would be awesome! In the time while I'm waiting on the sticks I'm going to try to learn some more of the Arduino IDE. I have that Leonardo also and think I might make another type of controller, then we'll have the Sparkfun and the Leonardo route.
 
Forgot your password?