Newbi problems whit displaying values on LCD

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 :wink:

#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

Check to see if your number is > 9 or if your number is > 99 or if your number is > 999, etc.
Then move your starting position by one digit.

I've tried that but it gives me similar problem but whit the "1" off 10 (or 100) being left on the display instead of the "0".

jungelkungen:
I've tried that but it gives me similar problem but whit the "1" off 10 (or 100) being left on the display instead of the "0".

Do you have code for what you supposedly tried?

I wrote it like this:

#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;
        if (encValue <=9){
          lcd.setCursor(10, 0);
          }
        if (encValue >9){
          lcd.setCursor(9, 0);
          }
        if (encValue >99) {
        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

I tryed it for the "adress" displayed on the first row.

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?

Replace:

  lcd.print(encValue);

With:

  if (encValue < 100) lcd.print(" ");
  if (encValue < 10) lcd.print(" ");                  
  lcd.print(encValue);

Is there a way to "clear" parts of the display ( lcd.clear(); ) will clear all the LCD.

Yes:

  • Position the cursor.
  • Print a space or several spaces.
  • Reposition the cursor ir required.

Don

floresta:
Replace:

  lcd.print(encValue);

With:

  if (encValue < 100) lcd.print(" ");

if (encValue < 10) lcd.print(" ");                  
 lcd.print(encValue);





Yes:

- Position the cursor.
- Print a space or several spaces.
- Reposition the cursor ir required.

Don

Thx alot, this solved my problem!