I've pretty much torn my sketch apart. As it wasn't working for me. I want to be using UnoJoy to switch the arduino over from a programming board, to a PC Joystick. Even when I average out the input that it reads it wills till jump around when in 'joystick' mode.
The sketch I'm using for the UnoJoy joystick is below.
#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;
}
Or well I was using it. I've been experimenting with different codes to see what kind of results I get as a Serial Print. My overall goal is to get an 8bit number I can spit back out to the controllerData that doesn't move if I don't move my controller.
What I found strange while doing some testing, was that if I connected my ESC to the receiver for power, and just take the PWM signal to the arduino it'll give me a reading of min 0, max ~1470 so its pulsing up and down between 0 and 1470. While if I use the power from the 5v on the arduino it'll just bounce around from ~1430-1470. Don't really know what that means.
As for that page, I've looked over it a few times, I didn't quite understand it. I've got very basic knowledge when it comes to coding, even more so when it comes to arduino.