Arduino pro micro + cd74hc4067 Programming and connection

Hello, I hope you are well.

I would like to request your help, I am somewhat new to this Arduino, and there are things I can't understand.

I am making a hotas controller for flight video games (personal use), and I need to separate more inputs through a MUX. The issue is that I don't know how to include it in the code I used without MUX.

I have tried to modify it learning from other codes but really everything works wrong.

I am using Arduino Pro Micro + Mux cd74hc4067

The code without including anything from the MUX is this:

// USB Joystick with hall effect sensors
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//------------------------------------------------------------
 
//change these to define which pins your hall effect sensors or potentiometers are connected.
//to change button connections, scroll down to loop()
#define X_PIN A1
#define Y_PIN A0
#define R_PIN A3
#define T_PIN A2
//change these to change trim and limits. Calibrating your joystick in Windows achieves the same thing
#define X_MIN 0
#define X_MAX 1023
#define X_TRIM 0
#define X_INVERT -1
//to invert an axis, change 1 to -1
#define Y_MIN 0
#define Y_MAX 1023
#define Y_TRIM 0
#define Y_INVERT -1
 
#define R_MIN 0
#define R_MAX 1023
#define R_TRIM 0
#define R_INVERT 1
 
#define T_MIN 0
#define T_MAX 1023
#define T_TRIM 0
#define T_INVERT 1
 
 
#include <Joystick.h>
 
Joystick_ Joystick(0x04,JOYSTICK_TYPE_JOYSTICK,
  7, 0,                  // Button Count, Hat Switch Count
  true, true, false,     // X and Y, but no Z Axis
  false, false, false,   // No Rx, Ry, or Rz
  true, true,            // Yes rudder, yes throttle
  false, false, false);  // No accelerator, brake, or steering
 
void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
 
  // Initialize Joystick Library
  Joystick.begin(false); //false = dont send automatically. We will sendState() at the end of the loop
  Joystick.setXAxisRange(-512, 512);
  Joystick.setYAxisRange(-512, 512);
  Joystick.setRudderRange(-512, 512);
  Joystick.setThrottleRange(-512, 512);
}
 
 
void loop() {
  //read buttons. Change pins and button numbers here, if you want to have different number connected to different pin
  Joystick.setButton(0, !digitalRead(3)); //pin 2 LOW means button 0 PRESSED
  Joystick.setButton(1, !digitalRead(2)); //etc.
  Joystick.setButton(2, !digitalRead(5));
  Joystick.setButton(3, !digitalRead(6));
  Joystick.setButton(4, !digitalRead(7));
  Joystick.setButton(5, !digitalRead(4));
  Joystick.setButton(6, !digitalRead(8));
 
  //read analog axes
  int value = map(analogRead(X_PIN) + X_TRIM , X_MIN, X_MAX, -512, 512) * X_INVERT;
  Joystick.setXAxis(value);
  value = map(analogRead(Y_PIN) + Y_TRIM , Y_MIN, Y_MAX, -512, 512) *Y_INVERT;
  Joystick.setYAxis(value);
  value = map(analogRead(R_PIN) + R_TRIM , R_MIN, R_MAX, -512, 512) * R_INVERT;
  Joystick.setRudder(value);
  value = map(analogRead(T_PIN) + T_TRIM , T_MIN, T_MAX, -512, 512) * T_INVERT;
  Joystick.setThrottle(value);
  
  Joystick.sendState();
  delay(10);
}

The truth is not if I am correctly connected the arduino to the mux

I tried this code, but nothings happens

// USB Joystick with hall effect sensors
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//------------------------------------------------------------
 
//change these to define which pins your hall effect sensors or potentiometers are connected.
//to change button connections, scroll down to loop()
#define X_PIN A1
#define Y_PIN A0
#define R_PIN A3
#define T_PIN A2
//change these to change trim and limits. Calibrating your joystick in Windows achieves the same thing
#define X_MIN 0
#define X_MAX 1023
#define X_TRIM 0
#define X_INVERT -1
//to invert an axis, change 1 to -1
#define Y_MIN 0
#define Y_MAX 1023
#define Y_TRIM 0
#define Y_INVERT -1
 
#define R_MIN 0
#define R_MAX 1023
#define R_TRIM 0
#define R_INVERT 1
 
#define T_MIN 0
#define T_MAX 1023
#define T_TRIM 0
#define T_INVERT 1

#define S0 15
#define S1 14
#define S2 16
#define S3 10

#define digital 9


#include <Joystick.h>
 
Joystick_ Joystick(0x04,JOYSTICK_TYPE_JOYSTICK,
  7, 0,                  // Button Count, Hat Switch Count
  true, true, false,     // X and Y, but no Z Axis
  false, false, false,   // No Rx, Ry, or Rz
  true, true,            // Yes rudder, yes throttle
  false, false, false);  // No accelerator, brake, or steering

 
void setup() {
  // Initialize Button Pins
  {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);

  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);

  pinMode(digital,INPUT_PULLUP);

  Serial.begin(9600);
}

 
  // Initialize Joystick Library
  Joystick.begin(false); //false = dont send automatically. We will sendState() at the end of the loop
  Joystick.setXAxisRange(-512, 512);
  Joystick.setYAxisRange(-512, 512);
  Joystick.setRudderRange(-512, 512);
  Joystick.setThrottleRange(-512, 512);
}
 
 
void loop() {
  //read buttons. Change pins and button numbers here, if you want to have different number connected to different pin

  Serial.println(myDigitalRead(0,digital));
}

