Issues with displaying data sent over i2c on lcd

So all I'm trying to do is take in keypad input, send it to the second processor and display that character, this would appear simple but I just can't get the darn thing to work, I'm guessing it's got something to do with how I'm transmitting data over i2c, I've tried using both byte and char on the board transmitting but no success, I just get a character on the LCD which is 3 horizontal stripes

Things I've worked out:
For some reason the char value 'key' isn't global and the value isn't kept outside of that switch statement
It's a pain to use the serial console on a mac vs windows

Code on the board that's transmitting

// i2c Basic Writing Program
// based off example code from the included arduino sketch and http://www.arduino.cc/en/Tutorial/MasterReader

#include <Wire.h>

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 8, 9, 10, 11 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 2, 3, 4 }; 

#define ledpin 13


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

void setup()
{
  Wire.begin(2); // join i2c bus (address optional for master)
  Wire.onRequest(requestEvent); // register event
}

char key;

void loop()
{
char key = kpd.getKey();
  if(key)  // same as if(key != NO_KEY)
  {
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
      case '#':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

void requestEvent()
{
  Wire.send(key);
}

Code on the board that's recieving and attempting to display the data

// i2c Basic Reading Program
// Based off the example code supplied with arduino IDE

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  lcd.setCursor(0, 1);
  Wire.begin(4);        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(2, 1);    // request 6 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.receive(); // receive a byte as character
    Serial.print(c); 
    lcd.setCursor(0, 1);
    lcd.print(c);
    Serial.println(c);
  }

  delay(500);
}

Sorry if it looks a bit daft, all this arduino stuff is new to me :slight_smile:

I know I can transmit stuff to the lcd over i2c, done it already with an incrementing character, just not had any luck with this keypad information.

Any help would be greatly appreciated

I think maybe you have a bus with no master. Try leaving the '4' out of Wire.begin(4);

"Parameters
address: the 7-bit slave address (optional); if not specified, join the bus as a master."

I wish it was that easy :frowning: That code works just fine if I have a character and increment as in the wire examples. I believe my issue is how I handle the keypad input

void loop()
{
char key = kpd.getKey();

Every time through loop() you are setting 'key' to whatever you get from the keypad library, usually NO_KEY. Unless you happen to get a Wire request at the instant a real key press comes in it will almost instantly be replaced with NO_KEY and that is what you will send.

Try something like this:

void loop()
{
char keyval = kpd.getKey();
  if(keyval)  // same as if(key != NO_KEY)
  {
    key = keyval;
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
      case '#':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

void requestEvent()
{
       Wire.send(key);
       key = NO_KEY;  // Only send it once.
}

You may want to put a check on the receiving side to detect "NO_KEY" values and ignore them.

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.receive(); // receive a byte as character
    if (c)
        {
        lcd.setCursor(0, 1);
        lcd.print(c);
        Serial.println(c);
        }
  }