Mini i2c lcd

Hi, I recently bought this diplay:http://international.switch-science.com/catalog/1407/

On that site there is a sketch of the test and it works, but I can not print a variable with the command "lcd_print ();".
I can only print a word, for example: "lcd_print (" Hello! ");"

Can anyone help me? Thanks :slight_smile:

the sketch test is this:

#include <Wire.h>
 
#define vddPin 16    // ArduinoA2
#define gndPin 17    // ArduinoA3
#define sdaPin 18    // ArduinoA4
#define sclPin 19    // ArduinoA5
#define I2Cadr 0x3e  // Fixed
byte contrast = 35;  // Contrast (0~63)
boolean contrastFlag = false;
 
void setup() {
  pinMode(gndPin, OUTPUT);
  digitalWrite(gndPin, LOW);
  pinMode(vddPin, OUTPUT);
  digitalWrite(vddPin, HIGH);
  delay(500);
  Wire.begin();
  lcd_cmd(0b00111000); // function set
  lcd_cmd(0b00111001); // function set
  lcd_cmd(0b00000100); // EntryModeSet
  lcd_cmd(0b00010100); // interval osc
  lcd_cmd(0b01110000 | (contrast & 0xF)); // contrast Low
  lcd_cmd(0b01011100 | ((contrast >> 4) & 0x3)); // contast High/icon/power
  lcd_cmd(0b01101100); // follower control
  delay(200);
  lcd_cmd(0b00111000); // function set
  lcd_cmd(0b00001100); // Display On
  lcd_cmd(0b00000001); // Clear Display
  delay(2);
}
 
void loop() {
  lcd_setCursor(0, 0);
  lcd_printStr("SWITCH");
  lcd_setCursor(1, 1);
  lcd_printStr("SCIENCE");
  if (contrastFlag == false) {
    if (++contrast >= 63) {
      contrastFlag = true;
    }
  } else {
    if (--contrast <= 0) {
      contrastFlag = false;
    }
  }
  lcd_setContrast(contrast);
  delay(100);
}
 
void lcd_cmd(byte x) {
  Wire.beginTransmission(I2Cadr);
  Wire.write(0b00000000); // CO = 0,RS = 0
  Wire.write(x);
  Wire.endTransmission();
}
 
void lcd_contdata(byte x) {
  Wire.write(0b11000000); // CO = 1, RS = 1
  Wire.write(x);
}
 
void lcd_lastdata(byte x) {
  Wire.write(0b01000000); // CO = 0, RS = 1
  Wire.write(x);
}
 
// Show the string.
void lcd_printStr(const char *s) {
  Wire.beginTransmission(I2Cadr);
  while (*s) {
    if (*(s + 1)) {
      lcd_contdata(*s);
    } else {
      lcd_lastdata(*s);
    }
    s++;
  }
  Wire.endTransmission();
}
 
// Set the character location.
void lcd_setCursor(byte x, byte y) {
  lcd_cmd(0x80 | (y * 0x40 + x));
}
void lcd_setContrast(byte c) {
  lcd_cmd(0x39);
  lcd_cmd(0b01110000 | (c & 0x0f)); // contrast Low
  lcd_cmd(0b01011100 | ((c >> 4) & 0x03)); // contast High/icon/power
  lcd_cmd(0x38);
}

Hi

There is no lcd_print() function in the program, just lcd_printStr() and it only prints strings.

If you need to display a string variable, you can do something like this:

char myString[] = "test string";
lcd_printStr(myString);

To display an integer, you can use sprintf() to convert the integer into a string and, as an option, add formatting:

char myNumber[50];  // Allocate space for the string
int i = 42;  // The integer to display
sprintf(buffer, "The value is %d", i);
lcd_printStr(buffer);

The sprintf() function gives you a lot of control over how you format the variables. But note that, on the Arduino, it does not work with floats. The alternative for floats is dtostrf()

Regards

Ray

Hi @Hackscribble. I have tested your solution and it doesn't work, the only thing showed in the LCD was "The value is %d".
I have been searching a bit for the same thing, and was able to find a couple of StackOverflow pages talking exactly about that, but no solution seems to work.

sad outcome for such a nice little cheap LCD screen.

[Edit] this worked for me :: http://playground.arduino.cc/Code/PrintingNumbers

I have tested your solution and it doesn't work, the only thing showed in the LCD was "The value is %d".

We'd have to see exactly how you implemented the solution. There is no way that the %d should have remained in the output string, unless you failed to provide a value to be substituted in its place.

If the device is displaying anything useful at all, then it is a software problem and not a problem with the device.

Your code is probably wrong.