Go Down

Topic: USB Gamepad with Rotary Encoders help (Read 412 times) previous topic - next topic

hydrolisk1792

Hello, I'm having some trouble here.  I got the rotary encoders to do something in the Windows game controller window, but they just kind of fidget back and forth no matter which direction you go in.  Here is my code:

Code: [Select]

/*
 * Shanoa's Media Remote with Baby Monitor Interface.
 * Electcorp Industries 2018
 */


#define ENCODER_USE_INTERRUPTS
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
#include <Joystick.h>
 
Encoder axisXRotation(1, 0);
Encoder axisYRotation(4, 7);


//Number of Physical Pins that buttons are attached to. Ground out to simulate button press.
const byte buttonArray[] = {2, 5, 6, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() { 
  // Initialization of Buttons with internal pullup resistors
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], INPUT_PULLUP);
    pinMode(1, INPUT_PULLUP);
    pinMode(0, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
    pinMode(7, INPUT_PULLUP);
  }
 
  // Initialize Joystick Library
  Joystick.begin(false);
}

void loop()

//Encoder Knobs
{
  Joystick.setXAxisRotation(180);
  int newX = axisXRotation.read();
  if (newX > 180)
  {
    Joystick.setXAxisRotation(359);
    delay(50);
    axisXRotation.write(180);
  }
  if (newX < 180)
  {
    Joystick.setXAxisRotation(1);
    delay(50);
    axisXRotation.write(180);
  }


  Joystick.setYAxisRotation(180);
  int newY = axisYRotation.read();
  if (newY > 180)
  {
    Joystick.setYAxisRotation(359);
    delay(50);
    axisYRotation.write(180);
  }
  if (newY < 180)
  {
    Joystick.setYAxisRotation(1);
    delay(50);
    axisYRotation.write(180);
  }


  // Read button pin values
  {
  for (byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);     // Read button state
    Joystick.setButton(x, currentButtonState);                  // Set state in joystick
  }

  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}
}


I'm using a pro micro for this and all my buttons work just fine.  What I need the encoders for is to use them as volume and the other as seek back and forth on select media.  I'm doing it all via a game pad because I can take care of all the rest with interfacing it with the select programs and such I would like the buttons and rotary encoder knobs to manipulate.  Any help with this would be greatly appreciated.
I make things do what they were never intended to do :P

hydrolisk1792

I've got one knob to work but not the other.  The knob attached to physical pins 0, and 1 is working just fine.  The second one just fidgets still and now I'm completely lost.  I understand that I need to put rotary encoders on pins with interrupts, and that is what I did, but still stuck back at square one...  I've been working on the code a bit to try and enable interrupts on pins 4, and 7, but no luck.  Here is my updated code for this project.

Code: [Select]

/*
 * Shanoa's Media Remote with Baby Monitor Interface.
 * Electcorp Industries 2018
 */


#define ENCODER_USE_INTERRUPTS
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
#include <Joystick.h>
 
Encoder axisXRotation(1, 0);
Encoder axisYRotation(4, 7);

long lastDebounceTime = 0;
long debounceDelay = 50;
long longpressDelay = 500;


//Number of Physical Pins that buttons are attached to. Ground out to simulate button press.
const byte buttonArray[] = {2, 5, 6, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

//Encoder Pins
const byte interruptPin0 = 0;
const byte interruptPin1 = 1;
const byte interruptPin4 = 4;
const byte interruptPin7 = 7;
volatile byte state = LOW;

void setup()

{

  pinMode(interruptPin0, INPUT_PULLUP);
  pinMode(interruptPin1, INPUT_PULLUP);
  pinMode(interruptPin4, INPUT_PULLUP);
  pinMode(interruptPin7, INPUT_PULLUP);
   
  // Initialization of Buttons with internal pullup resistors
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], INPUT_PULLUP);
  }
 
  // Initialize Joystick Library
  Joystick.begin(false);
}

void loop()


{
{
  // Read button pin values
  {
  for (byte x = 0; x < sizeof(buttonArray); x++)
    {
      byte currentButtonState = !digitalRead(buttonArray[x]);     // Read button state
      Joystick.setButton(x, currentButtonState);                  // Set state in joystick
    }

    Joystick.sendState();               // Send the joystick data
    delay(50);                          // Wait a bit
  }
}
    //Encoder Knobs

{
  Joystick.setXAxisRotation(180);
  int newX = axisXRotation.read();
  if (newX > 180)
  {
    Joystick.setXAxisRotation(359);
    axisXRotation.write(180);
  }
  if (newX < 180)
  {
    Joystick.setXAxisRotation(1);
    axisXRotation.write(180);
  }


  Joystick.setYAxisRotation(180);
  int newY = axisYRotation.read();
  if (newY > 180)
  {
    Joystick.setYAxisRotation(359);
    axisYRotation.write(180);
  }
  if (newY < 180)
  {
    Joystick.setYAxisRotation(1);
    axisYRotation.write(180);
  }
}
}




Please any one, if there is something wrong with my sketch let me know.  I do have physical debouncing circuits as well built on to the boards, and that is what cleared up the first encoder, but not the second one.  And yes, the encoders are cleaned, and I've tried the "bad" one on the working pins, so I already ruled out that it could be a bad knob...
I make things do what they were never intended to do :P

hydrolisk1792

Never mind you all, I sorted out the problem. It was my pin selection.
I make things do what they were never intended to do :P

Go Up