Using two Arduino boards with the Same Pin numbers assigned to different tasks

Hello, I have connected two arduinos together, there are LEDs connected to both boards, but they both use pins 10, 11, 12 and 14, so when I try to light the LED connected to pin 11 on board 1, the LED on board 2 on pin 11 also lights up. Any solutions?
(Ps. I found a workaround to this by only defining the pins of each arduino in their respective code, but for moving forward I would like both boards to know about the pins of the other)

For context, I am trying to make a Tic Tac Toe game controlled by a keypad, where the board consists of 9 red and 9 green LEDs, and depending on whether a separate red or green LED is on, a red or green LED will light up when keys 1-9 are pressed.

For those interested, or if it helps, below is the code for both boards. (Please don't be too hard on me, I know it's very messy as this is my first project, but thanks in advance).

Master code for Board 1

#include <Wire.h>

#include <Keypad.h>

#define ARDUINONUMBER 1
#define ARDUINONUMBER 2

// Keypad Setup
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


// Arduino 1 Pin Assignments
const int ledPins1[7] = {10, 11, 12, 14, 15, 16, 17};
// Arduino 2 Pin Assignments(redundant, maybe??)
const int ledPins2[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};

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

  Wire.begin(); // Join I2C bus with address #8 (as slave)


  // Setup for Arduino 1
  for (int i = 0; i < 7; i++) {
    pinMode(ledPins1[i], OUTPUT);
    digitalWrite(ledPins1[i], LOW);
    digitalWrite(ledPins1[5], HIGH);
    digitalWrite(ledPins1[6], LOW);
  }

  // Setup for Arduino 2
  for (int i = 0; i < 13; i++) {
    pinMode(ledPins2[i], OUTPUT);
    digitalWrite(ledPins2[i], LOW);
  }

}


void loop() {

  char customKey = customKeypad.getKey();


  if (customKey) {
    Serial.println(customKey); // Print the key pressed
    Wire.beginTransmission(8); // Transmit to device #8 (slave address)
    Wire.write(customKey);     // Send the key pressed
    Wire.endTransmission();    // Stop transmitting
    delay(100);                // Small delay to debounce

    if (Serial.available()) {
      char receivedKey = Serial.read();
    }
    delay(10);                // Small delay to debounce

    // "A" active
    if (customKey == 'A') {
      digitalWrite(ledPins1[5], HIGH);
      digitalWrite(ledPins1[6], LOW);
    }

    if (digitalRead(ledPins1[5]) == HIGH && digitalRead(ledPins1[6]) == LOW) {
      if (customKey == '1' ) {
        digitalWrite(ledPins1[2], HIGH);
      }
      if (customKey == '2' ) {
        digitalWrite(ledPins1[3], HIGH);
      }
      if (customKey == '3' ) {
        digitalWrite(ledPins1[4], HIGH);
      }
      if (customKey == '4' ) {
        digitalWrite(ledPins1[0], HIGH);
      }
      if (customKey == '5' ) {
        digitalWrite(ledPins1[1], HIGH);
      }
      if (customKey == '6' ) {
        digitalWrite(ledPins2[0], HIGH);
      }
      if (customKey == '7' ) {
        digitalWrite(ledPins2[1], HIGH);
      }
      if (customKey == '8' ) {
        digitalWrite(ledPins2[2], HIGH);
      }
      if (customKey == '9' ) {
        digitalWrite(ledPins2[3], HIGH);
      }
    }

    // "B" active
    if (customKey == 'B') {
      digitalWrite(ledPins1[6], HIGH);
      digitalWrite(ledPins1[5], LOW);
    }
/*
    if (digitalRead(ledPins1[6]) == HIGH && digitalRead(ledPins1[5]) == LOW) {
      if (customKey == '1' ) {
        digitalWrite(ledPins2[4], HIGH);
      }
      if (customKey == '2' ) {
        digitalWrite(ledPins2[5], HIGH);
      }
      if (customKey == '3' ) {
        digitalWrite(ledPins2[6], HIGH);
      }
       // deactivated cuz PIN overlap
        if (customKey == '4' ) {
        digitalWrite(ledPins2[7], HIGH);
        }
        if (customKey == '5' ) {
        digitalWrite(ledPins2[8], HIGH);
        }
        if (customKey == '6' ) {
        digitalWrite(ledPins2[9], HIGH);
        }
        if (customKey == '7' ) {
        digitalWrite(ledPins2[10], HIGH);
        }
        if (customKey == '8' ) {
        digitalWrite(ledPins2[11], HIGH);
        }
        if (customKey == '9' ) {
        digitalWrite(ledPins2[12], HIGH);
        }
    }
*/

    if (customKey == '*')
    {
      for (byte i = 0; i < 7; i++) {
        digitalWrite(ledPins1[i], LOW);
        delay(10);
        digitalWrite(ledPins1[5], HIGH);
        digitalWrite(ledPins1[6], LOW);
        for (byte i = 0; i < 13; i++) {
          digitalWrite(ledPins2[i], LOW);
          delay(10);
        }
      }
    }
  }
}

