Printing an integer to an LCD

Hi there, I'm trying to print out the time to my LCD with an RTC module using an Arduino

I'm using the LiquidCrystal library, and have gotten it to work just dandy.

I feel really stupid right now, but I can't get it to print out an integer to the LCD. It puts a solid black box. Characters work just fun, and I tried to cast it (char)number_variable, but it just doesn't seem to work.

Is there a trick with this?

Hi,
the ascii-characters for numbers start at 48 (for 0) up to 57(for 9);

Easiest solution for a single digit is

int numbervalue=3;
char digit=(char)(numbervalue+48);

For a bigger (unsigned) int-value this here should work;

int value=12345; //for instance
//int on arduino has 5 digits maximum
char digits[5]={' ',' ',' ',' ',' '};
while(value>0) {
     char[4]=(char)((value%10)+48);
     value=value/10;
     char[3]=(char)((value%10)+48);
     value=value/10;
     char[2]=(char)((value%10)+48);
     value=value/10;
     char[1]=(char)((value%10)+48);
     value=value/10;
     char[0]=(char)((value%10)+48);
}
//now the number should be in the char array. Don't forget to handle the sign!

Eberhard

Neat method!

Did you mean digits[4] instead of char[4]? it yelled at me about that

Here's what I have now:

char* intToChar(int value) {
  char digits[5] = {' ',' ',' ',' ',' '};
  while(value > 0) {
     digits[4]=(char)((value%10)+48);
     value=value/10;
     digits[3]=(char)((value%10)+48);
     value=value/10;
     digits[2]=(char)((value%10)+48);
     value=value/10;
     digits[1]=(char)((value%10)+48);
     value=value/10;
     digits[0]=(char)((value%10)+48);
  }
  return digits;
}

...

lcd.print(intToChar(1));

The error it gives me is:
In function 'void loop()':
error: invalid conversion from 'char*' to 'int

I tried
lcd.print((char)intToChar(1));
and
lcd.print((char*)intToChar(1));

But no luck :frowning:

Thanks!!

Neat method!

Did you mean digits[4] instead of char[4]? it yelled at me about that

Yes.

But the bad news is, the whole code is
1. written badly
2. does not work

sorry here is a another attempt :frowning:

I would implement the function (for the arduino environment only) a static array that is created on
startup.I actually never tried to create and return an array inside a function.

Putting the whole conversion into a while() loop was completely dumb of me. We are handling all 5 digits
in the code, there is nothing to iterate over.
Ok, this one should be better

//somewhere at the top of the code
char digits[5];

void intToChar(int value) {
  //blank the char values 
  for(int i=0;i<5;i++) {
     digits[i]=' ';
  }
  digits[4]=(char)((value%10)+48);
  value=value/10;
  digits[3]=(char)((value%10)+48);
  value=value/10;
  digits[2]=(char)((value%10)+48);
  value=value/10;
  digits[1]=(char)((value%10)+48);
  value=value/10;
  digits[0]=(char)((value%10)+48);
}

//Sorry, I never used the lcd-lib, does the lib take an array-argument ??
lcd.print(intToChar(1));

I hope this one is a bit better.

As a little extra here is some code that use in one of my projects to format the currrent value returned by millis()
into a string to be printed onto a display with an SPI-interface.

void writeUptime() {
  char uptimeVal[16];

  //build a String With the uptime
  unsigned long ut=millis();
  
  unsigned long m_sec=ut%1000;
  ut=ut/1000;
  unsigned long sec=ut%60;
  ut=ut/60;
  unsigned long m=ut%60;
  ut=ut/60;
  unsigned long h=ut%24;
  ut=ut/24;
  
  //I have 16 chars and want the uptime centered on the upper line of the display
  uptimeVal[0]=' ';
  uptimeVal[1]=' ';
  uptimeVal[2]=(h/10)+48;     
  uptimeVal[3]=(h%10)+48;
  uptimeVal[4]=':';
  uptimeVal[5]=(m/10)+48;     
  uptimeVal[6]=(m%10)+48;
  uptimeVal[7]=':';
  uptimeVal[8]=(sec/10)+48;     
  uptimeVal[9]=(sec%10)+48;
  uptimeVal[10]=':';
  //milliseconds start here
  uptimeVal[11]=(m_sec/100)+48;     
  m_sec=m_sec%100;
  uptimeVal[12]=(m_sec/10)+48;
  uptimeVal[13]=(m_sec%10)+48;
  uptimeVal[14]=' ';
  uptimeVal[15]=' ';

  //now write that string to the display
  //none of these next calls will work for the lcd-lib 
  clearDisplay();
  moveCursor(0,0);  
  //now print it to the LCD
  digitalWrite(CS,LOW);
  delay(0);
  for(int i=1;i<16;i++) {
    spi_transfer(uptimeVal[i],true);
  }
  digitalWrite(CS,HIGH);  
  moveCursor(0,1);
}

Sorry for the misinformation
Eberhard

If lcd.print() expects a string as its parameter, your mistake is not sending a null terminated string. You need to add one more character to the end of your array and stick a null character '\0' in there, e.g.digits[5] = '\0';

-j

Ok, so now I've got:

char digits[5];

char intToChar(int value) {
  //blank the char values 
  for(int i=0;i<5;i++) {
     digits[i]=' ';
  }
  digits[5] = '\0';
  digits[4]=(char)((value%10)+48);
  value=value/10;
  digits[3]=(char)((value%10)+48);
  value=value/10;
  digits[2]=(char)((value%10)+48);
  value=value/10;
  digits[1]=(char)((value%10)+48);
  value=value/10;
  digits[0]=(char)((value%10)+48);
}

...

lcd.print(intToChar(2));

Seems to be closer! But no matter what number I put in there, it returns 0.

Do I need to put a return in the function?

I'm not sure if the LCD library accepts an array to tell you the truth.

Do I need to put a return in the function?

Yes, if you want the function to return a value, you must use return and that value to do it.

In addition to that error, your function is of type char, but you apparently intend to return a character array pointer (char *).

Looks like lcd.printIn prints a null terminated string (why it isn't called printStr I have no idea...).

hmm, someone clever has already written an integer to ASCII function for us, so give this a try:

int val = 12345;
char s[10];

lcd.printIn(itoa(val, s, 10));

-j

Wow, that worked wonderfully!

How can I string multiple of these together? I feel like an idiot right now, but nothing is working. I've tried:

char output[20] = itoa(hour, s, 10) + ':' + itoa(minute, s, 10) + ':' + itoa(second, s, 10);
lcd.printIn(itoa(hour, s, 10) + '/' + itoa(minute, s, 10) + '/' + itoa(second, s, 10);
lcd.printIn(itoa(hour, s, 10) + "/" + itoa(minute, s, 10) + "/" + itoa(second, s, 10));

I come from the Ruby world where this stuff is much easier!! :slight_smile:

lcd.print would do it for me, but it doesn't like this stuff (I guess because it doesn't have the \0 on the end)

Thanks for all your help,

Sorry I'm an idiot, I didn't fully realize what printIn did until now, for some reason I thought it cleared the screen before writing and you couldn't daisy-chain them together by moving the cursor.

Thanks for all your help!