Joystick that works as a hat switch

Im very new to arduino and have been messing around for a few days trying to do something with my joystick but i can find anything in relation to using a joystick as a hat switch... im going to be using it in a button box as a dpad to navigate through some games as im putting a custom steering wheel on my thrusmaster base and it wont have any buttons on it. If anyone could provide me with some code in relation to my problem it would be much appreciated

Sure. You already have lots to start with in the Arduino development programs you downloaded.

Paul

Paul_KD7HB:
Sure. You already have lots to start with in the Arduino development programs you downloaded.

Paul

Im not sure what you mean by dev programs... are you talking about the "examples"? if so i can really do much with them as ive only got a mirco board a joystick and a few wires.

I don't know what you mean by "hat switch" or "dpad".

If you explain the terms the solution may be simple.

...R

So the dpad is the common term for the cross shaped 4 button switch that is used in almost all generic game controller designs and the hat switch term is used by windows game controller config program for the same configuration of buttons as the dpad. The wording on this could be completely wrong so excuse me for that but i hope it helps.

xdfbgxdth.PNG

download.jpg

my joystick

What joystick is that? You can see it, we can't.

So you want digital outputs from an analog joystick?

groundFungus:
So you want digital outputs from an analog joystick?

If that's possible...

There would be a way to code it so from the value of y0750 to y1023 it would be on the up switch and the down switch would be from y0 to y0250. Or is this not possible? My x axis is connected to a1 and my y axis is a0. the button when pushed in is set to d6.

Thanks Zac.

Ortrin:
If that's possible...

Digital outputs from an analog joystick should be straightforward. Treat any analog value below some threshold as 0 and treat higher values as 1. Something like this

potVal = analogRead(potPin);
if (potVal < 512) {
  selectedVal = 0;
}
else {
  selectedVal = 1;
}

...R

The ADC can't output more than 1023 or less than 0 so all you need to test for is > 750 or < 250.

The next question is how the digital outputs are going to be used? I mean will each comparison call a function?

int yValue = analogRead(A0);
if(yValue > 750)
{
  moveUp();
}
else if(yValue < 250)
{
  moveDown();
}
else
{
  stop();
}

Or will each comparison set the value of some variable or variables for later use? And what use would that be?

Robin2:
Digital outputs from an analog joystick should be straightforward. Treat any analog value below some threshold as 0 and treat higher values as 1. Something like this

potVal = analogRead(potPin);

if (potVal < 512) {
  selectedVal = 0;
}
else {
  selectedVal = 1;
}




...R

Because of its lower quality it likes to jump around a bit so if i were to create a dead zone but doing this code it will work right?

potVal = analogRead(potPin);
if (potVal < 490) {
  selectedVal = 0;
}
of (potval > 510
  selectedVal = 1;
}

Ortrin:
Because of its lower quality it likes to jump around a bit so if i were to create a dead zone but doing this code it will work right?

That's a good idea

...R