 # How to clear 3 digit on LCD?

Hello, I’m back with another problem. Using a LCD display I have amps displaying fairly well - except for when the amps roll over 10 amps to display 10.4, then drops back to <10 amps. The 3rd digit “4” doesn’t clear. I have got it to clear by using lcd.clear(); with a 1 millis delay, then printing again.

Maybe the comments will help make clear what I’m trying to say.

``````// variables for input pin and control LED
#include <LiquidCrystal.h>

int amp_pin = 4;   //pin 27

//This is resistors used on for divider circuit
float R1 = 6695.0;    //  resistance of R1
float R2 = 2974.0;     //  resistance of R2

// variable to store the value
float ampraw = 0;
float amp = 0.0;
float ampV = 0;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (RS, Enable, data lines)this meets the include LiquidCrystal.h above

void setup()
{
pinMode(amp_pin, INPUT);  // declaration of pin modes
lcd.begin(16,2); // setup to send to LCD with 16 columns and 2 lines.
}

//this must be inside it's own brackets in "void setup()"
void printTenths(int value)
{
lcd.print(value / 10);
lcd.print(".");
lcd.print(value % 10);
}

void loop()
{
for (int i = 0; i < 20; i++)   // read the value on analog input for the number of spanamp, then devide by 20 to average.
ampraw = ampraw / 20;
ampV = ((ampraw * 4.5) / 1024.0);   //this calculates the actual input voltage on the pin
amp = ampV; // - 2.482;  //2.482 is the offset - at this point amps = zero

//this little section causes display to show A=0.0 steady - without this section, it bounces around above and below zero.
if (amp < .01)
{
amp = 0;
}
else (amp >= .01);

amp = (amp/.04);  //40mA output for each amp of current

lcd.setCursor(0,0);
lcd.print("A=");
printTenths(amp);

lcd.clear(); //when LCD reads more than A=10.4 (3 spaces) then drops back to amps is 9.9 (2 spaces) the 3rd space is not cleared "A=9.94"
delay(1);  //this statement clears the display for 1 millis, then next group prints it back so the 3rd space is cleared, and only reads A=9.9

lcd.setCursor(0,0);
lcd.print("A=");
printTenths(amp);
}
``````

This works, but does cause a slight flicker on the display. Is there a more elegant way of doing this?

Thank ya’ll for any help and suggestions. If anything is seen on other sections of code I’d appreciate help there also.

Ken H.

I think this will fix the problem without the lcd.clear():

``````void printTenths(int value)
{
int units;

units = value / 10;
if (units < 10)
lcd.print(" ");
lcd.print(units);
lcd.print(".");
lcd.print(value % 10);
}
``````

Regards,

-Mike

Thanks Mike - that works like a champ. I like the way it prints better also, the decimal stays at the same spot with your code makes the results look better.

You folks sure are GREAT!! Thanks,

Ken H>

You're welcome, Ken. I'm especially glad to help out a fellow Alabamian.

-Mike

Hi,

Just for info, I had a similar problem the other day with an uptime counter.

I am no programmer, but a pal who is told me to avoid division like the plague as it’s resource hungry if you don’t have a maths co-processor. My sketch is quite big so I took his advice.

01
02
03
.
.
.
10
11
12

``````if (seconds == 0) {
lcd.setCursor(18,3);
lcd.print("00");
} else if (seconds < 10) {
lcd.setCursor(18,3); lcd.print("0"); lcd.print(seconds);
} else if (seconds < 100) {
lcd.setCursor(18,3); lcd.print(seconds);
}
``````

I did similar for days, hours & minutes to give:-

Uptime=####:##:##:##

Ian.

Interesting to avoid division as it's resource hungry - my code is all so small the extra load isn't a problem.... maybe someday my programs will get that large:)

Good idea on how to get leading zeros - simple but very effective - thanks.

Ken H>