LCD displaying analogRead() values.

I'm using this code to read the a value from a potentiometer and write it to an LCD character display. It is part of a larger sketch that I use to control my coil gun.

//Read in the value of the potentiometer and map it do the pulse time.
  pulse_time = map(analogRead(timer_pin),0,1024,0,200);
   
  //Output pulse time to LCD screen. 
  lcd.setCursor(7,1);
  lcd.print(pulse_time);

However, the values displayed on the screen are irregular. When rotating the pot, the value increase rapidly from 0 - 900 withing the first half of the rotation. Then the values go back to 100 and start incrementing gradually until the pot hit's it's upper limit. What's wrong?

Is it a linear pot?

What does a multimeter show?

Yup.. it's a linear pot. The multimeter shows the correct values for voltage.

I don't think the problem is in the map function anymore.. I just tried writing the (analogRead value/ 4) to the LCD.. I get the same problem.

Ok.. I figured out what went wrong.
Basically, what was happening was that in the first part of the potentiometers scale, the numbers output to the LCD were two-digit ones. Then, after the mid point, the numbers become three digit. (i.e 99 --> 100).
When I turned the pot back down from 3 digit numbers to the two digit number range, the last 0 (of the 100) was not erased.

So instead of displaying 99, the screen would display 990 where the 0 was just left over from the last number displayed. Since all the numbers lower than 99 were also two-digits long, the 0 remained, making all the small numbers look like they were multiplied by 10.

I hope that made sense.. :wink:

EDIT: Clearing the LCD or printing a row of spaces will introduce a lot of flicker into the display.

For the benefit of anyone else who might have the same problem, here's the solution.

Print a single character after each number. That way, any digits left over from the previous number are overwritten, leaving the display with the proper value.