RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    [SOLVED] N64 Analog Joystick Issue with Teensy LC

    Scheduled Pinned Locked Moved Help and Support
    retropien64n64 controllerjoystickanalog stick
    17 Posts 3 Posters 1.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Steven33S
      Steven33
      last edited by Steven33

      Hello,

      I have built a handheld raspberry pi 3b portable with Retropie on it. I am using a Teensy LC as a custom USB HID device. I am able to configure the controller just fine and navigate the Retropie menu with the d-pad and/or both analog sticks. However, when I enter a n64 game, the analog sticks are wack for a lack of a better word. Sometimes they go the correct direction, but sometimes they go the opposite. I have tried messing around with the Analog Deadzone and AnalogPeak values with no luck. If someone could help me or lead me in the right direction that would be great!

      Video clips that better show my problem.

      TeensyLC Code:

      /* Buttons to USB Joystick Example
      
         You must select Joystick from the "Tools > USB Type" menu
      
         This example code is in the public domain.
      */
      
      #include <Bounce.h>
      
      #define ANALOG_CENTER 512
      
      // Create Bounce objects for each button.  The Bounce object
      // automatically deals with contact chatter or "bounce", and
      // it makes detecting changes very simple.
      // 10 = 10 ms debounce time which is appropriate for most mechanical pushbuttons
      
      // Action buttons 
      Bounce button0 = Bounce(13, 10);
      Bounce button1 = Bounce(14, 10);  
      Bounce button2 = Bounce(15, 10); 
      Bounce button3 = Bounce(16, 10); 
      
      // Select
      Bounce button4 = Bounce(17, 10);
      
      // D-Pad
      Bounce button5 = Bounce(0, 10);
      Bounce button6 = Bounce(1, 10);  
      Bounce button7 = Bounce(2, 10);  
      Bounce button8 = Bounce(3, 10);  
      
      // Start
      Bounce button9 = Bounce(4, 10);
      
      // Shoulder buttons 
      Bounce button10 = Bounce(9, 10);
      Bounce button11 = Bounce(10, 10);  
      Bounce button12 = Bounce(11, 10); 
      Bounce button13 = Bounce(12, 10); 
      
      void setup() {
        // 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(9, INPUT_PULLUP);
        pinMode(10, INPUT_PULLUP);
        pinMode(11, INPUT_PULLUP);
        pinMode(12, INPUT_PULLUP);
        
        pinMode(13, INPUT_PULLUP);
        pinMode(14, INPUT_PULLUP);
        pinMode(15, INPUT_PULLUP);
        pinMode(16, INPUT_PULLUP);
        pinMode(17, INPUT_PULLUP);
      
      }
      
      void loop() {
        // 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.
      
        // Right Analogue Stick
        Joystick.X(((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
        Joystick.Y(((analogRead(19)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
      
        // Left Analogue Stick
        Joystick.Z(((analogRead(22)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
        Joystick.Zrotate(((analogRead(23)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
        
        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);
        }
      }
      
      1 Reply Last reply Reply Quote 0
      • G
        grant2258 Banned
        last edited by grant2258

        have you looked at the raw values? what is the real center value from you teensy?

        Once you know this value change the define to that.

        checkout this code here will get you in the right direction

        https://github.com/tuffrabit/teensy-stuff/blob/joystick-calibration/joystick_deadzoner/Joystick_Deadzoner.ino

        What this code does if you press the analog button in for a fixed amount of time it will calibrate the joystick for you and save to eprom. It will run in this mode until you turn it off.

        Thats not my code by the way its just a way to automate it you can just check the value yourself when its centered

        1 Reply Last reply Reply Quote 0
        • Steven33S
          Steven33
          last edited by

          So would I incorporate that code into the one I have? I'm not really that familiar with Arduino coding, I'm more of a python kind of guy. I did check the values of my sticks and the centers seem to change a bit, anywhere between 495 - 530. Shouldn't the Analog Deadzone value help fix that problem tho? What I didn't understand is that when navigating the Retorpie menu, the joysticks work completely fine but once in a n64 game, they are wack.

          1 Reply Last reply Reply Quote 0
          • G
            grant2258 Banned
            last edited by grant2258

            Well The retropie menu doesnt use analog. It usues digital to it probably wanting at least a 50% pull before it registers. When real analog is polled it wish up as jumpy.

            I just finished work I havent compiled this fix any errors if there is any. its trivial to add the other axis only done the x and y to get you on the right path. Hope it helps you out

            /* Buttons to USB Joystick Example
            
               You must select Joystick from the "Tools > USB Type" menu
            
               This example code is in the public domain.
            */
            
            #include <Bounce.h>
            
            #define ANALOG_CENTER 512
            int Xstick;
            int Ystick;
            int upperBound=530;
            int lowerBound=495;
            
            // Create Bounce objects for each button.  The Bounce object
            // automatically deals with contact chatter or "bounce", and
            // it makes detecting changes very simple.
            // 10 = 10 ms debounce time which is appropriate for most mechanical pushbuttons
            
            // Action buttons 
            Bounce button0 = Bounce(13, 10);
            Bounce button1 = Bounce(14, 10);  
            Bounce button2 = Bounce(15, 10); 
            Bounce button3 = Bounce(16, 10); 
            
            // Select
            Bounce button4 = Bounce(17, 10);
            
            // D-Pad
            Bounce button5 = Bounce(0, 10);
            Bounce button6 = Bounce(1, 10);  
            Bounce button7 = Bounce(2, 10);  
            Bounce button8 = Bounce(3, 10);  
            
            // Start
            Bounce button9 = Bounce(4, 10);
            
            // Shoulder buttons 
            Bounce button10 = Bounce(9, 10);
            Bounce button11 = Bounce(10, 10);  
            Bounce button12 = Bounce(11, 10); 
            Bounce button13 = Bounce(12, 10); 
            
            void setup() {
              // 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(9, INPUT_PULLUP);
              pinMode(10, INPUT_PULLUP);
              pinMode(11, INPUT_PULLUP);
              pinMode(12, INPUT_PULLUP);
              
              pinMode(13, INPUT_PULLUP);
              pinMode(14, INPUT_PULLUP);
              pinMode(15, INPUT_PULLUP);
              pinMode(16, INPUT_PULLUP);
              pinMode(17, INPUT_PULLUP);
            
            }
            
            void loop() {
              // 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.
            
              // Right Analogue Stick
              Xstick = analogRead(18);
              Ystick = analogRead(19);
            
              if ((Xstick > 512 && Xstick <= upperBound) || (Xstick < 512 && Xstick >= lowerBound)) {
                  Xstick = 512;
                }
              
                if ((Ystick > 512 && Ystick <= upperBound) || (Ystick < 512 && Ystick >= lowerBound)) {
                  Ystick = 512;
                }
              Joystick.X(Xstick);
              Joystick.Y(Ystick);
            	
            //  Joystick.X(((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
            //  Joystick.Y(((analogRead(19)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
            
              // Left Analogue Stick
              Joystick.Z(((analogRead(22)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
              Joystick.Zrotate(((analogRead(23)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
            
              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);
              }
            }
            
            1 Reply Last reply Reply Quote 1
            • G
              grant2258 Banned
              last edited by

              @grant2258 said in N64 Analog Joystick Issue with Teensy LC:

              Joystick.X(((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);

              you might want to change to your original code if you to change Joystick.X(Xstick); to Joystick.X( ((Xstick - ANALOG_CENTER) * 1.6) + ANALOG_CENTER);

              1 Reply Last reply Reply Quote 0
              • Steven33S
                Steven33
                last edited by

                Thank you so much! This works so much better, however, I tried to add the multiplier I had to the analog read ((((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER)). This made the controls back to what it was. I only did this tho since I wasn't able to configure the right joystick x-axis unless I increased the sensitivity. Is there a way to set the range of the analog input or something I should do differently in the Teensy code?

                New Code:

                /* Buttons to USB Joystick Example
                
                   You must select Joystick from the "Tools > USB Type" menu
                
                   This example code is in the public domain.
                */
                
                #include <Bounce.h>
                
                #define ANALOG_CENTER 512
                int Xstick;
                int Ystick;
                int Zstick;
                int Zstickrotate;
                int upperBound=530;
                int lowerBound=495;
                
                // Create Bounce objects for each button.  The Bounce object
                // automatically deals with contact chatter or "bounce", and
                // it makes detecting changes very simple.
                // 10 = 10 ms debounce time which is appropriate for most mechanical pushbuttons
                
                // Action buttons 
                Bounce button0 = Bounce(13, 10);
                Bounce button1 = Bounce(14, 10);  
                Bounce button2 = Bounce(15, 10); 
                Bounce button3 = Bounce(16, 10); 
                
                // Select
                Bounce button4 = Bounce(17, 10);
                
                // D-Pad
                Bounce button5 = Bounce(0, 10);
                Bounce button6 = Bounce(1, 10);  
                Bounce button7 = Bounce(2, 10);  
                Bounce button8 = Bounce(3, 10);  
                
                // Start
                Bounce button9 = Bounce(4, 10);
                
                // Shoulder buttons 
                Bounce button10 = Bounce(9, 10);
                Bounce button11 = Bounce(10, 10);  
                Bounce button12 = Bounce(11, 10); 
                Bounce button13 = Bounce(12, 10); 
                
                void setup() {
                  // 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(9, INPUT_PULLUP);
                  pinMode(10, INPUT_PULLUP);
                  pinMode(11, INPUT_PULLUP);
                  pinMode(12, INPUT_PULLUP);
                  
                  pinMode(13, INPUT_PULLUP);
                  pinMode(14, INPUT_PULLUP);
                  pinMode(15, INPUT_PULLUP);
                  pinMode(16, INPUT_PULLUP);
                  pinMode(17, INPUT_PULLUP);
                
                }
                
                void loop() {
                  // 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.
                
                  // Right Analogue Stick
                  Xstick = analogRead(18);
                  Ystick = analogRead(19);
                
                  if ((Xstick > 512 && Xstick <= upperBound) || (Xstick < 512 && Xstick >= lowerBound)) {
                      Xstick = 512;
                    }
                  
                    if ((Ystick > 512 && Ystick <= upperBound) || (Ystick < 512 && Ystick >= lowerBound)) {
                      Ystick = 512;
                    }
                  Joystick.X(Xstick);
                  Joystick.Y(Ystick);
                
                  // Left Analogue Stick
                  Zstick = analogRead(22);
                  Zstickrotate = analogRead(23);
                
                  if ((Zstick > 512 && Zstick <= upperBound) || (Zstick < 512 && Zstick >= lowerBound)) {
                      Zstick = 512;
                    }
                  
                    if ((Zstickrotate > 512 && Zstickrotate <= upperBound) || (Zstickrotate < 512 && Zstickrotate >= lowerBound)) {
                      Zstickrotate = 512;
                    }
                  Joystick.Z(Zstick);
                  Joystick.Zrotate(Zstickrotate);
                  
                //  Joystick.X(((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                //  Joystick.Y(((analogRead(19)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                
                
                //  Joystick.Z(((analogRead(22)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                //  Joystick.Zrotate(((analogRead(23)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                
                  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);
                  }
                }
                
                1 Reply Last reply Reply Quote 0
                • G
                  grant2258 Banned
                  last edited by

                  Im not sure what your wanting to do here do you want to scale +- 1024 to +-32678 ?

                  1 Reply Last reply Reply Quote 0
                  • Steven33S
                    Steven33
                    last edited by

                    When I try to configure the analog stick in Retropie, some of the axis doesn't get picked up. However, when I test the joysticks in the terminal, jstest /dev/input/js1, they are showing up. How can change the range of the analog sticks so they can be configured or how can I increase their sensitivity?

                    G 1 Reply Last reply Reply Quote 0
                    • G
                      grant2258 Banned @Steven33
                      last edited by grant2258

                      Just finished work what is the minimum and maximum picked up with jstest for each axis?

                      1 Reply Last reply Reply Quote 0
                      • Steven33S
                        Steven33
                        last edited by Steven33

                        LEFT JOYSTICK (approximate values):

                        • X = 2400, -2400

                        • Y = 2400, -2400

                        RIGH JOYSTICK (approximate values):

                        • X = 2200, -2500

                        • Y = 2400, -2400

                        1 Reply Last reply Reply Quote 0
                        • G
                          grant2258 Banned
                          last edited by

                          I only gave a few Arduinos here they need some extra work to get hid working when i get time ill have a look whats going on. Are them values a standard return ie.
                          Joystick.X(Xstick);

                          1 Reply Last reply Reply Quote 0
                          • Steven33S
                            Steven33
                            last edited by

                            What do you mean a standard return? The values are what it says when I run jstest /dev/input/js1 and move the sticks around. I'm using the new code that I posted yesterday. So its just Joystick.X(Xstick). It seems like the range for joysticks aren't in line with the range Retorpie has. How can I increase the sensitivity of the analog read without messing up the corrections we made to the old code? Before I just did Joystick.X(((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);. However, ANALOG_CENTER equals a fixed value and we know the center values are in a range so when I tried to add it in, the joysticks went back to their messed up their state a couple of days ago.

                            1 Reply Last reply Reply Quote 0
                            • G
                              grant2258 Banned
                              last edited by

                              try this code

                              /* Buttons to USB Joystick Example
                              
                                 You must select Joystick from the "Tools > USB Type" menu
                              
                                 This example code is in the public domain.
                              */
                              
                              #include <Bounce.h>
                              
                              #define ANALOG_CENTER 512
                              int Xstick;
                              int Ystick;
                              int Zstick;
                              int Zstickrotate;
                              int upperBound=530;
                              int lowerBound=495;
                              
                              // Create Bounce objects for each button.  The Bounce object
                              // automatically deals with contact chatter or "bounce", and
                              // it makes detecting changes very simple.
                              // 10 = 10 ms debounce time which is appropriate for most mechanical pushbuttons
                              
                              // Action buttons 
                              Bounce button0 = Bounce(13, 10);
                              Bounce button1 = Bounce(14, 10);  
                              Bounce button2 = Bounce(15, 10); 
                              Bounce button3 = Bounce(16, 10); 
                              
                              // Select
                              Bounce button4 = Bounce(17, 10);
                              
                              // D-Pad
                              Bounce button5 = Bounce(0, 10);
                              Bounce button6 = Bounce(1, 10);  
                              Bounce button7 = Bounce(2, 10);  
                              Bounce button8 = Bounce(3, 10);  
                              
                              // Start
                              Bounce button9 = Bounce(4, 10);
                              
                              // Shoulder buttons 
                              Bounce button10 = Bounce(9, 10);
                              Bounce button11 = Bounce(10, 10);  
                              Bounce button12 = Bounce(11, 10); 
                              Bounce button13 = Bounce(12, 10); 
                              
                              void setup() {
                                // 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(9, INPUT_PULLUP);
                                pinMode(10, INPUT_PULLUP);
                                pinMode(11, INPUT_PULLUP);
                                pinMode(12, INPUT_PULLUP);
                                
                                pinMode(13, INPUT_PULLUP);
                                pinMode(14, INPUT_PULLUP);
                                pinMode(15, INPUT_PULLUP);
                                pinMode(16, INPUT_PULLUP);
                                pinMode(17, INPUT_PULLUP);
                              
                              }
                              
                              void loop() {
                                // 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.
                              
                                // Right Analogue Stick
                                Xstick = analogRead(18);
                                Ystick = analogRead(19);
                              
                                if ((Xstick > 512 && Xstick <= upperBound) || (Xstick < 512 && Xstick >= lowerBound)) {
                                    Xstick = 512;
                                  }
                                
                                  if ((Ystick > 512 && Ystick <= upperBound) || (Ystick < 512 && Ystick >= lowerBound)) {
                                    Ystick = 512;
                                  }
                              
                              Xstick = (Xstick - 511) * 2 + 511;
                               // cap result
                               if (Xstick > 1023)
                               Xstick=1023;
                               if (Xstick < 0)
                               Xstick = 0;
                               
                                Joystick.X(Xstick);
                                Joystick.Y(Ystick);
                              
                                // Left Analogue Stick
                                Zstick = analogRead(22);
                                Zstickrotate = analogRead(23);
                              
                                if ((Zstick > 512 && Zstick <= upperBound) || (Zstick < 512 && Zstick >= lowerBound)) {
                                    Zstick = 512;
                                  }
                                
                                  if ((Zstickrotate > 512 && Zstickrotate <= upperBound) || (Zstickrotate < 512 && Zstickrotate >= lowerBound)) {
                                    Zstickrotate = 512;
                                  }
                                Joystick.Z(Zstick);
                                Joystick.Zrotate(Zstickrotate);
                                
                              //  Joystick.X(((analogRead(18)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                              //  Joystick.Y(((analogRead(19)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                              
                              
                              //  Joystick.Z(((analogRead(22)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                              //  Joystick.Zrotate(((analogRead(23)-ANALOG_CENTER) * 1.6) + ANALOG_CENTER);
                              
                                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);
                                }
                              }
                              
                              1 Reply Last reply Reply Quote 1
                              • G
                                grant2258 Banned
                                last edited by grant2258

                                looked into this a little more seems the zaxis is 8 bits. Im not sure of your exact setup or what lib you are using you would need to check with your lib.

                                https://github.com/NicoHood/HID/blob/efa8d0f16596279d6269982471f55d186449e311/src/HID-APIs/GamepadAPI.h#L86-L93

                                1 Reply Last reply Reply Quote 0
                                • Steven33S
                                  Steven33
                                  last edited by

                                  Well, I tried your code and everything seems to be working good so I'm going to say this fixed the issue. Thank you so much!

                                  1 Reply Last reply Reply Quote 1
                                  • G
                                    grant2258 Banned
                                    last edited by

                                    no problems at all glad it worked out :)

                                    1 Reply Last reply Reply Quote 0
                                    • DTEAMD
                                      DTEAM
                                      last edited by

                                      Simple comment. I had the same issue but my lowerBound had to be fix to value 490. It's important to check the value when your stick go back in place. My left stick showed a value of 491 when It came back from left to center. Now, Everything's working fine

                                      I suggest to use joy.cpl to find It.

                                      https://www.pjrc.com/teensy/td_joystick.html

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post

                                      Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                                      Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.