I have a teensy 4.0 with the USB mode set to serial/mouse/keyboard/joystick, and loaded with this example code using the arduino IDE. If I connect a 10k potentiometer between the 3V pin and pin 14 (Analog pin 0), and look at the test dialog on windows, the X axis jumps from the far left to the far right and stays there regardless of the position of the potentiometer. If I then disconnect the potentiometer, the X axis remains at the far right position.
If I add a serial print statement to see the actual values, on power up without the potentiometer it reads 6, when I connect that the value varies between 1017 and 1023 depending on position. If I disconnect the potentiometer, the value goes to 1023 and stays there.
1) What am I missing or doing wrong to not get a full range of values?
2) Why does the Teensy read a high value when the pin is disconnected instead of a low value?
/* 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);
}
If I add a serial print statement to see the actual values, on power up without the potentiometer it reads 6, when I connect that the value varies between 1017 and 1023 depending on position. If I disconnect the potentiometer, the value goes to 1023 and stays there.
1) What am I missing or doing wrong to not get a full range of values?
2) Why does the Teensy read a high value when the pin is disconnected instead of a low value?
/* 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);
}