Help needed with code please

Status
Not open for further replies.

Glenn

Member
I'm trying to create a simple 2 axis joystick using my Teensy 3.1 and a Arduino Compatible X and Y Axis Joystick Module (image below). I have the GND connected to the AGND, the +5V connected to the Vin (also tried 3.3V), the VRX connected to A0 and the VRY connected to A3. I have the Teensy set to keyboard + mouse + joystick and I'm using the following code which is the basic USB joystick example.

Code:
void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
}

void loop() {
  Joystick.X(analogRead(0));
  Joystick.Y(analogRead(3));

  // a brief delay, so this runs 20 times per second
  delay(50);
}

Windows 10 detects the joystick successfully in Game Controllers (joy.cpl) but when I go into properties to check the operation the + symbol in the X Axis / Y Axis box is jumping all over the place. Moving the pot joystick doesn't seem to make much difference. Can anyone see what I might be doing wrong? Is there anything I need to add or change for the analogRead command?

I really appreciate any help I can get. Thanks
 
Would suggest going to examples->teensy->tutorial4 and look at the analog input example with a joystick input wired on pin 14/A0 and it's other two inputs to 3.3V and Agnd/gnd (doesn't really matter in this basic case

If that's not working try changing the analogRead(0) to analogRead(A0) since I think there is some delta around digital 0 and analog 0 along the way. And use INPUT not INPUT_PULLUP unless you actually want to bias high. It's possible you nuked the input pins connecting to 5V given they are 3.3V chips, though the 5V tolereance circuitry hopefully did it's job.
 
It would help to see your actual wiring. Also do you have a volt meter? Sometimes good to try to see if your voltage is changing on the pin connected to A0 when you turn the knob...

Also not sure what the pinMode statements are for?
As pinMode(0, ...) and analogRead(0) are not the same pins. analogRead of 0 will read from A0 which is digital pin 14...

Note: analogRead(0) and analogRead(A0) will read from the same pin, where as
digitalRead(0) and digitalRead(A0) will read from two different pins....
 
I have the GND connected to the AGND, the +5V connected to the Vin (also tried 3.3V), the VRX connected to A0 and the VRY connected to A3.

Maybe post a photo of the wiring? This description sounds good, but the problem you've described really sounds like the wires may be connected to the wrong pins.
 
Hey guys, thanks for your replies and suggestions they were really appreciated. I actually found I had an A-PAC controller from an old project so I am using that instead of the Teensy. I have another plan to turn the Teensy into a three directional push button keyboard device so that I can control pc games. Thanks again for your help.
 
Status
Not open for further replies.
Back
Top