Transferring Data through I2C

Noob here,

I need some help trying to transfer some information from a master teensy to atmega nano. My situation is I have a joystick that I’m trying to make which attaches to a gimbal that outputs x and y movement. I have the joystick motherboard (teensy 4.0) that outputs buttons and I want to communicate that data to the other motherboard (nano) that will take the outputs and make it understandable for the computer to read via usb. I will also try to find a code that will take the x and y inputs via Hall effect sensor and communicate that data to the computer via usb. I will post the code I have and some more info.

/* 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);
Bounce button10 = Bounce(10, 10);
Bounce button11 = Bounce(11, 10);
Bounce button12 = Bounce(12, 10);
Bounce button14 = Bounce(14, 10);
Bounce button15 = Bounce(15, 10);
Bounce button16 = Bounce(16, 10);
Bounce button17 = Bounce(17, 10);
Bounce button20 = Bounce(20, 10);
Bounce button21 = Bounce(21, 10);
Bounce button22 = Bounce(22, 10);
Bounce button23 = Bounce(23, 10);
Bounce button24 = Bounce(24, 10);
Bounce button26 = Bounce(26, 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);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(17, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);
  pinMode(24, INPUT_PULLUP);
  pinMode(26, 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();
  button10.update();
  button11.update();
  button12.update();
  button14.update();
  button15.update();
  button16.update();
  button17.update();
  button20.update();
  button21.update();
  button22.update();
  button23.update();
  button24.update();
  button26.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);
  }
  if (button10.fallingEdge()) {
    Joystick.button(11, 1);
  }
  if (button11.fallingEdge()) {
    Joystick.button(12, 1);
  }
  if (button12.fallingEdge()) {
    Joystick.button(13, 1);
  }
  if (button14.fallingEdge()) {
    Joystick.button(14, 1);
  }
  if (button15.fallingEdge()) {
    Joystick.button(15, 1);
  }
  if (button16.fallingEdge()) {
    Joystick.button(16, 1);
  }
  if (button17.fallingEdge()) {
    Joystick.button(17, 1);
  }
  if (button20.fallingEdge()) {
    Joystick.button(18, 1);
  }
  if (button21.fallingEdge()) {
    Joystick.button(19, 1);
  }
  if (button22.fallingEdge()) {
    Joystick.button(20, 1);
  }
  if (button23.fallingEdge()) {
    Joystick.button(21, 1);
  }
  if (button24.fallingEdge()) {
    Joystick.button(22, 1);
  }
  if (button26.fallingEdge()) {
    Joystick.button(23, 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);
  }
  if (button10.risingEdge()) {
    Joystick.button(11, 0);
  }
  if (button11.risingEdge()) {
    Joystick.button(12, 0);
  }
  if (button12.risingEdge()) {
    Joystick.button(13, 0);
  }
  if (button14.risingEdge()) {
    Joystick.button(14, 0);
  }
  if (button15.risingEdge()) {
    Joystick.button(15, 0);
  }
  if (button16.risingEdge()) {
    Joystick.button(16, 0);
  }
  if (button17.risingEdge()) {
    Joystick.button(17, 0);
  }
  if (button20.risingEdge()) {
    Joystick.button(18, 0);
  }
  if (button21.risingEdge()) {
    Joystick.button(19, 0);
  }
  if (button22.risingEdge()) {
    Joystick.button(20, 0);
  }
  if (button23.risingEdge()) {
    Joystick.button(21, 0);
  }
  if (button24.risingEdge()) {
    Joystick.button(22, 0);
  }
  if (button26.risingEdge()) {
    Joystick.button(23, 0);
   
}
}

This code is just a modified version of the joystick button example that arduino ide offers. Is there a way to take this data and transfer it through i2c so that the computer reads the slave or master device as a controller. I also need to add the additional code for x and y axis read through hall effect sensors.

Basically I'm trying to make it so that I can swap the joystick for another joystick while keeping the gimbal base.



int pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22, 23, 24, 26}; 

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  for (int i = 0; i < 23; i++){
    pinMode(pins[i], INPUT_PULLUP);
   
    
  }
}

// the loop routine runs over and over again forever:
void loop() {
  for (int i = 0; i < 23; i++){
    Serial.print(digitalRead(pins[i]));
    Serial.print("\t");
  }
  
 Serial.println();
   
   delay(5);
 }

So this code takes the all the pins from the motherboard and displays them as 0 and 1 outputs. How can I take this data and transfer it to the nano so that it displays the data the same way as if I was looking at it through a serial monitor view. Perhaps making it so that the computer reads the device as a usb

The above for() loop might be exhausted even before you press a key. Re-design the codes so that the loop waits to see that a key is closed.

This is the typical idle output of your above for() loop for 10 buttons ( 2 to 11 inclusive).

1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 

Oops.

So just change the 0 to 1? I’m really new to this. How do I take the data i see from the serial monitor and transfer it to the other motherboard via i2c?

Why oops

Just start with one key connected with DPin-2 with internal pull-up resistor enabled. Read the status of the closed key (LOW) and then send it to the NANO using I2C Bus.

Because this

I was trying to figure out the code for that. So far I’ve only gotten a code to transfer data like hello world and turning on and off leds with sending a 0 or 1 through the serial monitor. Do I use the serial write or read commands? Where do I begin.

No idea what you’re referring to. :grin:

Me neither - I thought this was the Nano code

Fix the above first.

Hints:
If you are asked to write code to set the direction of DPin-2 (Digital Pin-2) as input with internal pull--up resistor enabled, what code would you present?

Do you have an Arduino UNO/NANO Board? If yes, then there might appear some short tutorials for you. I am not much familiar with Teensy 4.0 Board that you own as a primary Learning System.

No idea. Like I said, still new at this. Thank you! :smile:

That means that you do not own an Arduino UNO Board something like Fig-1. Correct?


Figure-1:

@GolamMostafa

I don't own Teensy and what is atmega nano? Is it Arduino NANO?

No. The boards are Teensy 4.0 and an Az-Delivery Nano V3.0 with Atmega328 CH340. Aruduino nano similar board.

Now, tell us what do you want to learn using this NANO Board, the PC, and the IDE?

O! There was a question for you in post #15. We can help you to solve that using NANO Board, if you are still interested.