Printing analogRead value to LCD

I’m trying to use an LCD to display data from a sensor (photoresistor), but I’m having a problem. My code is as follows.

#include <LCD4Bit.h>

LCD4Bit lcd = LCD4Bit(1);

int sensorpin = 2;
int val;

void setup()
{
  lcd.init();
}

void loop()
{
  val = analogRead(sensorpin);
  lcd.clear();
  lcd.print(val);
  delay(100);
}

Everything is connected to the arduino correctly, but when I run the code the LCD just shows random symbols for the first character. I’m guessing that it is not displaying the actual value in numbers, but displaying the character defined by the value. Is there a way to convert the value into characters before I print it?

what library is that? can i see it?

Matt, you need to convert the number to a string and then print it using printIn(). There are various methods for converting numbers to strings that you can find searching the threads on this site.

Or you could try something like this function:

void printNumber(long n){
   byte buf[10];  // prints up to 10 digits  
   byte i=0;
   if(n==0)
         lcd.print('0');
   else{
       if(n < 0){
        lcd.print('-');
            n = -n;
       }
     while(n>0 && i <= 10){
         buf[i++] = n % 10;  // n % base
         n /= 10;   // n/= base
       }
       for(; i >0; i--)
             lcd.print( (char)(buf[i-1] < 10 ? '0' + buf[i-1] : 'A' + buf[i-1] - 10));        
   }
}

Tigrezno, it’s the LCD 4bit library. You can find it from the LCD page in the playground: http://www.arduino.cc/playground/Code/LCD

Thanks, I used "itoa()" and it works great now.