Printing integer value to LCD

I’m making a device to measure the level of water in a tank. I’m using the MaxSonar EZ1 rangefinder and an LCD keypad shield from RobotShop.com
The problem is, I’m using the pulse width function of the sensor to get the distance, and reading that value into digital port 3 on the arduino. Now when I try to print the calculated distance to the LCD screen, it prints the ASCII value, and not the int value.

//example use of LCD4Bit_mod library
#include <LCD4Bit_mod.h> 
//create object to control an LCD.  
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2); 

//Key message
char msgs[5][15] = {"  Menu 1     ", 
                    "Up Key OK    ", 
                    "Down Key OK  ", 
                    "  Menu 2     ", 
                    "Select Key OK" };
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
const int pwPin = 3;
long pulse, inches, cm;
int level;

void setup()
{ 
  Serial.begin(9600);
  lcd.init();
  lcd.clear();
  lcd.printIn("Ultrasonic Level");
    
}

void loop() 
{
  //char val[4];
  adc_key_in = analogRead(0);      // read the value from the sensor  
  key = get_key(adc_key_in);         // convert into key press
  if (key != oldkey)               // if keypress is detected
  {
    delay(50);                       // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor  
    key = get_key(adc_key_in);         // convert into key press
    if (key != oldkey)                        
    {                  
      oldkey = key;
      if (key >=0)
      {
        lcd.clear();
        lcd.cursorTo(1, 0);  //line=2, x=0
        lcd.printIn(msgs[key]);
        Serial.print(key);
        Serial.print(msgs[key]);
        Serial.println();
      }
      if (key == 0)
      {
          pulse = pulseIn(pwPin, HIGH);
          level = pulse / 147;
          Serial.print(level);
          lcd.cursorTo(2,0);                       // I can't print the value
          lcd.printIn("Level: ");                  // 'level' to the LCD
          lcd.print(level);                          //  here
        
      }  
    }
  }
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
  int k;
  for (k = 0; k < NUM_KEYS; k++)
  {
    if (input < adc_key_val[k])
    {
      return k;
    }
  }
  if (k >= NUM_KEYS)
    k = -1;     // No valid key pressed
  return k;
}

I tried using the atoi() function with no success. Any help would be appreciated… I’m trying to make an LCD with a menu which is what most of the code is.

Does lcd.print(level, DEC); work?

I tried using the atoi() function with no success.

Well, of course not. atoi() is the ASCII to Integer function. You want the Integer to ASCII function. Can you guess what it's name is?

Google itoa if you need a hint.

Well, of course not. atoi() is the ASCII to Integer function. You want the Integer to ASCII function. Can you guess what it's name is?

Ok, so I tried the itoa() function, but I'm confused on the parameters of the function: itoa(int, char, int)

          pulse = pulseIn(pwPin, HIGH);
          level = pulse / 147;
          itoa(newLevel, level, 10);
          Serial.print(level);
          lcd.cursorTo(2,0);                       
          lcd.printIn("Level: ");                  
          lcd.print(newLevel);

It tells me I have an invalid conversion from char to constant char.

Does lcd.print(level, DEC); work?

No that gave me a no matching function error

Please don't post code snippets that do not describe the whole problem. When I look at that snippet, the error clearly refers to the itoa line, but the key part of the message refers to the first argument, newLevel, and its type, but you haven't shown the declaration for newLevel.

//example use of LCD4Bit_mod library
#include <LCD4Bit_mod.h>
//create object to control an LCD.  
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);

//Key message
char msgs[5][15] = {"  Menu 1     ",
                    "Up Key OK    ",
                    "Down Key OK  ",
                    "  Menu 2     ",
                    "Select Key OK" };
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
const int pwPin = 3;
long pulse, inches, cm;
int level;
int newLevel;

void setup()
{
  Serial.begin(9600);
  lcd.init();
  lcd.clear();
  lcd.printIn("Ultrasonic Level");
    
}

void loop()
{
  //char val[4];
  adc_key_in = analogRead(0);      // read the value from the sensor  
  key = get_key(adc_key_in);         // convert into key press
  if (key != oldkey)               // if keypress is detected
  {
    delay(50);                       // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor  
    key = get_key(adc_key_in);         // convert into key press
    if (key != oldkey)
    {
      oldkey = key;
      if (key >=0)
      {
        lcd.clear();
        lcd.cursorTo(1, 0);  //line=2, x=0
        lcd.printIn(msgs[key]);
        Serial.print(key);
        Serial.print(msgs[key]);
        Serial.println();
      }
      if (key == 0)
      {
          pulse = pulseIn(pwPin, HIGH);
          level = pulse / 147;
          itoa(newLevel, level, 10);
          Serial.print(level);
          lcd.cursorTo(2,0);                       // I can't print the value
          lcd.printIn("Level: ");                  // 'level' to the LCD
          lcd.print(newLevel);                          //  here
        
      }  
    }
  }
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
  int k;
  for (k = 0; k < NUM_KEYS; k++)
  {
    if (input < adc_key_val[k])
    {
      return k;
    }
  }
  if (k >= NUM_KEYS)
    k = -1;     // No valid key pressed
  return k;
}

I know the data type for level is wrong, since itoa() needs a char…

Have a look at: http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ There's a nice example on that page.

Ah, that did help...works good now, thanks!

Quote: Does lcd.print(level, DEC); work?

No that gave me a no matching function error

I thought the LCD and Serial library print functions had been aligned now - which version of the IDE are you using.