I'm stuck, I figured I'd ask here for some help... Note I'm a newb and this is essentially my first Arduino project beyond blink...
I'm creating Virtual cockpit panels for DCS World, using an Arduino Leonardo as an HID device. I'm using three SN74HC165 Shift-In registers for multi-buttons (24) - as it seemed the easiest - and all in all it does work...
So I figured that I'm using only 4 pins on the Leonardo, so why not try to setup 3 of these "shift-in clusters" (12 pins) to appear as 3 separate joysticks?
I got the one working and I got 2 more joysticks showing up in Windows10... now my problem is: how would I setup 2 more "shiftIn Clusters", I've tried many things but I must defer to outside help (cause I really don't know what I'm doing)
Edit to clarify: So each joystick need 3 or 4 shift registers making 24-32 buttons on each joystick
Shift In Lib: https://github.com/.../Ardu.../blob/master/ShiftIn/src/ShiftIn.h
Joy2 Lib: https://github.com/MHeironimus/ArduinoJoystickLibrary
Here's an image of the Layout I want: https://i.sstatic.net/sEtRp.png
... any help would relieve a great headache, Thank for your time
#include <ShiftIn.h>
#include <Joystick.h>
// use <n> shift registers
ShiftIn<3> shift; // use three shift register
// Create joystick
// TESTING MULTI JOYSTICKS
// 1/3 Works!!!
// 3/3 Appear in Windows
// ????? must add shiftIn sections for 2nd and 3rd joysticks????
// (add pin usage + modify line 77-79 stuff)
#define JOYSTICK_COUNT 3
Joystick_ Joystick[JOYSTICK_COUNT] = {
Joystick_(0x03, JOYSTICK_TYPE_GAMEPAD, 32, 0, false, false, false, false, false, false, false, false, false, false, false),
Joystick_(0x04, JOYSTICK_TYPE_GAMEPAD, 32, 0, false, false, false, false, false, false, false, false, false, false, false),
Joystick_(0x05, JOYSTICK_TYPE_GAMEPAD, 32, 0, false, false, false, false, false, false, false, false, false, false, false),
};
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
//const bool testAutoSendMode = true;
const bool testAutoSendMode = false;
const unsigned long gcCycleDelta = 1000;
const unsigned long gcAnalogDelta = 25;
const unsigned long gcButtonDelta = 500;
unsigned long gNextTime = 0;
unsigned int gCurrentStep = 0;
int gJoystickId = 0;
/*
// WORKING SECTION Single Joystick with Shift Reg
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
32, 0, // Button Count, Hat Switch Count
false, false, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
*/
void setup() {
shift.begin(8, 9, 11, 12); // set pins of the shift register
// shift[1].begin(7, 6, 5, 4); // set pins of the shift register
// shift[2].begin(3, 2, 1, 0); // set pins of the shift register
Joystick[0].begin();
Joystick[1].begin();
Joystick[2].begin();
}
void loop() {
if (shift.update()) {
for (int i = 0; i < shift.getDataWidth(); i++)
Joystick[0].setButton(i, shift.state(i)); // just press or release the button
}
/*if (shift[1].update()) {
for (int i = 0; i < shift[1].getDataWidth(); i++)
Joystick[0].setButton(i, shift[1].state(i)); // just press or release the button
}
if (shift[2].update()) {
for (int i = 0; i < shift[2].getDataWidth(); i++)
Joystick[0].setButton(i, shift[2].state(i)); // just press or release the button
}
delay(10);
*/
}
1 Answer 1
Each ShiftIn
instance handles just one connection. You need to make multiple instances.
ShiftIn shift1<3>;
ShiftIn shift2<3>;
ShiftIn shift3<3>;
Then you begin
each of them:
void setup() {
shift1.begin(8, 9, 11, 12);
shift2.begin(7, 6, 5, 4);
shift3.begin(3, 2, 1, 0);
... etc ...
}
Each shiftx
is a completely separate entity. You can just repeat your block of code for each shift
object:
if (shift1.update()) {
for (int i = 0; i < shift1.getDataWidth(); i++)
Joystick[0].setButton(i, shift1.state(i));
}
if (shift2.update()) {
for (int i = 0; i < shift2.getDataWidth(); i++)
Joystick[1].setButton(i, shift2.state(i));
}
if (shift3.update()) {
for (int i = 0; i < shift3.getDataWidth(); i++)
Joystick[2].setButton(i, shift3.state(i));
}
You could arrange the ShiftIn
objects into an array like you do with your Joystick
objects and then loop through them, but that is pretty pointless for just three of them.
-
Thanks for your reply! I didn't make my question clear, I need at least 24 buttons on each joystick so 3 shift registers on each joystick.. I added a pictureFedaykinWolf– FedaykinWolf2019年07月09日 18:23:19 +00:00Commented Jul 9, 2019 at 18:23
-
@FedaykinWolf Ah right. I misunderstood. I'll modify my answer.Majenko– Majenko2019年07月09日 18:59:21 +00:00Commented Jul 9, 2019 at 18:59
-
That was it!!! the first part I din't understand how to start 3 instances (Also I was trying to modify "ShiftIn" and not "Shift"... Thank you soo much! suddenly my headache is cured.FedaykinWolf– FedaykinWolf2019年07月09日 19:10:16 +00:00Commented Jul 9, 2019 at 19:10
-
@FedaykinWolf Another option to save wiring is to connect all of them together to make one
ShiftIn shift<9>;
and do how I had before.Majenko– Majenko2019年07月09日 19:15:12 +00:00Commented Jul 9, 2019 at 19:15 -
Thanks again! I was thinking about that option, but I need it a little more modular so I can change cockpit types (F-16, F-18, A-10) quickly, and easilyFedaykinWolf– FedaykinWolf2019年07月09日 19:38:45 +00:00Commented Jul 9, 2019 at 19:38