Firstly I hooked up the joystick as shown in the schematic. The two 10k resistors coming from Joystick X and Y are there so when the digital GPIO is sinked to GND the Analog input is not also sinked completely to ground. to check for X GPIO 13 is set to floating and GPIO 14 is set to GND. using those 2 resistors along the voltage at the ADC will be half of what is coming from the Joystick X which will be a maximum of 1.65V. Since the ADC will only read to a maximum of 1 Volt, this voltage needs to be brought down even more. An additional 10K resistor is used to bring the voltage down even further. Ideally this should be a smaller value but in testing 10K seemed to be adequate and this further brought down the BOM.
Using this code I was able to make a function to calibrate the joystick by finding it's maximum and minimum values for both the X and Y and then creating functions for getting the X and the Y values.
int joystick_xmax = 1023;
float joystick_xmult = 1;
int joystick_ymax = 1023;
float joystick_ymult = 1;
static void calibrate()
{
int cnt = 0;
int xmax = 0;
int xmin = 1023;
int ymax = 0;
int ymin = 1023;
while(cnt < 4)
{
int jx = joystick_xmax-get_joystick_x();
int jy = joystick_ymax-get_joystick_y();
if((cnt % 2 == 0) && jx < 450)
cnt++;
if((cnt % 2 == 1) && jx > 800)
cnt++;
xmax = max(xmax, jx);
xmin = min(xmin, jx);
ymax = max(ymax, jy);
ymin = min(ymin, jy);
}
joystick_xmax = xmax;
joystick_xmult = 1023.0/(xmax - xmin);
joystick_ymax = ymax;
joystick_ymult = 1023.0/(ymax - ymin);
}
static unsigned int get_joystick_x()
{
pinMode(14,INPUT);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
return (unsigned int)max(0, min(1023,((joystick_xmax-analogRead(A0))*joystick_xmult)));
}
static unsigned int get_joystick_y()
{
pinMode(13,INPUT);
pinMode(14,OUTPUT);
digitalWrite(14,LOW);
return (unsigned int)max(0, min(1023,((joystick_ymax-analogRead(A0))*joystick_ymult)));
}
Here are some videos of me testing the circuits.
Bread-boarding the circuit.
OLED pong ported over to it using the joystick.
Overview of the fully featured game module. Will be creating PCBs for it soon.
Hey this is pretty clever