Joystick to another Joystick (or throttle)

Can I take this code and make so that I can transmit the data of the push buttons onto another arduino micro. Basically I would like this joystick's controller (arduino micro) to be able to talk to the other joystick controller (arduino micro) over i2c and take make it so the controllers appear as one in windows. If so, how do I accomplish this? I'm new to the coding thing.

/* Buttons to USB Joystick Example

   You must select Joystick from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

#include <Bounce.h>

// Create Bounce objects for each button.  The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10);  // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10);  // which is appropriate for
Bounce button3 = Bounce(3, 10);  // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10);
Bounce button6 = Bounce(6, 10);
Bounce button7 = Bounce(7, 10);
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);

void setup() {
  // Configure the pins for input mode with pullup resistors.
  // The pushbuttons connect from each pin to ground.  When
  // the button is pressed, the pin reads LOW because the button
  // shorts it to ground.  When released, the pin reads HIGH
  // because the pullup resistor connects to +5 volts inside
  // the chip.  LOW for "on", and HIGH for "off" may seem
  // backwards, but using the on-chip pullup resistors is very
  // convenient.  The scheme is called "active low", and it's
  // very commonly used in electronics... so much that the chip
  // has built-in pullup resistors!
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);  // Teensy++ LED, may need 1k resistor pullup
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

  // Please be aware the X, Y, Z, Zr and Slider axes will have default
  // settings, if you only use the buttons.  This can give the appearance
  // of the buttons interfering with the axes, if your PC software shows
  // different default assumed values before your first button press.
  //  More details here:
  //  https://forum.pjrc.com/threads/29320-Teensy-3-1-Button-problems?p=80275#post80275
}

void loop() {
  // Update all the buttons.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  button6.update();
  button7.update();
  button8.update();
  button9.update();

  // Check each button for "falling" edge.
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  if (button0.fallingEdge()) {
    Joystick.button(1, 1);
  }
  if (button1.fallingEdge()) {
    Joystick.button(2, 1);
  }
  if (button2.fallingEdge()) {
    Joystick.button(3, 1);
  }
  if (button3.fallingEdge()) {
    Joystick.button(4, 1);
  }
  if (button4.fallingEdge()) {
    Joystick.button(5, 1);
  }
  if (button5.fallingEdge()) {
    Joystick.button(6, 1);
  }
  if (button6.fallingEdge()) {
    Joystick.button(7, 1);
  }
  if (button7.fallingEdge()) {
    Joystick.button(8, 1);
  }
  if (button8.fallingEdge()) {
    Joystick.button(9, 1);
  }
  if (button9.fallingEdge()) {
    Joystick.button(10, 1);
  }

  // Check each button for "rising" edge
  // Update the Joystick buttons only upon changes.
  // rising = low (pressed - button connects pin to ground)
  //          to high (not pressed - voltage from pullup resistor)
  if (button0.risingEdge()) {
    Joystick.button(1, 0);
  }
  if (button1.risingEdge()) {
    Joystick.button(2, 0);
  }
  if (button2.risingEdge()) {
    Joystick.button(3, 0);
  }
  if (button3.risingEdge()) {
    Joystick.button(4, 0);
  }
  if (button4.risingEdge()) {
    Joystick.button(5, 0);
  }
  if (button5.risingEdge()) {
    Joystick.button(6, 0);
  }
  if (button6.risingEdge()) {
    Joystick.button(7, 0);
  }
  if (button7.risingEdge()) {
    Joystick.button(8, 0);
  }
  if (button8.risingEdge()) {
    Joystick.button(9, 0);
  }
  if (button9.risingEdge()) {
    Joystick.button(10, 0);
  }
}

Maybe this could help,

Master ,

#include <Wire.h>

struct test {
  byte a = 0;
};
test dataStruct;

char  messageBuffer[sizeof(dataStruct)];

byte a = 0;

void setup() {
  // put your setup code here, to run once:

}

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

}
void readSubModule() {
  Wire.beginTransmission(0x50);
  Wire.write(0x01);
  Wire.endTransmission();
  Wire.requestFrom(0x50, sizeof(dataStruct));    // request data from slave device #2
  if (Wire.available()==sizeof(dataStruct)) {
    Wire.readBytes(messageBuffer , sizeof(dataStruct));
    memcpy(&dataStruct, & messageBuffer , sizeof(dataStruct));
  }
}

slave

#include <Wire.h>

struct test {
  byte a = 255;
};
test dataStruct;

char  messageBuffer[sizeof(dataStruct)];

byte CMD = 0;

void setup() {
  // put your setup code here, to run once:
  Wire.begin(0x50);                // join i2c bus with address #2
  Wire.onRequest(requestEvent);

  Wire.onReceive(receiveEvent);
}

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

}
void receiveEvent(int HowMany)
{
  if (Wire.available() > 0)
  {
    CMD = Wire.read();
  }
}

void requestEvent()
{
  if (CMD == 0x01)
  {
    Wire.write((byte*) &dataStruct, sizeof(dataStruct));
  }
  if (CMD == 0x02)
  {
    //do something
  }
  if (CMD == 0x03)
  {
    //do something
  }
  if (CMD == 0x04)
  {
    //do something
  }
}

I have not tested.

An "X-Y" problem...

Please take a step back and explain what you are trying to achieve.

Nano cannot act as a joystick for a PC. For that you need Pro Micro.

Using 2 Arduino in a project is usually a mistake. Even experts will only attempt this when there is no other choice or no simpler way.

Of course. So the joystick I created has a bunch of buttons, 23 to be exact. They’re all connected to one common ground. When the button is pushed, the button connects to ground. This is controlled by a teensy at the moment but I think I’m going to switch to an esp32. I’m trying to connect this joystick to a base that takes x and y movements from a Hall effect sensor. I’m trying to connect the joystick and base through serial communication via i2c, that way the joystick and base reads as one controller in windows (joystick arduino/teensy takes button commands and transfers to base arduino which takes those commands and the x/y inputs and outputs everything to windows).

Apologies,
I should of mentioned that the board is an arduino micro clone (w/ icsp) and it is fully compatible with arduino.

This all very confusing! Nano, Micro, teensy (which model, there are a large number and they are very different) and now esp32 have all been mentioned. But I see no reason that the Micro can't do everything you want. Using more than one "Arduino" in a project is a common mistake many beginners make, believing it to be the answer to various problems such as insufficient pins or their own limited programming skills. Even experts will only use more than one Arduino in a project after very careful consideration, because it is more difficult than it seems and there are easier solutions in most cases.

Right. So the arduino platform couldn’t work because of the size constraints and limited number of I/o pins. I could’ve used shift registers or an mcp expander of some sort, however the size constraints of adding resistors and additional boards would be excessive for the application (all this in a joystick). So far I’ve gotten the micro to read the teensy buttons outputs by a series of 1 and 0. Since the arduino does not have usb hid capability, I would have to make a program that can read the arduino outputs or am I mistaken?

Still very confusing. I'm not sure if you are agreeing or disagreeing with me.

As far as I can tell, you have a joystick and a button box with 20+ buttons. I think an Arduino micro can deal with that. Maybe an i²c expander inside the button box would help keep the wiring simpler between the box and the joystick.

So the arduino is inside the joystick that has 20 buttons. Let me explain, the stick portion has 20 buttons. I’m attaching the stick to the base that is taking the x and y movements. The arduino is taking the x and y movements while the teensy is taking button presses. I’m trying to have the controllers act as one

image

This

I guess I need to add the arduino I’m using had the atmega328p chip which doesn’t help it become an his device

Yes, you need to use an Arduino that can act as a HID device, like Micro or Pro Micro which is based on atmega32u4 chip, but there are other more modern chips that can also act as HID. Anything based on atmega328, like Nano, can't do HID.

So I suggest a Micro or Pro Micro and maybe an I/o expander (which won't take up any more space than your mysterious teensy, and won't need resistors either).

Maybe the teensy can act as a HID device? What model is it? (I did ask earlier but you didn't pick up on my question).

It’s a teensy 4.0

Wow, that's quite a beast! Totally overkill for this project. I would save your T4.0 for a project more worthy of its capabilities and get something simpler for this project.

But if you insist on using it, it can act as a HID and has plenty of pins, so why do you feel the need to add another Arduino?

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