Clearing an LCD display before each print

Hi

I am trying to print to the display when a button is pressed, and before each print I want the screen to be wiped clean of everything that was there before so that there are no leftover characters from the previous print, however when I try to do this it keeps cleaning then printing, the end result being an incredibly fast flickering between clean and the printed text.

How do I solve this?

//Testing the use of addition and subtraction with the menu options.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);         


int lcd_key     = 0;
int adc_key_in  = 0;
int calc_total;
int display_options;
bool clean_display = false;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int read_LCD_buttons(){              
    adc_key_in = analogRead(0);     

  

    if (adc_key_in > 1000) return btnNONE; 


    if (adc_key_in < 50)   return btnRIGHT;  
    if (adc_key_in < 150)  return btnUP; 
    if (adc_key_in < 300)  return btnDOWN; 
    if (adc_key_in < 450)  return btnLEFT; 
    if (adc_key_in < 700)  return btnSELECT;  

  

    return btnNONE;      
    
}

void setup(){
   lcd.begin(16, 2);              
   lcd.setCursor(0,0);    
   lcd.print (calc_total);  
       
}
 
void loop(){
   
   lcd_key = read_LCD_buttons();  
   

   switch (lcd_key){              
    
       case btnDOWN:{ 
            calc_total = calc_total - 1;
            calc_total = constrain(calc_total,-5, 5);
            delay(500); 
            break;
       }
       case btnUP:{
            calc_total = calc_total + 1;
            calc_total = constrain(calc_total,-5, 5);
            delay(500);
            break;
       }
     }

    if (clean_display = true){
      lcd.setCursor(0,0); 
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("                ");       
    }

    if (calc_total == 0){
      clean_display = true;
      delay(100);
      clean_display = false;
      lcd.setCursor(0,0);
      lcd.print("Menu Home");
    }
    if (calc_total == 1){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 1");
    }
    if (calc_total == 2){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 2 ");
    }
    if (calc_total == 3){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 3");
    }
    if (calc_total == 4){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 4");
    }
    if (calc_total == 5){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 5");
    }
    if (calc_total == -1){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 1");
    }
    if (calc_total == -2){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 2");
    }
    if (calc_total == -3){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 3");
    }
    if (calc_total == -4){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 4");
    }
    if (calc_total == -5){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 5");
    }
    
    
}

Any help you can give will be greatly appreciated.

Liam

Hi,
This

      clean_display = true;
      delay(100);
      clean_display = f

does not work. You have a single loop. Just set clean_display = true; and then inside Clean_Display, set it false..

    if (clean_display = true){
      lcd.setCursor(0,0); 
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("                ");       
    }

= is not the same as ==
Assuming you meant ==, clean_display will never be true here.

  if (calc_total == 0){
      clean_display = true;
      delay(100);
      clean_display = false;
      lcd.setCursor(0,0);
      lcd.print("Menu Home");
    }

You set clean_display to true, do nothing for 0.1 second, then set it to false.

Why not use lcd.clear()? And only when the display needs to print something new.

I tried using lcd.clear(); initially here:

 if (calc_total == 0){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Menu Home");
    }

but it kept flashing between clear and the printed text.

I just tried putting lcd.clear(); immediately after the button press:

case btnDOWN:{ 
            lcd.clear();
            calc_total = calc_total - 1;
            calc_total = constrain(calc_total,-5, 5);
            delay(500); 
            break;

and it seems to work.

Thanks for your pointers

Liam

Once you have got the program working based on the problems pointed out then consider improving it.

For instance

    if (calc_total == 1)
    {
      lcd.setCursor(0, 0);
      lcd.print("Flash X 1");
    }
    if (calc_total == 2)
    {
      lcd.setCursor(0, 0);
      lcd.print("Flash X 2 ");
    }
    if (calc_total == 3)
    {
      lcd.setCursor(0, 0);
      lcd.print("Flash X 3");
    }
    if (calc_total == 4)
    {
      lcd.setCursor(0, 0);
      lcd.print("Flash X 4");
    }
    if (calc_total == 5)
    {
      lcd.setCursor(0, 0);
      lcd.print("Flash X 5");
    }

If calc_total is between 1 and 5 you print the same text followed by the value of calc_total which you could do in 4 lines of code.