Hi, I am pretty new to arduino. I am trying to make an I2C lcd display a number between 690 and 0, but I have a problem. When i plug in the arduino and turn the potentiometer all the way to the right and all the way to the left, the lcd leaves unnecessary digits on its display. Can anybody help me with this?
Here is the code that i am using:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
// put your setup code here, to run once:
pinMode(INPUT, A1);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(A1);
lcd.print(sensorValue);
lcd.setCursor(0,0);
}
Here is an example that only changes the data that can change, leaving the permanent labels alone. Spaces are used to overwrite old data ala @LarryD, but only the necessary area. One step further is to only write the changeable data when the data changes value. I leave that to you.
// hd44780 library see https://github.com/duinoWitchery/hd44780
// thehd44780 library is available through the IDE library manager
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
const byte pot1Pin = A0;
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup()
{
Serial.begin(115200);
lcd.begin(LCD_COLS, LCD_ROWS);
// permanent labels
lcd.setCursor(0, 0);
lcd.print("ADC");
lcd.setCursor(9, 0);
lcd.print("COUNTS");
}
void loop()
{
updateLCD();
}
void updateLCD()
{
static unsigned long lcdTimer = 0;
unsigned long lcdInterval = 500; // update 2 times per second
if (millis() - lcdTimer >= lcdInterval)
{
lcdTimer = millis();
int adcCounts = analogRead(pot1Pin);
//only write data that changes
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(adcCounts);
}
}
The display is updated every 1/2 second. Plenty fast for humans to read.
I use the hd44780 to avoid confusion and because it is the better LCD library. The hd44780 library is available via the IDE library manager.