And the Slave code for Board 2:

#include <Wire.h>
#include <Keypad.h>

// Keypad Setup
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Arduino 1 Pin Assignments(modified)
//const int ledPins1[7] = {10, 11, 12, 14, 15, 16, 17};
const int ledPins1[2] = {16, 17};
// Arduino 2 Pin Assignments
const int ledPins2[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};

void setup() {
  Serial.begin(9600);
  Wire.begin(8); // Join I2C bus with address #8 as slave
  Wire.onReceive(receiveEvent); // Register event

#define ARDUINONUMBER 1
#define ARDUINONUMBER 2
/*
#if ARDUINONUMBER == 1
#define const int ledPins1[7] = {10, 11, 12, 14, 15, 16, 17};
*/
#if ARDUINONUMBER == 2                                                         // do "#elif" if ARDUINONUMBER == 1 is active
#define const int ledPins2[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};

#else
#error Board number must be 1 or 2.
#endif

  // Setup LED pins
  for (int i = 0; i < 2; i++) {
    pinMode(ledPins1[i], OUTPUT);
    digitalWrite(ledPins1[i], LOW);
    digitalWrite(ledPins1[1], LOW);
    digitalWrite(ledPins1[0], HIGH);
  }
  for (int i = 0; i < 13; i++) {
    pinMode(ledPins2[i], OUTPUT);
    digitalWrite(ledPins2[i], LOW);
  }
}

void receiveEvent(int howMany) {

  while (Wire.available()) {

    char c = Wire.read(); // Receive byte as a character
    Serial.print("Received: ");
    Serial.println(c);    // Print the character
    handleKey(c);          // Handle the received key press
  }
}

void loop() {
  //empty cuz slave
}
void handleKey(char customKey) {


  if (customKey) {
    Serial.println(customKey); // Print the key pressed

    if (Serial.available()) {
      char receivedKey = Serial.read();
    }
    delay(100);                // Small delay to debounce

    // "A" active
    if (customKey == 'A') {
      digitalWrite(ledPins1[0], HIGH);
      digitalWrite(ledPins1[1], LOW);
    }

    if (digitalRead(ledPins1[0]) == HIGH && digitalRead(ledPins1[1]) == LOW) {
      /*if (customKey == '1' ) {
        digitalWrite(ledPins1[2], HIGH);
        }
        if (customKey == '2' ) {
        digitalWrite(ledPins1[3], HIGH);
        }
        if (customKey == '3' ) {
        digitalWrite(ledPins1[4], HIGH);
        }
        if (customKey == '4' ) {
        digitalWrite(ledPins1[5], HIGH);
        }
        if (customKey == '5' ) {
        digitalWrite(ledPins1[6], HIGH);
        }*/
      if (customKey == '6' ) {
        digitalWrite(ledPins2[0], HIGH);
      }
      if (customKey == '7' ) {
        digitalWrite(ledPins2[1], HIGH);
      }
      if (customKey == '8' ) {
        digitalWrite(ledPins2[2], HIGH);
      }
      if (customKey == '9' ) {
        digitalWrite(ledPins2[3], HIGH);
      }
    }

    // "B" active
    if (customKey == 'B') {
      digitalWrite(ledPins1[1], HIGH);
      digitalWrite(ledPins1[0], LOW);
    }

    if (digitalRead(ledPins1[1]) == HIGH && digitalRead(ledPins1[0]) == LOW) {
      if (customKey == '1' ) {
        digitalWrite(ledPins2[4], HIGH);
      }
      if (customKey == '2' ) {
        digitalWrite(ledPins2[5], HIGH);
      }
      if (customKey == '3' ) {
        digitalWrite(ledPins2[6], HIGH);
      }
      if (customKey == '4' ) {
        digitalWrite(ledPins2[7], HIGH);
      }
      if (customKey == '5' ) {
        digitalWrite(ledPins2[8], HIGH);
      }
      if (customKey == '6' ) {
        digitalWrite(ledPins2[9], HIGH);
      }
      if (customKey == '7' ) {
        digitalWrite(ledPins2[10], HIGH);
      }
      if (customKey == '8' ) {
        digitalWrite(ledPins2[11], HIGH);
      }
      if (customKey == '9' ) {
        digitalWrite(ledPins2[12], HIGH);
      }
    }


    if (customKey == '*')
    {
      for (byte i = 0; i < 7; i++) {
        digitalWrite(ledPins1[i], LOW);
        delay(10);
        for (byte i = 0; i < 13; i++) {
          digitalWrite(ledPins2[i], LOW);
          delay(10);
          digitalWrite(ledPins1[0], HIGH);
          digitalWrite(ledPins1[1], LOW);
        }
      }
    }
  }
}

