My plan is to take my RC car transmitter & receiver connect the receiver to my arduino and convert the raw data over PWM for two channels into a usable joystick I can use on my PC to play VRC Pro (Virtual RC Pro).
However I'm currently trying to troubleshoot why my arduino is jumping around when it is set as a Joystick using UnoJoy. My current code is this.
#include "UnoJoy.h"
const int numReadings = 16;
int ch1index = 0;
int ch2index = 0;
int ch1t = 0; //Channel 1 Total
int ch2t = 0; //Channel 2 Total
int ch1a = 0; //Channel 1 Average
int ch2a = 0; //Channel 2 Average
int ch1raw[numReadings]; //Raw Ch1 Data
int ch2raw[numReadings]; //Raw Ch2 Data
int ch1 = 0; //Final Ammount /255
int ch2 = 0; //Same for Ch2
void setup(){
setupPins();
setupUnoJoy();
for (int ch1read = 0; ch1read < numReadings; ch1read++)
ch1raw[ch1read] = 0;
for (int ch2read = 0; ch2read < numReadings; ch2read++)
ch2raw[ch2read] = 0;
}
void loop(){
// Always be getting fresh data
dataForController_t controllerData = getControllerData();
setControllerData(controllerData);
ch1t= ch1t - ch1raw[ch1index];
ch2t= ch2t - ch2raw[ch2index];
ch1raw[ch1index] = ch1;
ch2raw[ch2index] = ch2;
ch1 = pulseIn(2, HIGH, 20000);
ch2 = pulseIn(3, HIGH, 20000);
ch1 = map(ch1, 965, 1965, 0, 255);
ch2 = map(ch2, 960, 1955, 0, 255);
//Adding the total.
ch1t= ch1t + ch1raw[ch1index];
ch2t= ch2t + ch2raw[ch2index];
//Indexing + 1
ch1index = ch1index + 1;
ch2index = ch2index + 1;
if (ch1index >= numReadings)
ch1index =0;
if (ch2index >= numReadings)
ch2index =0;
ch1a = ch1t / numReadings;
ch2a = ch2t / numReadings;
//Serial.print("Channel 1:");
//Serial.println(ch1a);
//Serial.print("Channel 2:");
Serial.println(ch2a);
//delay(10);
}
void setupPins(void){
// Set all the digital pins as inputs
// with the pull-up enabled, except for the
// two serial line pins
pinMode(2, INPUT);
pinMode(3, INPUT);
}
dataForController_t getControllerData(void){
// Set up a place for our controller data
// Use the getBlankDataForController() function, since
// just declaring a fresh dataForController_t tends
// to get you one filled with junk from other, random
// values that were in those memory locations before
dataForController_t controllerData = getBlankDataForController();
// Since our buttons are all held high and
// pulled low when pressed, we use the "!"
// operator to invert the readings from the pins
// Set the analog sticks
// Since analogRead(pin) returns a 10 bit value,
// we need to perform a bit shift operation to
// lose the 2 least significant bits and get an
// 8 bit number that we can use
controllerData.leftStickX = ch1a;
//controllerData.leftStickY = analogRead(A1) >> 2;
controllerData.rightStickX = ch2a;
//controllerData.rightStickY = analogRead(A3) >> 2;
// And return the data!
return controllerData;
}
Using the serial print I'm watching the data that I'm sending to the controllerData. Which I've gotten it to be stable and not jump around. However once I change the unit into DFU and update the firmware to turn it into a Joystick. It reads very unstable on windows. Hovering back and forth between 130 and 138 for channel 1. I suspect the problem might not be with my code but UnoJoy. However I don't know much about coding and could use some assistance. As this is one of my first projects that I've been wanting to do.