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);
}
}
}
}
}
