Analog Stick

Arduino/Teensy software/code questions and discussion
Post Reply
maxd
Posts: 7
Joined: Tue Nov 29, 2016 2:49 pm
Been thanked: 1 time

Analog Stick

Post by maxd » Tue Nov 29, 2016 2:58 pm

Hi, I want to include an analog stick to my project but need help with the teensy code. Does anyone know how to reverse directions. Basically my up=down and my down = up. Not sure how to fix this.

User avatar
wermy
Site Admin
Posts: 1346
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 620 times
Been thanked: 1322 times
Contact:

Re: Analog Stick

Post by wermy » Tue Nov 29, 2016 3:12 pm

maxd wrote:Hi, I want to include an analog stick to my project but need help with the teensy code. Does anyone know how to reverse directions. Basically my up=down and my down = up. Not sure how to fix this.
Sure, could you post some code? Probably as simple as multiplying by -1 somewhere. :)
ImageImageImageImage

maxd
Posts: 7
Joined: Tue Nov 29, 2016 2:49 pm
Been thanked: 1 time

Re: Analog Stick

Post by maxd » Tue Nov 29, 2016 6:44 pm

Thank you for looking at this for me. Here is the piece of code that I am using to manipulate the analog joystick.

/* 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.
*/


#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10);
Bounce button6 = Bounce(6, 10);
Bounce button7 = Bounce(7, 10);
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);
Bounce button10 = Bounce(10, 10);
Bounce button11 = Bounce(11, 10);
Bounce button12 = Bounce(12, 10);
Bounce button13 = Bounce(13, 10);

int axisX;
int axisY;

void setup() {


//Serial.begin(9600);

// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);

}

void loop() {
// read analog inputs and set X-Y position

axisX = analogRead(0);

axisY = analogRead(1);
// If your analog joystick is unprecise or uncalibrated try reading the max and min values, and then type them down below.
// The first line is mapping the X axis. Type the min X value where it says 210, and type your max X value where it says 880.
// Do the Same for the Y axis

Joystick.X(map(axisX, 210, 516, 0, 1023));
Joystick.Y(map(axisY, 18, 516, 0, 1023));

Joystick.send_now();

// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();
button12.update();
button13.update();

// Check each button for "falling" edge.
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Joystick.button(1, 1);
}
if (button1.fallingEdge()) {
Joystick.button(2, 1);
}
if (button2.fallingEdge()) {
Joystick.button(3, 1);
}
if (button3.fallingEdge()) {
Joystick.button(4, 1);
}
if (button4.fallingEdge()) {
Joystick.button(5, 1);
}
if (button5.fallingEdge()) {
Joystick.button(6, 1);
}
if (button6.fallingEdge()) {
Joystick.button(7, 1);
}
if (button7.fallingEdge()) {
Joystick.button(8, 1);
}
if (button8.fallingEdge()) {
Joystick.button(9, 1);
}
if (button9.fallingEdge()) {
Joystick.button(10, 1);
}
if (button10.fallingEdge()) {
Joystick.button(11, 1);
}
if (button11.fallingEdge()) {
Joystick.button(12, 1);
}
if (button12.fallingEdge()) {
Joystick.button(13, 1);
}
if (button13.fallingEdge()) {
Joystick.button(14, 1);
}

// Check each button for "rising" edge
// Update the Joystick buttons only upon changes.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button0.risingEdge()) {
Joystick.button(1, 0);
}
if (button1.risingEdge()) {
Joystick.button(2, 0);
}
if (button2.risingEdge()) {
Joystick.button(3, 0);
}
if (button3.risingEdge()) {
Joystick.button(4, 0);
}
if (button4.risingEdge()) {
Joystick.button(5, 0);
}
if (button5.risingEdge()) {
Joystick.button(6, 0);
}
if (button6.risingEdge()) {
Joystick.button(7, 0);
}
if (button7.risingEdge()) {
Joystick.button(8, 0);
}
if (button8.risingEdge()) {
Joystick.button(9, 0);
}
if (button9.risingEdge()) {
Joystick.button(10, 0);
}
if (button10.risingEdge()) {
Joystick.button(11, 0);
}
if (button11.risingEdge()) {
Joystick.button(12, 0);
}
if (button12.risingEdge()) {
Joystick.button(13, 0);
}
if (button13.risingEdge()) {
Joystick.button(14, 0);
}

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

//Joystick.send_now();


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

User avatar
wermy
Site Admin
Posts: 1346
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 620 times
Been thanked: 1322 times
Contact:

Re: Analog Stick

Post by wermy » Tue Nov 29, 2016 9:04 pm

Alright, try replacing this:
Joystick.X(map(axisX, 210, 516, 0, 1023));
Joystick.Y(map(axisY, 18, 516, 0, 1023));

With this:

Joystick.X(-map(axisX, 210, 516, 0, 1023));
Joystick.Y(-map(axisY, 18, 516, 0, 1023));
ImageImageImageImage

maxd
Posts: 7
Joined: Tue Nov 29, 2016 2:49 pm
Been thanked: 1 time

Re: Analog Stick

Post by maxd » Tue Nov 29, 2016 9:28 pm

Will try that out. Thank you!!!

User avatar
wermy
Site Admin
Posts: 1346
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 620 times
Been thanked: 1322 times
Contact:

Re: Analog Stick

Post by wermy » Tue Nov 29, 2016 9:54 pm

maxd wrote:Will try that out. Thank you!!!
Actually don't thank me just yet, I just realized the call to Joystick.X()/Y() is expecting values between 0-1023, so adding a negative sign in front the value you pass it definitely won't do what you want. Also I realized you said just the y axis is reversed. Sorry about that! :]

Try this:
Joystick.Y(1023 - map(axisY, 18, 516, 0, 1023));
ImageImageImageImage

maxd
Posts: 7
Joined: Tue Nov 29, 2016 2:49 pm
Been thanked: 1 time

Re: Analog Stick

Post by maxd » Fri Dec 02, 2016 6:01 am

Thank you again. The code update worked. I wish I understood why. Would you know of any documentation that can help enlighten me?

User avatar
wermy
Site Admin
Posts: 1346
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 620 times
Been thanked: 1322 times
Contact:

Re: Analog Stick

Post by wermy » Fri Dec 02, 2016 1:27 pm

maxd wrote:Thank you again. The code update worked. I wish I understood why. Would you know of any documentation that can help enlighten me?
I googled the documentation for that Arduino library to see what the Joystick.X()/Y() functions were actually expecting. You can usually find documentation pretty easily for most open-source (and most Arduino) things. :)

As for the fix, it's being fed a value between 0 - 1023, but you said it was reversed, so it needed to send 1023 when it would have otherwise sent 0, and 0 when it would have otherwise sent 1023. Adding the "1023 - " in that function call accomplishes this: while it was sending 0 before, now it sends 1023 - 0, and when it was sending 1023 before, now we're sending 1023 - 1023. So it flips it. :)

Hope this helps!
ImageImageImageImage

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest