Teensy and Analog Stick works in Windows but not in game

Status
Not open for further replies.

laughingloud

New member
Hello, I am new to Teensy and coding in general. I am replacing a dpad with a analog stick on a gamepad. Just trying to get one Analog stick to work in game. I used the Teensy joystick example and got it working in the windows game controller properties. It also works on a html gamepad test site but the game doesn't recognize it. It shows up as a Keyboard/Mouse/Joystick under game controllers. I would like for it to act as the left analog stick like the xbox controller on Windows. I appreciate any help. I tried searching but every similar project seemed too advanced and not the same problem I was having.

*Edit I wired the analog signals to 8 and 9
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(8));
  Joystick.Y(analogRead(9));

  // 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);
}
 
Last edited:
I found an xinput library. I got the game to recognize my device but its just constantly cycles the analog stick.
I modified this example to only include the left thumbstick but not sure how to get it to recognize the analog wires 8 and 9 and the one digital one. Trying to get a hold of the guy that made the tutorial but there's no direct email. Only youtube and twitter.
Code:
/*
 *  Project     Arduino XInput Library
 *  @author     David Madison
 *  @link       github.com/dmadison/ArduinoXInput
 *  @license    MIT - Copyright (c) 2019 David Madison
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *  Example:      SimulateAll
 *  Description:  Automatically activate all possible XInput controls.
 *                Useful to test that everything is functioning properly.
 * 
 *  WARNING: This will spam inputs! Ground pin '0' (RX) to stop.
 *
 */

#include <XInput.h>
#include "Joystick.h"
// Config Settings
const unsigned long CycleTime = 5000;  // ms
const int SafetyPin = 0;  // Ground this pin to prevent inputs



// Joystick Setup
const int JoyMax = 32767;  // int16_t max
const double angle_precision = (2 * PI) / (CycleTime / 4);  // 4 because 250 Hz update rate
double angle = 0.0;

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
	XInput.setAutoSend(false);  // Wait for all controls before sending

	XInput.begin();
}

void loop() {

  // read analog inputs and set X-Y position
  Joystick.X(analogRead(9));
  Joystick.Y(analogRead(8));

  // 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);
}


  
	if (digitalRead(SafetyPin) == LOW) {
		return;
	}

	unsigned long t = millis();  // Get timestamp for comparison

	

	// Calculate joystick x/y values using trig
	int axis_x = sin(angle) * JoyMax;
	int axis_y = cos(angle) * JoyMax;

	angle += angle_precision;
	if (angle >= 360) {
		angle -= 360;
	}

	XInput.setJoystick(JOY_LEFT, axis_x, axis_y);  // Clockwise
	

	// Send values to PC
	XInput.send();
}
 
Last edited:
Code:
/*
 *  Project     Arduino XInput Library
 *  @author     David Madison
 *  @link       github.com/dmadison/ArduinoXInput
 *  @license    MIT - Copyright (c) 2019 David Madison
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *  Example:      GamepadPins
 *  Description:  Uses all of the available pin inputs to build a 'complete'
 *                Xbox gamepad, with both analog joysticks, both triggers,
 *                and all of the main buttons.
 *
 *                * Joysticks should be your typical 10k dual potentiometers.
 *                * Triggers can be either analog (pots) or digital (buttons).
 *                  Set the 'TriggerButtons' variable to change between the two.
 *                * Buttons use the internal pull-ups and should be connected
 *                  directly to ground.
 *
 *                These pins are designed around the Leonardo's layout. You
 *                may need to change the pin numbers if you're using a
 *                different board type
 *
 */

#include <XInput.h>

// Setup
const boolean UseLeftJoystick   = false;  // set to true to enable left joystick
const boolean InvertLeftYAxis   = false;  // set to true to use inverted left joy Y


const int ADC_Max = 1023;  // 10 bit

// Joystick Pins
const int Pin_LeftJoyX  = 9;
const int Pin_LeftJoyY  = 8;




void setup() {
	

	XInput.setJoystickRange(0, ADC_Max);  // Set joystick range to the ADC
	XInput.setAutoSend(false);  // Wait for all controls before sending

	XInput.begin();
}

void loop() {
	// Read pin values and store in variables
	// (Note the "!" to invert the state, because LOW = pressed)
	

	
	// Set left joystick
	if (UseLeftJoystick == true) {
		int leftJoyX = analogRead(Pin_LeftJoyX);
		int leftJoyY = analogRead(Pin_LeftJoyY);

		// White lie here... most generic joysticks are typically
		// inverted by default. If the "Invert" variable is false
		// then we need to do this transformation.
		if (InvertLeftYAxis == false) {
			leftJoyY = ADC_Max - leftJoyY;
		}

		XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
	}

	

	// Send control data to the computer
	XInput.send();
}
I was using the wrong example. I got my pc to recognize the teensy as a xbox 360 controller using this other example but the analog stick doesn't do anything now.
 
Status
Not open for further replies.
Back
Top