Pages: [1]   Go Down
Author Topic: New function in library  (Read 1796 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
// 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:
Code:
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:
Code:
void BV4618::print(int n){
  Wire.beginTransmission(_i2adr);
  Wire.send(n);
  Wire.endTransmission();
}
BV4618.h:
Code:
void print(int n);

but that doesn't work smiley-sad

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
#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);
}

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 smiley).
I would like to add function to lib that when I write:
Code:
int hour
lcd.print(hour); //or maybe lcd.print(hour, DEC); ???
there would be the correct hour on the display smiley

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


« Last Edit: March 07, 2011, 07:11:12 pm by skipper » Logged

Offline Offline
Full Member
***
Karma: 8
Posts: 147
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 8
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
 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:

Code:
 lcd.print('0' + hour);
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

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

Best regards
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 8
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ah, yeah true
Code:
lcd.print('0' + (hour/10));
lcd.print('0' + (hour%10));

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


Pages: [1]   Go Up
Jump to: