Help with Wire.write(string)

I have a 2x16 I2C character LCD I can write individual characters to using the Wire.write() command. I'd like to write entire strings to it, though, and the Reference suggests Wire.write() should be able to do this without me having to explicitly send a single character at a time, but I'm not having any success. Wire.write("string") and Wire.write(x) (where x = "string") both fail. What's the secret? Thanks.

write()

Wire.write("string") and Wire.write(x) (where x = "string") both fail. What's the secret?

Define "fail"! Do you get a compiler error? Does your LCD not do the right thing?

No compiler error. The display shows one character, an @, which isn’t one of the ones I sent. I was hoping someone had some code that worked I could try.

Show the code you say failed. Is x a string or a String?

Here you go:

#include <Wire.h>

char red[] = "RED";
char green[] = "GREEN";
char blue[] = "BLUE";
char yellow[] = "YELLOW";
char orange[] = "ORANGE"; 
char brown[] = "BROWN";
char unknown[] = "UNKNOWN";

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

void loop() {
  char x;
  char* y = green;
  Serial.print("First character:  ");
  Serial.println(y[0]);   // This prints "G" to the monitor, as it should
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);   // Clear display
  Wire.write(0x51);
  Wire.endTransmission();
  Wire.write(y);  // Just prints "@" followed by the alphabet, as does Wire.write("green");
  Wire.endTransmission();
  for (x = 0; x < 33; x++) {     // Print the alphabet
    Wire.beginTransmission(0x28);
    if (x == 17) {
      Wire.write(0xFE); // Move cursor to 2nd line
      Wire.write(0x45);
      Wire.write(0x40);
    }
    Wire.write(64 + x);
    Wire.endTransmission();
    delay(500);
  }
}

That's the really hard way to utilize a 1602 display. Unless it has a really funky non-hd44780 compatible controller (unlikely), you should be using an I2C LCD library that has methods that will properly handle device initialization and other escape sequences for you, i.e. Clearing the display, positioning the cursor, etc. Using the base Wire library forces you to reinvent the wheel...

This library will do everything but connect the wires for you. https://github.com/duinoWitchery/hd44780

If you have any issues with above library, please make new thread in the displays sub-section as you'll get faster responses there since it's really a display problem, not programming.

Thanks, avr_fred. I'll give it a shot, though I'd still like to know what the problem is with Wire.write(string). Seems like it should work. Plus, I have other non-LCD I2C things in my design, so it would reduce my dependencies if I could get everything to work with one library.

void loop() {
  char x;
  char* y = green;
  Serial.print("First character:  ");
  Serial.println(y[0]);   // This prints "G" to the monitor, as it should
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);   // Clear display
  Wire.write(0x51);
  Wire.endTransmission();
  Wire.write(y);  // Just prints "@" followed by the alphabet, as does Wire.write("green");
  Wire.endTransmission();
  for (x = 0; x < 33; x++) {     // Print the alphabet
    Wire.beginTransmission(0x28);
    if (x == 17) {
      Wire.write(0xFE); // Move cursor to 2nd line
      Wire.write(0x45);
      Wire.write(0x40);
    }
    Wire.write(64 + x);
    Wire.endTransmission();
    delay(500);
  }
}

In that code you have no Wire.beginTransmission() before the Wire.write(y). You cannot have two Wire.endTransmission() without a Wire.beginTransmission() in between.

pylon: In that code you have no Wire.beginTransmission() before the Wire.write(y). You cannot have two Wire.endTransmission() without a Wire.beginTransmission() in between.

Thanks. I noticed that after posting and fixed it by adding anothg Wire.beginTransmission(0x28). Doesn't change anything.

Post a link to the documentation of your display controller. The usual I2C LCD controllers use 0xFE, 0x14 to clear the screen. As your’s seems to be different, there may be other differences.