unsigned myDigitalRead(short inputCH, short dig_in)
{
  
  unsigned digitalVal;

  if(inputCH & 0x1) digitalWrite(S0, HIGH); else digitalWrite(S0, LOW);
        if(inputCH & 0x2) digitalWrite(S1, HIGH); else digitalWrite(S1, LOW);
        if(inputCH & 0x4) digitalWrite(S2, HIGH); else digitalWrite(S2, LOW);
        if(inputCH & 0x8) digitalWrite(S3, HIGH); else digitalWrite(S3, LOW);
 
  //read analog axes
  int value = map(analogRead(X_PIN) + X_TRIM , X_MIN, X_MAX, -512, 512) * X_INVERT;
  Joystick.setXAxis(value);
  value = map(analogRead(Y_PIN) + Y_TRIM , Y_MIN, Y_MAX, -512, 512) *Y_INVERT;
  Joystick.setYAxis(value);
  value = map(analogRead(R_PIN) + R_TRIM , R_MIN, R_MAX, -512, 512) * R_INVERT;
  Joystick.setRudder(value);
  value = map(analogRead(T_PIN) + T_TRIM , T_MIN, T_MAX, -512, 512) * T_INVERT;
  Joystick.setThrottle(value);
  
  Joystick.sendState();
  delay(10);
}

From already thank you very much!

Your code looks a bit odd, this routine to handle the multiplexer

unsigned myDigitalRead(short inputCH, short dig_in)

has joystick stuff in it. Is this code you wrote?

Can you draw and upload a schematic so we can see your wiring?

Are you using a bare chip or a module? How are you doing the connections mechanically? Maybe take a picture of your setup.

What do you expect to do with the multiplexer? It is conceptually very simple, the function just mimics digitalRead so that 16 more inputs can be used in the exact same way you use a real input.

Have you tested your hardware with any simple example code that might have accompanied a tutorial you scared up?

This uses a few advanced techniques, maybe a simpler tutorial can be found - where did you get the code you posted?

Feel free to say too much… better than too little. :expressionless:

a7

Thanks for your answer, I'm trying to make a HOTAS joystick to play Star citizen, unfortunately where I live these devices are too expensive, so I want to make one for myself and that's how I learn.

I got the code from a youtuber named Akaki Kuumeri (Flight sim Joystick with Hall effect sensors and Arduino by akaki - Thingiverse) And try to change it to make it work with more inputs in a MUX, this to put more buttons

This is the connection I currently have (I only have one button, because I'm testing)

I have never tried it with a multiplex, I have been studying them, but I don't really know how to apply it. If I have managed to make a jacks with fewer buttons before. The reason I need the multiplex is to have fewer cables in the base, and put these in the controller.

OK, the wiring looks plausible.

It is preferred to have each push button wired between its respective input pin C# on the mux to GND rather than 5 volts.

It is necessary to provide for when the button is open, that is the roll of a pull up resistor.

10K will be fine to steer the logic HIGH when the switch is not closed.

Here (compiles, untested, based on your code) is a complete sketch for just the multiplexer. Note that I invert the actual digitalRead with the '!' operator, logical negation (up is down, down is up) to compensate for using normally pulled up input.

Also in a comment a different way to do the address pin manipuklation for your consideration. No large deal, your logic is fine.

# define digital 9  // mux signal input pin

// mux address pins

# define S0 15
# define S1 14
# define S2 16
# define S3 10

void setup()
{
	Serial.begin(9600);

	pinMode(digital, INPUT_PULLUP);  // or use an external pullup resistor 10K
}


void loop() {
  Serial.println(myDigitalRead(0, digital));

  delay(500); // just to cut down on spamming to the serial monitor
}

unsigned char myDigitalRead(unsigned char inputCH, unsigned char dig_in)
{
  if (inputCH & 0x1) digitalWrite(S0, HIGH); else digitalWrite(S0, LOW);
  if (inputCH & 0x2) digitalWrite(S1, HIGH); else digitalWrite(S1, LOW);
  if (inputCH & 0x4) digitalWrite(S2, HIGH); else digitalWrite(S2, LOW);
  if (inputCH & 0x8) digitalWrite(S3, HIGH); else digitalWrite(S3, LOW);

//  return (digitalRead(dig_in));

  return (!digitalRead (dig_in));    // for pulled up input - presst = LOW
}

/*

// alternate to the if/else bit setting lines
 
digitalWrite(S0, (inputCH & 0x1) ? HIGH : LOW);

*/

At least you can play with this toy program and see how myDigitalRead() sets the address, then reads the actual pin, then returns with that value which is 1 or 0 depends on switch open or closed.

If you add a few more pushbuttons, put them on some random C# pins and use those numbers in the call to myDigitalRead() for testing.

joystick.h documetation hints at being able to handle multiplexers or shift registers, but I cannot find that.

That's all I got right now.

a7

Thanks, I'll do what you tell me and I'll see how it goes, any progress I'll tell right here.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.