Hi all.
I'm all new to programming and just started my first project.
I'm trying to use a LCD (from DFRobot) and a clickencoder to display some values (address and value) on my display.
I got it all working and making the display count upwards works fine, but as i descend from a value over 10 (or 100) the last digit (a "0") will remain on the LCD.
Is there any "trick" to solve this?
Is there a way to "clear" parts of the display ( lcd.clear(); ) will clear all the LCD.
The code looks like this:
Please also comment if there is anything else you would change
#define WITH_LCD 1
#include <ClickEncoder.h>
#include <TimerOne.h>
#ifdef WITH_LCD
#include <LiquidCrystal.h>
#define LCD_RS 8
#define LCD_RW 2
#define LCD_EN 9
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define LCD_CHARS 16
#define LCD_LINES 2
LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
#endif
ClickEncoder *encoder;
int16_t lastEnc, encValue;
int Adr;
int Val;
int State = 0;
void timerIsr() {
encoder->service();
}
void displayEncValueAdr() {
if(encValue == 0){
lcd.print("All");
}
else{
lcd.print(encValue);
}
Serial.print("EncValue: ");
Serial.println(encValue);
}
void displayEncValueVal() {
lcd.print(encValue);
Serial.print("EncValue: ");
Serial.println(encValue);
}
void setup() {
Serial.begin(9600);
encoder = new ClickEncoder(12, 13, 11, 4);
lcd.begin(LCD_CHARS, LCD_LINES);
lcd.clear();
lcd.print(F("Test rev 0.1"));
delay(1000);
lcd.clear();
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
lastEnc = -1;
encValue = 1;
}
void loop() {
ClickEncoder::Button encButton = encoder->getButton();
// lcd.cursor();
lcd.blink();
switch (State) {
case 0:
lcd.setCursor(0, 0);
lcd.print(F("Adress:"));
lcd.setCursor(0, 1);
lcd.print(F("Value: "));
lcd.setCursor(8, 0);
State = 1;
break;
case 1:
encValue = constrain(encValue, 0, 512);
encValue += encoder->getValue();
if (encValue != lastEnc) {
lastEnc = encValue;
lcd.setCursor(8, 0);
displayEncValueAdr();
}
if (encButton == ClickEncoder::Clicked) {
Adr = encValue;
lcd.setCursor(8, 0);
if(Adr == 0){
lcd.print("All");
}
else{
lcd.print(Adr);
}
lcd.setCursor(8,1);
encValue = 0;
State = 2;
}
break;
case 2:
encValue = constrain(encValue, 0, 255);
encValue += encoder->getValue();
if (encValue != lastEnc) {
lastEnc = encValue;
lcd.setCursor(8, 1);
displayEncValueVal();
}
if (encButton == ClickEncoder::Clicked) {
Val = encValue;
Serial.print(F("\t\t Val "));
Serial.println(Val);
lcd.setCursor(8, 1);
lcd.print(Val);
encValue = 1;
State = 0;
lcd.clear();
}
break;
} //End Main Case
} //End Loop