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!
