Problem with communication between two arduinos

Hello, I have a problem with communication between my Arduino Nano and Arduino Uno using I2C. The Nano is supposed to send characters to the Uno, but when I try to read the characters I sent, I only get the "⸮" thing in the serial monitor. I used pins 4 and 5 (analog).

Master (Arduino nano)

#include <WSWire.h>
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'#','0','*','D'}
};
byte rowPins[ROWS] = { 12, 11, 10, 9 };
byte colPins[COLS] = { 8, 7, 6, 5 }; 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup(){
  Wire.begin();
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)
  {
    switch (key)
    {
      case '#':
        digitalWrite(ledpin, LOW);
        Wire.beginTransmission(9);
        Wire.write(key);
        Wire.endTransmission();
        break;
      case '*':
        digitalWrite(ledpin, HIGH);
        Wire.beginTransmission(9);
        Wire.write(key);
        Wire.endTransmission();
        break;
      default:
        Serial.println(key);
        Wire.beginTransmission(9);
        Wire.write(key);
        Wire.endTransmission();
    }
    delay(100);
  }
}

Slave (Arduino Uno)

#include <WSWire.h> 
char key = 0;

void setup(){

pinMode (13, OUTPUT);
Wire.begin(9);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent( int bytes ){
char key = Wire.read();
Wire.endTransmission();
}
void loop(){
  char key = Wire.read();
if(key)
  {
    switch (key){
      case '#':
        digitalWrite(13, LOW);
        Serial.println(key);
        break;
      case '*':
        digitalWrite(13, HIGH);
        Serial.println(key);
        break;
      default:
        Serial.println(key);
    }
    delay(100);
  }
}

I used the WSWire library because I had problems with my Nano locking up every time I send the wire.endTransmission .
Can someone help me identify the problem?

Screenshot your serial monitor with the bad output, please

Oops

You appear to have an event listener that reads from the wire and in your loop you are also reading from the wire. You only need one of these. What is most likely happening is the event handler is working correctly but the wire read in the loop is overwriting it with junk, which is what you are printing.

Thanks, I'll try making the changes

Excellent.
Play with it and please post your results even if you are successful so others can learn from this.

I've changed the code and now I'm not getting the repeating reverse question mark output, but I'm still not getting anything from the other board.

How?

Forgive me but my clairvoyance beam is malfunctioning today. :laughing:
Can you please post your updated code so we can read it?

Deleted the excess trigger just like er_name_not_found told me to

We can't see your code

Slave (Arduino uno), updated code

#include <WSWire.h> 
char key = 0;

void setup(){

pinMode (13, OUTPUT);
Wire.begin(9);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent( int bytes ){
char key = Wire.read();
Wire.endTransmission();
}
void loop(){
if(key)
  {
    switch (key){
      case '#':
        digitalWrite(13, LOW);
        Serial.println(key);
        break;
      case '*':
        digitalWrite(13, HIGH);
        Serial.println(key);
        break;
      default:
        Serial.println(key);
    }
    delay(100);
  }
}
#include <WSWire.h> 
char key = 0;

void setup(){

pinMode (13, OUTPUT);
Wire.begin(9);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent( int bytes ){
char key = Wire.read();
Wire.endTransmission();
}
void loop(){
if(key)
  {
    switch (key){
      case '#':
        digitalWrite(13, LOW);
        Serial.println(key);
        break;
      case '*':
        digitalWrite(13, HIGH);
        Serial.println(key);
        break;
      default:
        Serial.println(key);
    }
    delay(100);
  }
}

You may need to clear the value from key after you read it or it will always see a value in key.

char key = Wire.read();

You need to read into the global key.

Like this?

#include <WSWire.h> 
char key = 0;

void setup(){

pinMode (13, OUTPUT);
Wire.begin(9);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent( int bytes ){
char key = Wire.read();
Wire.endTransmission();
}
void loop(){
char key = Wire.read();
if(key)
  {
    switch (key){
      case '#':
        digitalWrite(13, LOW);
        Serial.println(key);
        break;
      case '*':
        digitalWrite(13, HIGH);
        Serial.println(key);
        break;
      default:
        Serial.println(key);
    }
    delay(100);
  }
}

No, not like that.
You're reading into a local variable which then goes out of scope.

In the receiveEvent callback, lose the "char" keyword.

And lose the read in loop - you already know that's wrong.

Alright, made the changes:

#include <WSWire.h> 
char key = 0;

void setup(){

pinMode (13, OUTPUT);
Wire.begin(9);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent( int bytes ){
key = Wire.read();
Wire.endTransmission();
}
void loop(){

if(key)
  {
    switch (key){
      case '#':
        digitalWrite(13, LOW);
        Serial.println(key);
        break;
      case '*':
        digitalWrite(13, HIGH);
        Serial.println(key);
        break;
      default:
        Serial.println(key);
    }
    delay(100);
  }
}

Still not getting anything from the Nano.

Try moving this to your receiveEvent