Using pins for digital and analog simultaneously on arduino micro

Hi! I am using an arduino micro to build essentially a button box with analogs rockers, buttons and rotary encoders. I having trouble right now, because I need 8 digital pins for the rotaries, 16 buttons and all 5 analog pins. This means the analogs the analog pins will have to be used as analog and digital simultaneously. I am using USB game controller software on Microsoft.

I have gotten it to work partially. The problem I have run into is when I use the analogs, they map correctly but also trigger a button press. Has anyone run into this problem? Is there a way to stop this from happening? The follow code doesn't include the rotaries.

#include <Keypad.h>
#include <Joystick.h>

Joystick_ Joystick(
  JOYSTICK_DEFAULT_REPORT_ID,
  JOYSTICK_TYPE_JOYSTICK,
  24, 0,                // Button Count, Hat Switch Count
  true, true, true,    // X and Y (joystick), and Z Axis
  true, true, true,    // Rx, Ry, or Rz
  true, false,         // Rudder or throttle
  false, false, false); // accelerator, brake, steering


// define axis values
int xAxis_ = 0;
int yAxis_ = 0;
int zAxis_ = 0;
int rxAxis_ = 0;
int ryAxis_ = 0;    
int rzAxis_ = 0;    

bool xAxisChanged = true; 
bool yAxisChanged = true;
bool ZAxisChanged = true;
bool RxAxisChanged = true;
bool RyAxisChanged = true;
bool RzAxisChanged = true;

// dead zone for analog read, used over the 0-1023 input range
const int axisDeadZone = 30; // ~5% This is for spring rockers with deadzones at center
const int DialDeadZone = 100;  //This is rising curve filter for dials (beause of getting grounding noise)
const int PotDeadZone = 10;  // We were't getting grounding noise on this pin so can be higher resolution
// initial value read for each analog axis
int axisInit = 514;

const bool initAutoSendState = true; 

void setup() {
  // put your setup code here, to run once:
   // put your setup code here, to run once:
  Serial.begin(9600);
  
  // initialize btn pins
  pinMode(8, 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);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);

  Joystick.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  buttons();
  //analogs();
}

void buttons() {
  // Read pin values 

  int pinCount = 8;   // start count for while loop, represents pins on arduino, first btn pin will be pin 8
  int btnCount = 0;   // start count for while loop, represents btn number on usb tester software, btn 0 represents btn 1 on game controller
  int buttonState = 0;    // define variable for button state, this will be the digital value read from the arduino pins
  while (pinCount < 24) {   // while loop, uses pin count as parameter, pinCount will always be from 8 to 23
    if (pinCount == 11) {
      digitalWrite(pinCount, HIGH);   // solved the problem with digital pin 17 (SS) being stuck on LOW 
      buttonState = digitalRead(pinCount);    // digital read of arduino pin    
      if (buttonState == LOW) {   // if button is pressed for given pin, buttonState == LOW
        Joystick.pressButton(btnCount);   // presses button for give place on usb game controller      
      }
      else {    // if button is not pressed
        Joystick.releaseButton(btnCount);   // releases button after it has been pressed
        analogs();
      }

      btnCount = btnCount + 1;    // adds one to btnCount 
      pinCount = pinCount + 1;    // adds one to pinCount, when pinCount exceeds 23 while loop will terminate

    }
    else{
      digitalWrite(pinCount, HIGH);   // solved the problem with digital pin 17 (SS) being stuck on LOW 
      buttonState = digitalRead(pinCount);    // digital read of arduino pin    
      if (buttonState == LOW) {   // if button is pressed for given pin, buttonState == LOW
        Joystick.pressButton(btnCount);   // presses button for give place on usb game controller       
      }
      else {    // if button is not pressed
        Joystick.releaseButton(btnCount);   // releases button after it has been pressed
      }
      btnCount = btnCount + 1;    // adds one to btnCount 
      pinCount = pinCount + 1;    // adds one to pinCount, when pinCount exceeds 23 while loop will terminate
    }
  }

  delay(50);
}

void analogs() {
  // Read analog values
    // Reads analog values from variable an1 (pin A0), maps onto usb game controller
    
    xAxis_ =  analogRead(A0);
    xAxis_ = map(xAxis_,100,925,0,1023);
    Joystick.setXAxis(xAxis_); 


    // Reads analog values from variable an2 (pin A1), maps onto usb game controller
    yAxis_ = analogRead(A1);
    yAxis_ = map(yAxis_,100,925,0,1023);
    Joystick.setYAxis(yAxis_);  

    // Reads analog value from variable an3 (pin A2), maps onto usb game controller
    zAxis_ = analogRead(A2);
    zAxis_ = map(zAxis_,100,925,0,1023);
    Joystick.setZAxis(zAxis_);  

    // Reads analog value from variable an4 (pin A3), maps onto usb game controller
    rxAxis_ = analogRead(A3);
    rxAxis_ = map(rxAxis_,100,925,0,1023);
    Joystick.setRxAxis(rxAxis_);  

    // Reads analog value from variable an5 (pin A4), maps onto usb game controller
    ryAxis_ = analogRead(A4);
    ryAxis_ = map(ryAxis_,100,925,0,1023);
    Joystick.setRyAxis(ryAxis_);  

    // Reads analog value from variable an6 (pin A5), maps onto usb game controller
    rzAxis_ = analogRead(A5);
    rzAxis_ = map(rzAxis_,100,925,0,1023);
    Joystick.setRzAxis(rzAxis_);  
  
  delay(50);

}

type or paste code here

type or paste code here

Simple answer: you can't.

Could you show a schematic diagram of your project?

Welcome to the forum

Why did you start a topic in the Uncategorised category of the forum when its description explicitly tells you not to ?

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

Honestly, I don't have one. Sorry!

I experimented a bit with doing a button matrix but unfortunately my assignment doesn't work with that. It is being used to check game controllers so all the functions are connected to the same ground and can't be split up. I now have two cables connected to each analog pin, one using digital and one using analog.

Obviously this is not ideal. Is there a way to make the functions alternate maybe? or are they always going to be running over eachother?

Not without additional hardware. And even then, it's a roundabout way with very little reason to go there.

One way is to make this work by fixing your code and hardware so that it does work.

Another way is to use a GPIO expander (e.g. MCP23017) to get some more digital IOs so you can remove the digital inputs from your analog pins.

Will look into the GPIO expander, thank you. Was hoping there was a way to fix through code, but still really helpful!

Its almost always useful to show how your equipment is connected (ok except for a Fritzy)

Sorry; while GPIO stands for "general purpose input/output", it doesn't imply "multiple functions simultaneously"! One pin = one function. There are some ways around this, but they don't apply in your case and/or will be more convoluted than the obvious solution of a GPIO expander (or a board/controller with more GPIO's to begin with).

You could also use an external ADC to get more analog inputs so you can use the analog inputs on your controller for digital signals, but that's (1) conceptually the same as the GPIO expander, but (2) generally more expensive, especially if you need >4 analog inputs.

Was your reply to please provide a schematic.
If you haven't got one then how do you know how to wire it up? Rhetorical question because you can't.

you need to draw one. Without it no one can give you any proper help. Your choice.

If you are wanting to use an analog input for measuring a voltage or as well as a digital you can feed the analog source via a diode and then a potential divider . Set the divider such that the max voltage to the analog is say 2.5 volts .
Take the digital signal through a diode to the analog input directly .

So… anything upto. 2.5 volts is the analog , anything say about 3.5 volts must be from the digital signal .
Obviously can’t be present at the same time