Hi folks,
I have a Turnigy 5x controller that I would like to hook up to my PC so that I can use it with a flight simulator.
I've tried out UnoJoy, but the main file uses analog for the thumbsticks, but mine returns PWM values. I found one example that uses PWM, but it is for a controller than packages all the sticks into a single PWN output. My controller sends individual signals for each axis and stick (so 4 outputs/inputs instead of one from the previous tutorial).
I've arduino experienced, but not very code saavy, so this is out of my own problem-solving capabilities. If anyone has done this before, or can offer up a code that I can try, I would appreciate the help.
TIA
#include "UnoJoy.h"
bool FirstLoop;
dataForController_t controllerData;
#define PPM_Pin 3 // Pin of PPM Input. Must be 2 or 3
int ppm[16]; // array for storing up to 16 servo signals
void setup(){
setupUnoJoy();
FirstLoop = true;
pinMode(PPM_Pin, INPUT);
attachInterrupt(PPM_Pin - 2, read_ppm, CHANGE);
TCCR1A = 0; //reset timer1
TCCR1B = 0;
TCCR1B |= (1 << CS11); //set timer1 to increment every 0,5 us
}
void loop(){
if(FirstLoop == true) {
controllerData = getControllerData();
FirstLoop = false;
}
controllerData.leftStickX = map(ppm[0], 990, 2010, 0, 255);
controllerData.leftStickY = map(ppm[1], 990, 2010, 0, 255);
controllerData.rightStickX = map(ppm[2], 990, 2010, 0, 255);
controllerData.rightStickY = map(ppm[3], 990, 2010, 0, 255);
setControllerData(controllerData);
}
dataForController_t getControllerData(void){
dataForController_t controllerData = getBlankDataForController();
controllerData.triangleOn = 0;
controllerData.circleOn = 0;
controllerData.squareOn = 0;
controllerData.crossOn = 0;
controllerData.dpadUpOn = 0;
controllerData.dpadDownOn = 0;
controllerData.dpadLeftOn = 0;
controllerData.dpadRightOn = 0;
controllerData.l1On = 0;
controllerData.r1On = 0;
controllerData.selectOn = 0;
controllerData.startOn = 0;
controllerData.homeOn = 0;
controllerData.leftStickX = 0;
controllerData.leftStickY = 0;
controllerData.rightStickX = 0;
controllerData.rightStickY = 0;
return controllerData;
}
void read_ppm(){ //leave this alone
static unsigned int pulse;
static unsigned long counter;
static byte channel;
counter = TCNT1;
TCNT1 = 0;
if(counter < 1020){ //must be a pulse if less than 510us
pulse = counter;
}
else if(counter > 3820){ //sync pulses over 1910us
channel = 0;
}
else{ //servo values between 510us and 2420us will end up here
ppm[channel] = (counter + pulse)/2;
channel++;
}
}