Questions about custom gaming controller?

Status
Not open for further replies.

bwalls1996

New member
Hi everyone, im using a Teensy 3.2 to make a custom USB game controller using a psp style joystick and tactile momentary buttons. I've got the joystick being read, I'm just having trouble mapping it. I'm using the following code and have tried plugging in different values but nothing seems to be working correctly.
Code:
Joystick.X(map(analogRead(0),170, 853, 1023, 0));
Joystick.Y(map(analogRead(1),170, 853, 0, 1023));
The second problem I'm having is with the buttons. I have the pins set to INPUT_PULLUP and i understand the active low meaning the button is HIGH when pressed and LOW when released, but what this translates to on my controller is the buttons constantly being pressed until they receive a physical press, then the controller reads them as released. Its pretty much working backwards. And when I take the PULLUP code out, the buttons aren't picked up at all. Im pretty much just looking for someone to point me in the right direction on where to find and put the correct values for the joystick, and some insight on my button situation. Any help would be greatly appreciated and I thank you before hand for your time.
Im currently running the basic example code for the Teensy Joystick, as followed:

Code:
/* Basic USB Joystick Example
Teensy becomes a USB joystick

You must select Joystick from the "Tools > USB Type" menu

Pushbuttons should be connected to digital pins 0 and 1.
Wire each button between the digital pin and ground.
Potentiometers should be connected to analog inputs 0 to 1.

This example code is in the public domain.
*/

void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
}

void loop() {
  // read analog inputs and set X-Y position
  Joystick.X(analogRead(0));
  Joystick.Y(analogRead(1));

  // read the digital inputs and set the buttons
  Joystick.button(1, digitalRead(0));
  Joystick.button(2, digitalRead(1));

  // a brief delay, so this runs 20 times per second
  delay(50);
}
 
With your wiring, where one side of the button is to ground and the other goes to an pin using INPUT_PULLUP,

Then the value you read with digitalRead will be HIGH when the button is not pressed (as the pin will be high due to the input Pull up), and when you press the button, the button will cause the IO pin to go to ground and as such the digitalRead will be LOW.

Note: I am assuming your buttons are Normally Open buttons(NO) (The two pins are not tied to each other unless the button is pressed). There are some buttons which are know as Normally closed(NC) (pins are tied to each other, unless the button is pressed. If the buttons are NC, then the states will be opposite.

With Joystick values, I have usually found it best to experiment some. I usually run some code that keeps the min and max values and print them out for each axis, as to get the actual ranges of the values to use in mapping... I have also found that not all joysticks center properly, that is the position where you release the joystick will not give the exact half way value between min and max... So I actually use my calibration code to give me 3 values, min, center, max and then I do the mapping in two steps, something like:
Code:
if (value < center)
    return (map (value, min, center, 0, 512));
else
    return (map(value, center, max, 512, 1023));
 
Status
Not open for further replies.
Back
Top