Customize Joystick (Revert forward/backward)

Hi, everyone!

I just got my first arduino card - a Teensy 3.2 and this is really fun!

I bought an joystick to try to make my own controller and I almost got there, just 2 issues:

  1. When I press FORWARD on the joystick - I run backward in game, and when I press the joystick BACK it run forward… Is it possible to revert this by coding? I know I hold the joystick the correct way - since right and left is correct.

  2. Is it an easy way (already exist?) code to turn the joystick into WASD keys? Actually I found some code to do this already, but I also want it to be 8 way WASD… Like if I press the joystick between forward and right it should press [WD] and not only [W] or [D]. And so on for the other “middle” corners.

Thanks for any input! :slight_smile:

  1. Yes, that's totally possible. How depends on the code you haven't shown, the joystick you haven't described, and the rest of the setup that you haven't mentioned.

  2. That's doable. Depends on which type of Arduino you have and whether or not it can act as a keyboard. I don't know about the Teensy 3.2 but I do know that the Leonardo can do this.

You can make the Teensy advertise itself on the USB bus as a joystick or keyboard or both.

Your code can make any output based on any input. It seems like you have a good idea thete.

Delta_G:

  1. Yes, that's totally possible. How depends on the code you haven't shown, the joystick you haven't described, and the rest of the setup that you haven't mentioned.

  2. That's doable. Depends on which type of Arduino you have and whether or not it can act as a keyboard. I don't know about the Teensy 3.2 but I do know that the Leonardo can do this.

Sorry for not mentioning the full setup.

  1. It's a Teensy 3.2 card with this joystick: PS/2 Thumb Joystick Shield Module for Arduino, link:
    Vedlikehold / Oppdatering?route=PS-2-Thumb-Joystick-Shield-Module-for-Arduino&search=joystick

My code:

void setup() {
pinMode(A0, INPUT_PULLDOWN);
pinMode(A1, INPUT_PULLDOWN);
}
void loop() {
Joystick.X(analogRead(A0));
Joystick.Y(analogRead(A1));
delay(50);
}

  1. The teensy card can act as a keyboard. I already tryed to connect buttons to it, which send letters. I also managed to make the joystick send 'W', 'A', 'S' and 'D'.. But the difficult part is when the joystick is between the letters - i want it to send for example 'WD' together..

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

You will only need to change some connections to your joystick, but we need to see your circuit diagram.

Thanks… Tom… :slight_smile:

I connect the wires as shown on the image (blue ones only). AND! I did not wire the bottom blue wire for the push button.

Hi,
OPs image;


Thanks for the image, you will need to change some code to change the direction of the joystick.
Which input on the Arduino is the input from the reversed axis?

Tom... :slight_smile:

Aldrinos:

void setup() {

pinMode(A0, INPUT_PULLDOWN);
  pinMode(A1, INPUT_PULLDOWN);
}
void loop() {
  Joystick.X(analogRead(A0));
  Joystick.Y(analogRead(A1));
  delay(50);
}

You remind me why I love Arduino. This is a useful program that does something fun in only 8 lines.

First, reversing the Y axis. Let's read the raw values from the stick inputs and do some calculation on them before doing the output...

(Note the pinMode is ignored for analogRead: pullups are always turned off the instant you read.)

const int pinXAxis = A0;
const int pinYAxis = A1;

void setup() {
}
void loop() {
  int rawX, rawY, outputX, outputY;
  rawX = analogRead(pinXAxis);
  rawY = analogRead(pinYAxis);

  outputX = rawX;
  outputY = map(rawY, 0, 1023, 1023, 0);

  Joystick.X(outputX);
  Joystick.Y(outputY);
  delay(50);
}

So I made your program a lot longer. Twice the length, due to adding several blank lines. It really helps readability to add some space around units of code.

I named all the pins and intermediate variables. Now it is easier to see what is a pin number and what is an input or output.

The Arduino map() function is perfect for turning an input into an output.

Keyboard coming next but I have to go away and re-read the documentation.

MorganS:
You remind me why I love Arduino. This is a useful program that does something fun in only 8 lines.

First, reversing the Y axis. Let’s read the raw values from the stick inputs and do some calculation on them before doing the output…

(Note the pinMode is ignored for analogRead: pullups are always turned off the instant you read.)

const int pinXAxis = A0;

const int pinYAxis = A1;

void setup() {
}
void loop() {
  int rawX, rawY, outputX, outputY;
  rawX = analogRead(pinXAxis);
  rawY = analogRead(pinYAxis);

outputX = rawX;
  outputY = map(rawY, 0, 1023, 1023, 0);

Joystick.X(outputX);
  Joystick.Y(outputY);
  delay(50);
}




So I made your program a lot longer. Twice the length, due to adding several blank lines. It really helps readability to add some space around units of code. 

I named all the pins and intermediate variables. Now it is easier to see what is a pin number and what is an input or output.

The Arduino map() function is perfect for turning an input into an output.

Keyboard coming next but I have to go away and re-read the documentation.

Great! This worked perfectly and fixed the forward/back -issue. Thanks a bunch :slight_smile: Looking forward for the keyword part aswell.

There are a few more constants to adjust, depending on how well-centered your joystick is and how sensitive you want it to be to movements…

const int pinXAxis = A0;
const int pinYAxis = A1;

const int middleX = 512;
const int middleY = 512;
const int leftThreshold  = middleX - 256;
const int rightThreshold = middleX + 256;
const int upThreshold    = middleY - 256;
const int downThreshold  = middleY + 256;
const int hysteresis = 32; 

void setup() {
}
void loop() {
  int rawX, rawY;
  rawX = analogRead(pinXAxis);
  rawY = analogRead(pinYAxis);

  if(rawX < leftThreshold - hysteresis) Keyboard.press(KEY_A);
  if(rawX > leftThreshold + hysteresis) Keyboard.release(KEY_A);

  if(rawX > rightThreshold + hysteresis) Keyboard.press(KEY_D);
  if(rawX < rightThreshold - hysteresis) Keyboard.release(KEY_D);

  if(rawY < upThreshold - hysteresis) Keyboard.press(KEY_W);
  if(rawY > upThreshold + hysteresis) Keyboard.release(KEY_W);

  if(rawY > downThreshold + hysteresis) Keyboard.press(KEY_S);
  if(rawY < downThreshold - hysteresis) Keyboard.release(KEY_S);  
}

Note there’s no special case for diagonal movements. If you go left and up then it will hold down left (A) and then up (W) and keep them held down until you move the stick back to the middle.

I have not tested this. I don’t know what the keyboard library will do with 20000 “press” events per second. I expect it will be fine because it doesn’t actually send events over the USB wire that often.