How to read numbers from serial input and print in big fonts (2 lines) [SOLVED]

I have found this code from the old forum to print big 2 lines alphanumeric on an lcd

/*
A set of custom made large numbers for a 16x2 LCD using the
LiquidCrystal librabry. Works with displays compatible with the
Hitachi HD44780 driver.

The Cuicuit:
  LCD RS pin to D12
  LCD Enable pin to D11
  LCD D4 pin to D5
  LCD D5 pin to D4
  LCD D6 pin to D3
  LCD D7 pin to D2
  LCD Vee tied to a pot to control brightness
  LCD Vss and R/W tied to ground
  LCD Vcc to +5V
  LCD pin 15 tied to pushbutton for control of backlight

Made by Michael Pilcher
2/9/2010
*/

// include the library
#include <LiquidCrystal.h>

// initialize the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// the 8 arrays that form each segment of the custom numbers
byte LT[8] =
{
  B00111,
  B01111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};
byte UB[8] =
{
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
byte RT[8] =
{
  B11100,
  B11110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};
byte LL[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B01111,
  B00111
};
byte LB[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111,
  B11111
};
byte LR[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11110,
  B11100
};
byte UMB[8] =
{
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
byte LMB[8] =
{
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111,
  B11111
};

// loop counter
int x = 0;


void setup()
{
  // assignes each segment a write number
  lcd.createChar(0,LT);
  lcd.createChar(1,UB);
  lcd.createChar(2,RT);
  lcd.createChar(3,LL);
  lcd.createChar(4,LB);
  lcd.createChar(5,LR);
  lcd.createChar(6,UMB);
  lcd.createChar(7,LMB);

  // sets the LCD's rows and colums:
  lcd.begin(16, 2);

}

void custom0()
{ // uses segments to build the number 0
  lcd.setCursor(x, 0); // set cursor to column 0, line 0 (first row)
  lcd.write(0);  // call each segment to create
  lcd.write(1);  // top half of the number
  lcd.write(2);
  lcd.setCursor(x, 1); // set cursor to colum 0, line 1 (second row)
  lcd.write(3);  // call each segment to create
  lcd.write(4);  // bottom half of the number
  lcd.write(5);
}

void custom1()
{
  lcd.setCursor(x,0);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(x+1,1);
  lcd.write(5);
}

void custom2()
{
  lcd.setCursor(x,0);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(7);
  lcd.write(7);
}

void custom3()
{
  lcd.setCursor(x,0);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(7);
  lcd.write(7);
  lcd.write(5);
}

void custom4()
{
  lcd.setCursor(x,0);
  lcd.write(3);
  lcd.write(4);
  lcd.write(2);
  lcd.setCursor(x+2, 1);
  lcd.write(5);
}

void custom5()
{
  lcd.setCursor(x,0);
  lcd.write(0);
  lcd.write(6);
  lcd.write(6);
  lcd.setCursor(x, 1);
  lcd.write(7);
  lcd.write(7);
  lcd.write(5);
}

void custom6()
{
  lcd.setCursor(x,0);
  lcd.write(0);
  lcd.write(6);
  lcd.write(6);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(7);
  lcd.write(5);
}

void custom7()
{
  lcd.setCursor(x,0);
  lcd.write(1);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(x+1, 1);
  lcd.write(0);
}

void custom8()
{
  lcd.setCursor(x,0);
  lcd.write(0);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(7);
  lcd.write(5);
}

void custom9()
{
  lcd.setCursor(x,0);
  lcd.write(0);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x+2, 1);
  lcd.write(5);
}

void loop()
{
  custom0();    // displays custom 0 on the LCD
  x = x + 4;    // sifts cursor over 4 columns
  custom1();
  x = x + 4;
  custom2();
  x = x + 4;
  custom3();
  delay(4000);  // delay 4 seconds
  lcd.clear();  // clears the display
  x = 0;  // resets x to 0
  custom4();
  x = x + 4;
  custom5();
  x = x + 4;
  custom6();
  x = x + 4;
  custom7();
  delay(4000);
  lcd.clear();
  x = 0;
  custom8();
  x = x + 4;
  custom9();
  delay(4000);
  lcd.clear();
  x = 0;

}

Now how do I use this code to read the numbers from serial input and print in these fonts… ???

Simple. In pseudocode, you would:

loop: Wait for serial to be available Read serial character If serial character == 0 Print custom character 0 Increase X by 4 If serial character == 1 Print custom character 1 Increase X by 4 etc for characters 2 to 9 inclusive If serial character == carriage return End of line found - what do you want to do now? Move X to 0 perhaps? Goto loop

what I actually want to do is, I want to print some values on the lcd in big fonts..

let say the temperature read by lm35 and display it in big fonts....

let say the temperature read by lm35 and display it in big fonts....

What have you tried?

I was trying to do this in the loop

void loop(){
  0 == custom0()
  1 == custom1()
  2 == custom2()

     val = analogRead(potpin);
     val = map(val, 0, 1023, 0, 100);

}

Trying to print the map value of a potentiometer in big numbers

I was trying to do this in the loop

And that compiles?

You need to convert the value in val to a string, using itoa(). Then, for each character in the string, determine where to position it, and determine which function to call to show that character.

I am unable to find any source which will teach me how to convert an int to string :(

http://www.google.com/search?q=itoa+arduino

Please help me out…:frowning:

As I am a new coder this part is getting difficult for me… :frowning:

Please help me out…

It’s not really all that difficult.

// Convert the value to a string
char buffer[10];
itoa(val, buffer, 10);

// Show the string
int len = strlen(buffer);
for(int i=0; i<len; i++)
{
  char ltr = buffer[i];
  // position the cursor
  switch(ltr)
  {
     case '0':
       custom0();
       break;
    // 9 more similar cases
  }
}
// Convert the value to a string
char buffer[10];
itoa(val, buffer, 10);

Can you please make me understand this part..??

Can you please make me understand this part.

I can only try.

The first line creates a buffer (an array of characters). The second line uses the itoa() function (integer to ascii) to convert an integer to a string, storing the result in the buffer (the array).

The itoa function is explained here - http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

A different direction: No offense to the code's author but the code is pretty crude and seems to have been written by some novice. Have you made sure that you have tracked down the most recent version of the code?

You are welcome to use my much-more polished version if you like:

http://liudr.wordpress.com/libraries/phi_big_font/

You can print numbers characters and some symbols, not just numbers.

I've posted these videos plenty times so just read my blog.

I also have a super font library to print 4-row tall characters on 20*4 displays.

Another complete newbie - sorry.

I want to go the other way - char to float or int

I want to send, say, 123.45 over the serial port to the arduino. These are sent a char variables so I want to convert each group of *char*s to a *float*ing point variable in order to do some arithmatic on them.

The C++ refernence website says that the correct function for this is atol(). If I use atol() in my sketch will the arduino be able to use/interpret it or do I need to include some header files? (no idea about header files either TBH).

Thanks for you patience!

If I use atol() in my sketch will the arduino be able to use/interpret it or do I need to include some header files?

The compiler/linker knows where atol() is. Of course, since atol() (ascii to long) is the wrong function for converting a string to a float, it doesn't really matter.

The atof() function, on the other hand, might be useful.

Then I'll use atof. Thanks. And thank for the "ascii to float" tip... now I see the logic of the function name.

{  
  val = analogRead(potpin);            
  val = map(val, 0, 1023, 0, 9);
  
  // Convert the value to a string
char buffer[10];
itoa(val, buffer, 10);

// Show the string
int len = strlen(buffer);
for(int i=0; i<len; i++)
{
  char ltr = buffer[i];
  // position the cursor
  switch(ltr)
  {
     case '0':
       custom0();
       break;
    
    case '1':
       custom1();
       break;
       
    case '2':
       custom2();
       break;
       
    case '3':
       custom3();
       break;
       
    case '4':
       custom4();
       break;
       
    case '5':
       custom5();
       break;
       
    case '6':
       custom6();
       break;
       
    case '7':
       custom7();
       break;
    case '8':
       custom8();
       break;
       
    case '9':
       custom9();
       break;
    
  }
  
  
}


}

I have done this in the loop…

Now the problem is if I would like to change the map of val from val = map(val, 0, 1023, 0, 9); to val = map(val, 0, 1023, 0, 100);
how do I set the position of the second and the third digit…??

Another tricky problem is how do I avoid the remaining of the last digit when the digit changes…??

I tried lcd.clear();
delay(50);

This worked fine, But I do not want to use delay…

I was moreover thinking of doing it in another way…
Like if (last val != current val){
lcd.clear();
}

But cannot make out how do I store the last val and read it to check if it is same as current val or clear the lcd…

how do I set the position of the second and the third digit..??

Are your reading and comprehension skills that poor? The code you posted in your initial post showed how.

Another tricky problem is how do I avoid the remaining of the last digit when the digit changes..??

What? This does not make sense in English. Perhaps an example would help.

But cannot make out how do I store the last val and read it to check if it is same as current val or clear the lcd..

The value read from the pin should be stored in a global variable, and compared to the previous value, also stored in a global variable.

//Globals
int currValue;
int prevValue = -1;

void loop()
{
    val = analogRead(potpin);            
    currValue = map(val, 0, 1023, 0, 9);

    if(currValue != prevValue)
    {
       // Show the new value (in currValue)
    }
    prevValue = currValue;
}

Are your reading and comprehension skills that poor? The code you posted in your initial post showed how.

I know that… x = x + 4;

But what I wanted to say is that when the value is more than 9 say 10…
The how do i write the code so that the 0 is printed at x = x + 4;