New function in library

Hey guys.

I'm trying to add a function to a library for I2C LCD controller.

Now, there are two functions for writing on LCD: for printing strings and for printing single chars, but none for printing integers or decimal numbers. I know, that I can convert integers with command itoa to strings, but I would like to implement function for printing them directly, if possible.

For printing chars and strings, here is the code from lib:
BV4618.cpp:

// sends char
void BV4618::print(char c){
  Wire.beginTransmission(_i2adr);
  Wire.send(c);
  Wire.endTransmission();
}
// sends string
void BV4618::print(char *s){
  char *sp=s;  
    Wire.beginTransmission(_i2adr);
    // send *s as bytes of date
    while(*sp)
      Wire.send(*(sp++));
    Wire.endTransmission();
}

BV4618.h:

void print(char c);
void print(char *s);

For printing integers, I tried to use code from Print.h and changed it a bit
BV4618.cpp:

void BV4618::print(int n){
  Wire.beginTransmission(_i2adr);
  Wire.send(n);
  Wire.endTransmission();
}

BV4618.h:

void print(int n);

but that doesn't work :frowning:

Any help from more experienced coders would be much appreciated.
Thank you in advance.

When you say "it doesn't work", what do you mean?

It looks like you are using he code straight out of print(char). In that case it will likely take the low-order byte of the integer and print it as a character. I don't think that what you intend. You probably want to use itoa(n) and use the look from print(char *) to print the digits of the number.

johnwasser:
When you say "it doesn't work", what do you mean?

Hey,

when I said it doesn't work, I ment this:

On first row, there is time, printed with itoa function (integers converted to string with itoa and then printed as "print(char s*), in second line, it is writen with function, that I added to lib (as mentioned in my first post).
The code, used in video:

#include <bv4618.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and molcdfied by D. Sjunnesson

BV4618 lcd(0x31); // 0x62 I2C address (8 bit)

void setup()
{
  Serial.begin(9600);
  lcd.clear(); // clear screen
  lcd.setdisplay(4,20); // set up lcdsplay geometry
}

void loop()
{
  int hour, minute, second;
  hour = RTC.get(DS1307_HR,true);  //This is in military time  [0,23]
  minute = RTC.get(DS1307_MIN,false);
  second = RTC.get(DS1307_SEC,false);

  char hour_str[4]; // Define as a string
  char minute_str[4];
  char second_str[4];
  lcd.setCursor(1,1);  
 
  if(hour < 10){
    lcd.print("0");
  }
  lcd.print(itoa (hour, hour_str, 10)); //convert integer hour to string hour_str and print it on LCD
  lcd.print(':');
  if(minute < 10){
    lcd.print("0");
  }
  lcd.print(itoa (minute, minute_str, 10));
  lcd.print(':');
  if(second < 10){
    lcd.print("0");
  }
  lcd.print(itoa (second, second_str, 10));
  lcd.setCursor(1,11);

 /*************** PRINTS TIME "INTEGER" *****************/
   lcd.setCursor(2,1);  
  if(hour < 10){
    lcd.print("0");
  }
  lcd.print(hour);
  lcd.print(':');
  if(minute < 10){
    lcd.print("0");
  }
  lcd.print(minute);
  lcd.print(':');
  if(second < 10){
    lcd.print("0");
  }
  lcd.print(second);
}

johnwasser:
It looks like you are using he code straight out of print(char). In that case it will likely take the low-order byte of the integer and print it as a character. I don't think that what you intend. You probably want to use itoa(n) and use the look from print(char *) to print the digits of the number.

Its complicated for me (I guess you already noticed, I'm very new to coding :)).
I would like to add function to lib that when I write:

int hour
lcd.print(hour); //or maybe lcd.print(hour, DEC); ???

there would be the correct hour on the display :slight_smile:

Any more help would be much appreciated.
Thank you in advance.

You'll probably get better answers in the programming forum.

Your program is not converting types properly. For 59 it is printing not "59" but ";" which is ascii 59. For 42 it is printing "*" which is ascii 42. It has nothing to do with being in a library, it's a bug in your code.

Thank you for reply.

I still think, it has something to do with my wrong modification to lib. If I comment out my added lines (print(int n)), the above code writes II or III to LCD, where there is supposed to be a number.

Best regards

skipper:
Thank you for reply.

I still think, it has something to do with my wrong modification to lib. If I comment out my added lines (print(int n)), the above code writes II or III to LCD, where there is supposed to be a number.

Best regards

Umm, yeah, you don't want to do this:

  lcd.print(hour);

In the case of, say, 3:00am, that will print the 03 char of the ascii table, which is not a printable character, and does no good.

For that line, try this:

  lcd.print('0' + hour);

Hey,

but the time changes and that code would'nt work all time (for example, when hour is 10, 11, ...)?

Best regards

skipper:
but the time changes and that code would'nt work all time (for example, when hour is 10, 11, ...)?

Ah, yeah true

lcd.print('0' + (hour/10));
lcd.print('0' + (hour%10));

Or you could just wire up printf and be done with it all.