Welcome to the forum

Exactly how have you got the boards connected ?
Which pins to which pins ?

You cannot use the same pin for the keypad and for an LED

byte rowPins[ROWS] = {9, 8, 7, 6};
const int ledPins2[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};

Sorry for the lack of detailed explanation, but they do not use the same Pins, since the Keypad is connected to board 1 and the ledPins2 are connected to the second board.

Now I am even more confused

What stops all of the code in your "Master code for Board 1" being compiled ?

You have not explained how the two boards are connected. A diagram would help

There is no problem with compiling, and both boards are connected via the SDA and SCL ports.

It compiles OK but despite what you say you have the same pins defined as being used for two different purposes. Take pin 2 for instance

byte colPins[COLS] = { 5, 4, 3, 2 };

Here it is used as a keypad column pin

const int ledPins2[13] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15 };
    // Setup for Arduino 2
    for (int i = 0; i < 13; i++)
    {
        pinMode(ledPins2[i], OUTPUT);
        digitalWrite(ledPins2[i], LOW);
    }
}

Here it is used as an output

            if (customKey == '6')
            {
                digitalWrite(ledPins2[0], HIGH);
            }

which you write to

This is where you went wrong. Why did you do it? If you need more pins, there are much better ways to add more pins. There are also ways to reduce the number of pins needed to achieve many functions.

Sorry, as ive said before the code is very messy, since this is my first real project I did on my own, but to try to clear up the confusion, here is an explanation of the Pins on each board:

Arduino board 1:
Pins 2-9 : connected to the keypad defined as

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Pins 10, 11, 12, 14 : LED's on the Tic tac toe Grid defined as

const int ledPins1[7] = {10, 11, 12, 14, 15, 16, 17};

LED pins 15 and 16: the Red and green LED's indicating which players turn it is defined as

const int ledPins1[7] = {10, 11, 12, 14, 15, 16, 17};

Arduino board 2:
Pins 2-12, 14 and 15: also LED's on the Tic Tac Toe Grid defined as

const int ledPins2[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};

I hope this can clear up any confusion

I understand which pins you want to use on which board but you cannot use your "Master code for Board 1" as it is for the reasons given

Why would you need to tell the code on board 1 that pin 2 is an output and what do you expect to happen when you write to it using the code on board 1 ?

I do see that, as is right now, there isn't really a point in connecting the two boards, but moving forward i would like to add some logic, like checking if a player has won, for which i would like to check the state of the LED's

You are right, pins 2-9 should not be defined as an output (and i don't think they are, maybe im wrong), but the code for the Keypad still works as expected, LED's light up when a key is pressed, and the correct information is being displayed on the serial monitor.

Sorry, I was not clear. The error is to use two Arduino when only one Arduino is needed.

I am using an Arduno Uno, which does not have enough ports to connect both a keypad and 20 LED's

Yes, it does. Did you read my post #7?

And what would those ways be? I would be thankful for concrete advice.

For your LEDs, I would recommend a max7219 or HT16K33 module.

Probably the least expensive option is to buy one of these 8x8 matrix modules. But instead of connecting the 8x8 led matrix, connect your own leds.

Thanks for the advice, but what if I don't want to use any other external parts?

Then you could connect your 20 LEDs in a 7x3 or 6x4 matrix. But this would make the code more difficult.

Would the matrix only be in the code, since i want the two 3x3 grids (+2 status LED's )to stay in shape for the tic tac toe game. How would this code look like?

The LEDs would be connected as a matrix, like this:

RRR
RRR
RRR
GGG
GGG
GGG
SS

or

RRRGGG
RRRGGG
RRRGGG
SS

for example.

However, you can physically arrange the LEDs however you want.

The code would need to scan the matrix, row-by-row or column-by-column, using micros() function to time the scanning.