LCD sensor read inaccuracy

I uploaded an LCD menu to read the value of two potentiometers. I also hooked up a button to select between the two readings. However, the first reading is fine, reading 0 to 1023 as expected.

Number two reads with an extra 0 so it reads 0 to 10233.

Also, if I adjust the first pot to say, any reading like 500 as example, then the second pot reading (if pot is set to 0) starts at 500.

I put a 10k pick up resistor on the button and it helped a little but still get inaccurate reading on second pot.

Last, when I adjust a pot, and attempt to set it back to zero, it never does, it reads a small input.

Ideas?

#include <LiquidCrystal.h>

//index the pins with the library
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int potPin1 = A1;
int potPin2 = A2;
int button1 = 2;

int buttonVal1 = 0;

int menuCount = 0;

void setup()
{
//set LCD columns and rows
lcd. begin(16, 2);
lcd.clear();

pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(button1, INPUT);

digitalWrite(button1, HIGH);

lcd.setCursor(0, 0); //sets cursor to col0 and row 0
lcd.print("Press button "); //prints to LCD
lcd.setCursor(0, 1);
lcd.print(“to view pot val”);
}

void loop()
{
buttonVal1 = digitalRead(button1);

if (buttonVal1 == LOW)
{
menuCount++;
}
switch (menuCount)
{
case 1:
lcd.setCursor(0,0);
lcd.print("SensorVal1: “);
lcd.print(analogRead(potPin1));
lcd. setCursor(0,1);
lcd.print(” ");
delay(250);
break;
case 2:
lcd.setCursor(0,0);
lcd.print("SensorVal2 “);
lcd.print(analogRead(potPin2));
lcd.setCursor(0,1);
lcd.print(” ");
delay(250);
break;
case 3:
lcd.setCursor(0,0);
lcd.print("Press button ");
lcd.setCursor(0,1);
lcd.print(“to view pot val”);
delay(250);
break;
default:
menuCount = 0;
break;
}
}

You aren't erasing the whole line, and the legend for your second output is one character shorter than the first legend which means the subsequent number (1023) will be printed one character to the left, leaving the last digit (3) of the first value still on display.

    lcd.print("SensorVal1: ");
    lcd.print("SensorVal2 ");

Good eye, thanks Pete. I will adjust.

Pete, I adjusted the shortened sensorval2, and I no longer have the extra zero.

What did you mean by not erasing the whole line?

Thanks

I use a small function to clear a line before I print to it.

void lcdClearLine(byte line) {
lcd.setCursor(0,line);
lcd.print("                    "); // number of spaces as width (20 for mine)
lcd,setCursor(0,line);
}

Or perhaps more to the point, make sure what you write every time is the same length, padding it with as many trailing blanks as necessary.

I agree with Paul in that keeping track and padding is more efficient and leads to less flicker, but my memory is becoming more and more “write only” so I just clear the line and my memory issue becomes moot.

groundfungus: I agree with Paul in that keeping track and padding is more efficient and leads to less flicker, but my memory is becoming more and more "write only" so I just clear the line and my memory issue becomes moot.

You can write a function that accepts the output string as an argument. The function can write the string, use strlen() to determine how long it was, and based on that information and the width of the display, loops to print an appropriate number of spaces.

As far as spaces go, I have fixed it with your suggestions, but the pot still displays input when I dial back down to zero. It still displays the input when switching over to the second sensor val. I am thinking it is the pot itself, but then why would it transfer over to the second sensor read? I am using 10k pots.