Code for joystick. help needed

Status
Not open for further replies.

elvas

Member
Can Someone help me with code for a single analog joystick (X and Y axis) and 16 buttons please?
It's for teensy 3.2 so was hoping to have 16 buttons on pins 0-15 and analog joystick on A2 and A3
teensy32_front_pinout.png
I'm new to all this and I'm trying as hard as I can with no luck.
Any help greatly appreciated.
Thanks
 
Perhaps start with File > Examples > Teensy > USB_Joystick > Buttons.

That should get the first 10 buttons working instantly. Maybe you can extend the code for 6 more?

Maybe after that you could use analogRead() and extend it to read the axes? If you've never used analogRead before, start with this tutorial, or the prior tutorial if you're not familiar with how to view printing in the Arduino Serial Monitor.
 
Perhaps start with File > Examples > Teensy > USB_Joystick > Buttons.

Thanks for advice. I'm not good with codes as never done anything with them at all.
What I actually did was I used > Examples > Teensy > USB_Joystick > Complete which gave me 16 buttons and 6 axis. managed to get it down to 2 axis by simply removing from the code remaining ones. Also managed to change number of buttons by simply replacing number of them in code to 18. There was also something called hatswitch which was causing my xbox controller emulator to show buttons going in a loop one after another... Quick search thru code took me to some value stating "loop" and by quick deduction it made me believe that this is what causing it - deleted entire function and my joystick finally works as it should!!!!!!!!!!!!!

There's only one thing that I'm trying to sort out now but I'm failing big time. I'm not asking for someone to do the job for me as I'm really trying to understand what's what but with no knowledge and experience I'm struggling badly.

Button No.14 is showing to be constantly ON - no shortcuts or incorrectly connected wires.Untitled.png

Here's the code and I seriously can't find reason why is it happening. As I said I have removed few lines from oryginal one and reasigned analog pins but Button No.14 had same status even on example code before i touched it...


/* Complete USB Joystick Example
Teensy becomes a USB joystick with 16 or 32 buttons and 6 axis input

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

Pushbuttons should be connected between the digital pins and ground.
Potentiometers should be connected to analog inputs 0 to 5.

This example code is in the public domain.
*/

// Configure the number of buttons. Be careful not
// to use a pin for both a digital button and analog
// axis. The pullup resistor will interfere with
// the analog voltage.
const int numButtons = 18; // 16 for Teensy, 32 for Teensy++

void setup() {
// you can print to the serial monitor while the joystick is active!
Serial.begin(9600);
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
for (int i=0; i<numButtons; i++) {
pinMode(i, INPUT_PULLUP);
}
Serial.println("Begin Complete Joystick Test");
}

byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle=0;

void loop() {
// read 6 analog inputs and use them for the joystick axis
Joystick.X(analogRead(4));
Joystick.Y(analogRead(5));

// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(i)) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons = 1;
}
Joystick.button(i + 1, allButtons);
}


// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();

// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons != prevButtons) anyChange = true;
prevButtons = allButtons;
}

// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons, DEC);
}
Serial.println();
}

// a brief delay, so this runs "only" 200 times per second
delay(5);
}


Can You see anything in that code that would make that Button no 14 being ON ? Is it because it's connected to digital pin No13 which is a LED one by default?
Thank You
 
Last edited:
Can You see anything in that code that would make that Button no 14 being ON ? Is it because it's connected to digital pin No13 which is a LED one by default?
Thank You

Bingo! The way to get around that is here. Just create an array with all the pin numbers that you want to use (avoid pin 13) and loop through that array instead of the pin numbers directly.

A different method would be to change
Code:
for (int i=0; i<numButtons; i++) {
pinMode(i, INPUT_PULLUP);
And
Code:
for (int i=0; i<numButtons; i++) {
if (digitalRead(i)) {
to
Code:
for (int i=0; i<numButtons; i++) {
pinMode(i < LED_BUILTIN ? i : i+1, INPUT_PULLUP);
And
Code:
for (int i=0; i<numButtons; i++) {
if (digitalRead(i < LED_BUILTIN ? i : i+1)) {
That way all of your arrays stay the same and you don't use any additional memory. You would use pins 0-12 and 14-16, skipping over 13.
The constant LED_BUILTIN is usually defined as 13, but writing it this way will work on boards that have the LED on any pin. Perhaps this should be part of an example sketch. Shows a perfect reason to use both the ternary operator and a defined constant. Something many beginners struggle with.
In order to function, the cathode of the LED on that pin is connected to ground. When INPUT_PULLUP is set on that pin, it forward biases the diode which puts the pin in the same state as a pressed button; because the direct path through the diode is lower resistance than through the pullup resistor.
 
Last edited:
I have a similar question related to a joystick. The stick has two pots for X,Y (probably similar to the one above).
But what I need to do is oposite: send values, "resistances" to the X,Y axis because they're being read by another device. In other words, replace my finger with a Teensy.
Assuming that the joystick pot is sent 3.3V max (full stick one way), 1.6V (guessing center), 0V (full stick opposite), what would be the best way to send these values to the joystick signal pin to replicate finger movement without the pots ever actually moving?
 
Status
Not open for further replies.
Back
Top