Problem displaying variables on lcd screen

Good evening! :blush: I need help with this problem.
I have a little problem with my project, I'm new at this so from now on, I apologize if the code is messy and buggy. I'm still learning:D

My project is simple, it consists of a program that turns on a water pump, when certain conditions are met according to the humidity, light and rain sensors.

The problem that arose when I tried to include an LCD screen to the project.
I want to show the values that the sensors register in the lcd 16x2. To make it a little more fun I included a small animation at the beginning, what I wanted to achieve in the lcd screen was the following:
*- Have a fun animation displayed at startup.
- Then show an interface where the values recorded by the three sensors are shown and S.R = OFF/ON, to represent if the system is active or not..

My problem arose at the time when the values are printed on the LCD, because as I am working with numbers from 0 to 100, when it reaches 100 and then marks a value less than this, some characters remain on the screen (attached an image of how the error looks and the respective code).

Thank you very much in advance for your answer.
Un saludo, que tengan un buen día:D :orange_heart:

Error: Description (As you can see, in the image there are some characters that I do not want, such as the "%%" that are in excess, and it only happens when the value recorded by the sensors gets to work with two digits -increases one more "%"-, or when it reaches 100, i.e. when the sensor records a value of three digits -increases two "%%"-)

image

Code: (I know there are variables declared unnecessarily, I am still in the process of testing, that's why you will see some things that don't make sense, I have been trying possible solutions but no help yet)


// libreria para pantalla lcd
#include <LiquidCrystal.h>

//Declaramos los pines para la lcd
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);   

//ANIMACIÓN DE ENTRADA

// Reloj de Arena 

byte load1[8] = {
  B01111,
  B01101,
  B00100,
  B00100,
  B00110,
  B00011,
  B00001,
  B00000
};
byte load2[8] = {
  B11111,
  B01010,
  B10101,
  B01010,
  B00100,
  B00000,
  B10101,
  B11011
};
byte load3[8] = {
  B11110,
  B10110,
  B00100,
  B00100,
  B01100,
  B11000,
  B10000,
  B00000
};
byte load4[8] = {
  B00000,
  B00001,
  B00011,
  B00110,
  B00100,
  B00100,
  B01100,
  B01111
};
byte load5[8] = {
  B11011,
  B10001,
  B00100,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111
};
byte load6[8] = {
  B00000,
  B10000,
  B11000,
  B01100,
  B00100,
  B00100,
  B00110,
  B11110
};
//////////// fin  pre-load //////////////

///////// animacion 1 ////////////////
byte load1a[8] = {
  B01111,
  B01100,
  B00100,
  B00100,
  B00110,
  B00011,
  B00001,
  B00000
};
byte load2a[8] = {
  B11111,
  B01010,
  B10101,
  B01010,
  B00100,
  B00000,
  B10001,
  B11011
};
byte load5a[8] = {
  B11011,
  B10101,
  B00000,
  B00000,
  B00000,
  B00000,
  B01000,
  B11111
};
/////////////////ani2/////////

byte load2b[8] = {
  B11111,
  B01000,
  B10101,
  B01010,
  B00100,
  B00000,
  B10101,
  B11011
};
byte load5b[8] = {
  B11011,
  B10001,
  B00100,
  B00000,
  B00000,
  B00000,
  B01010,
  B11111
};
///////////ani3//////////////
// load1a
byte load2c[8] = {
  B11111,
  B01000,
  B10101,
  B01010,
  B00100,
  B00000,
  B10001,
  B11011
};
byte load3c[8] = {
  B11110,
  B00110,
  B00100,
  B00100,
  B01100,
  B11000,
  B10000,
  B00000
};
byte load5c[8] = {
  B11011,
  B10101,
  B00000,
  B00000,
  B00000,
  B00100,
  B01010,
  B11111
};
////////// ani4////////////

byte load2d[] = {
  B11111,
  B00000,
  B10101,
  B01010,
  B00100,
  B00000,
  B10101,
  B11011
};

byte load5d[8] = {
  B11011,
  B10001,
  B00100,
  B00000,
  B00000,
  B00100,
  B01010,
  B11111
};
byte load6d[8] = {
  B00000,
  B10000,
  B11000,
  B01100,
  B00100,
  B00100,
  B10110,
  B11110
};
////////ani 5 ////////

byte load2e[8] = {
  B11111,
  B00000,
  B10001,
  B01010,
  B00100,
  B00000,
  B10001,
  B11011
 };

byte load5e[8] = {
  B11011,
  B10101,
  B00000,
  B00000,
  B00000,
  B10100,
  B01010,
  B11111
};

//////// ani6 ///////
byte load2f[8] = {
  B11111,
  B00000,
  B00001,
  B01010,
  B00100,
  B00000,
  B10101,
  B11011
};
byte load4f[8] = {
  B00000,
  B00001,
  B00011,
  B00110,
  B00100,
  B00100,
  B01101,
  B01111
};
byte load5f[8] = {
  B11011,
  B10001,
  B00100,
  B00000,
  B00000,
  B10100,
  B01010,
  B11111
};
/////// ani7 /////////
byte load2g[8] = {
  B11111,
  B00000,
  B00000,
  B01010,
  B00100,
  B00000,
  B10001,
  B11011
};
byte load5g[8] = {
  B11011,
  B10101,
  B00000,
  B00000,
  B00000,
  B10101,
  B01010,
  B11111
};
///////ani8//////////

byte load2h[8] = {
  B11111,
  B00000,
  B00000,
  B01000,
  B00100,
  B00000,
  B10101,
  B11011
};
byte load5h[8] = {
  B11011,
  B10001,
  B00100,
  B00000,
  B00010,
  B10101,
  B01010,
  B11111
};
///////// ani 9 //////
byte load2i[8] = {
  B11111,
  B00000,
  B00000,
  B00000,
  B00100,
  B00000,
  B10101,
  B11011
};
byte load5i[8] = {
  B11011,
  B10001,
  B00100,
  B00000,
  B01010,
  B10101,
  B01010,
  B11111
};
////////ani10/////////

byte load2j[8] = {
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B10001,
  B11011
};
byte load5j[8] = {
  B11011,
  B10101,
  B00000,
  B00100,
  B01010,
  B10101,
  B01010,
  B11111
};
///////giro1////
byte giro1[8] = {
  B11000,
  B10000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
byte giro2[8] = {
  B10000,
  B01000,
  B00100,
  B00100,
  B00100,
  B00100,
  B00110,
  B00011
};
byte giro3[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11110
};
byte giro4[8] = {
  B01111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
byte giro5[8] = {
  B10000,
  B11000,
  B01000,
  B01010,
  B01001,
  B01010,
  B00101,
  B00010
};
byte giro6[8] = {
  B00001,
  B00000,
  B00000,
  B10101,
  B01010,
  B00100,
  B01001,
  B10010
};
/// giro2///
byte giro1a[8] = {
  B00000,
  B00000,
  B11000,
  B11110,
  B10011,
  B10000,
  B10000,
  B10000
};
byte giro2a[8] = {
  B00000,
  B00000,
  B00000,
  B00001,
  B10011,
  B11110,
  B01100,
  B00000
};
byte giro3a[8] = {
  B00000,
  B00011,
  B01111,
  B11001,
  B10011,
  B00001,
  B00011,
  B00001
};
byte giro4a[8] = {
  B10000,
  B10000,
  B10001,
  B10011,
  B11110,
  B11000,
  B00000,
  B00000
};
byte giro5a[8] = {
  B01100,
  B11110,
  B10011,
  B00001,
  B00000,
  B00000,
  B00000,
  B00000
};
byte giro6a[8] = {
  B01011,
  B00101,
  B10011,
  B11101,
  B01011,
  B00111,
  B00000,
  B00000
};
///// giro3///
byte giro1b[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B01111
};
byte giro2b[8] = {
  B00001,
  B00010,
  B00100,
  B00100,
  B00100,
  B00100,
  B01100,
  B11000
};
byte giro3b[8] = {
  B00011,
  B00001,
  B00000,
  B00101,
  B01010,
  B10101,
  B01010,
  B10101
};
byte giro4b[8] = {
  B11000,
  B10000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00001,
  B10011
};
byte giro5b[8] = {
  B00011,
  B00110,
  B00100,
  B00100,
  B00100,
  B00100,
  B01000,
  B10000
};
byte giro6b[8] = {
  B11110,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

int tiempo = 250;
int t = 100;
// AQUÍ ACABA ANIMACIÓN DE ENTRADA

//Declaramos variables para el funcionamiento del sistema de riego
int pin_bomba = 8;
int pin_sensor_luz = A0;
int pin_sensor_humedad = A1;
int pin_sensor_lluvia = A2;
int ultima_distancia_L = 0;
int ultima_distancia_H = 0;
int ultima_distancia_LL = 0;

void setup()
{
  //Para el lcd
  // Definimos las dimensiones de la pantalla
  lcd.begin(16, 2);

  //Definimos las salidas para los pines
  Serial.begin(9600);
  pinMode(pin_bomba, OUTPUT);	
  pinMode(pin_sensor_luz, INPUT);
  pinMode(pin_sensor_humedad, INPUT);
  pinMode(pin_sensor_lluvia,INPUT);
  
  letras();
  
  for(int i = 0; i<4; i++){
    preLoad(); 
  }
  
  delay(500);
  lcd.clear();

}

void loop()
{

  //******************************************************
  //Variables para guardar los datos recibidos por los sensores
  //******************************************************

  
 int cantidad_luz = analogRead(pin_sensor_luz); // leemos y guardamos la entrada del sensor de luz
 int cantidad_luz_porcentaje = map(cantidad_luz, 0, 679, 0, 100); //convertimos la cantidad de luz en porcentaje:D

 int humedad = analogRead(pin_sensor_humedad);	// leemos y guardamos la entrada del sensor de humedad
 int humedad_porcentaje = map(humedad, 0, 1023, 0, 100); //convertimos la cantidad de humedad en porcentaje:D

 int lluvia = analogRead(pin_sensor_lluvia);	// leemos y guardamos la entrada del sensor de lluvia
 int lluvia_porcentaje = map(lluvia, 0, 1023, 0, 100);	//convertimos la cantidad de lluvia detectada en porcentaje:D
   
  
  //******************
  // PARA EL LCD
  //******************
    lcd.setCursor(0, 0);
 	lcd.print("LUZ:");
 	lcd.print(cantidad_luz_porcentaje);
    lcd.print("%");
    lcd.setCursor(0, 1);
 	lcd.print("HUM:");
 	lcd.print(humedad_porcentaje);
    lcd.print("%");
    lcd.setCursor(9, 0);
 	lcd.print("LL:");
 	lcd.print(lluvia_porcentaje);
    lcd.print("%");
    
 	ultima_distancia_H = humedad_porcentaje;
 	ultima_distancia_L = cantidad_luz_porcentaje;
    ultima_distancia_LL = lluvia_porcentaje;
  
  delay(100);
  
  
  //******************************************************
  //CONDICIONES DE RIEGO		-- 	CEREBRO DEL PROGRAMA!
  //******************************************************
  // + Se regará siempre y cuando sea de noche (sensor ldr menor al 30%)
  // + Se regará siempre y cuando exista una humedad del suelo inferior al 50% 
  // + Se regará siempre y cuando el sensor de lluvia registre un número inferior del 15%
  // En caso no se cumplan con las tres condiciones
  // El sistema no regará 7u7
  
  
  
  /*
 	int cantidad_luz_porcentaje = map(cantidad_luz, 0, 679, 0, 100); //convertimos la cantidad de luz en porcentaje:D
 	int humedad_porcentaje = map(humedad, 0, 1023, 0, 100); //convertimos la cantidad de humedad en porcentaje:D
	int lluvia_porcentaje = map(lluvia, 0, 1023, 0, 100);	//convertimos la cantidad de lluvia detectada en porcentaje:D
   */
  
  if (cantidad_luz_porcentaje < 30 && humedad_porcentaje < 50 && lluvia_porcentaje < 15){
  	digitalWrite(pin_bomba, HIGH);	// Se enciende la bomba de agua
    lcd.setCursor(9, 1);
    
    lcd.print("SR:ON");
  } else {
    digitalWrite(pin_bomba, LOW);	// Se apaga la bomba de agua
    lcd.setCursor(9, 1);
    lcd.print("SR:OFF");
  }
  
}

 void letras()
  {
   for (int a=15; a>=6;a--)
    {
      lcd.clear();
      lcd.setCursor(a,0);
      lcd.print("Sistema");
      lcd.setCursor(a,1);
      lcd.print("De Riego");
      delay(100);
    }
    
  }
// Animación del reloj de arena que actua como un pre-load
// Se van sobrescribiendo los caracteres creados en cada fotograma.
void preLoad()
{
 
  lcd.createChar(0,load1);
  lcd.createChar(1,load2);
  lcd.createChar(2,load3);
  lcd.createChar(3,load4);
  lcd.createChar(4,load5);
  lcd.createChar(5,load6);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2a);
  lcd.createChar(2,load3);
  lcd.createChar(3,load4);
  lcd.createChar(4,load5a);
  lcd.createChar(5,load6);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);
  
  lcd.createChar(0,load1a);
  lcd.createChar(1,load2b);
  lcd.createChar(2,load3);
  lcd.createChar(3,load4);
  lcd.createChar(4,load5b);
  lcd.createChar(5,load6);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2c);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4);
  lcd.createChar(4,load5c);
  lcd.createChar(5,load6);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2d);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4);
  lcd.createChar(4,load5d);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);
  
 
  lcd.createChar(0,load1a);
  lcd.createChar(1,load2e);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4);
  lcd.createChar(4,load5e);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2f);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4f);
  lcd.createChar(4,load5f);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2g);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4f);
  lcd.createChar(4,load5g);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2h);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4f);
  lcd.createChar(4,load5h);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2i);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4f);
  lcd.createChar(4,load5i);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,load1a);
  lcd.createChar(1,load2j);
  lcd.createChar(2,load3c);
  lcd.createChar(3,load4f);
  lcd.createChar(4,load5j);
  lcd.createChar(5,load6d);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,giro1);
  lcd.createChar(1,giro2);
  lcd.createChar(2,giro3);
  lcd.createChar(3,giro4);
  lcd.createChar(4,giro5);
  lcd.createChar(5,giro6);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);
  
  lcd.createChar(0,giro1a);
  lcd.createChar(1,giro2a);
  lcd.createChar(2,giro3a);
  lcd.createChar(3,giro4a);
  lcd.createChar(4,giro5a);
  lcd.createChar(5,giro6a);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);

  lcd.createChar(0,giro1b);
  lcd.createChar(1,giro2b);
  lcd.createChar(2,giro3b);
  lcd.createChar(3,giro4b);
  lcd.createChar(4,giro5b);
  lcd.createChar(5,giro6b);
  lcd.setCursor(2,0);
  lcd.write(byte(0));
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(2,1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  delay(t);
}

The LCD doesn't clear the previous text.
It just writes from above.
Please overwrite whitespace to where character displayed before.

Also, clear before writing a new value and write everything new.
But this can cause flicker.

If you want to overwrite only the required part, print a whitespace in the part you want to erase.

Thank you very much for your response. :blush: :orange_heart:

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

What?!
I think the OP was using code tags properly at the time I saw it.
I don't know why, but later it was changed to without a code tag... :roll_eyes:

Hello
you have to aligne the values to be printed as follows:

// prototyping
if (value<100) lcd.print(" ");
if (value<10) lcd.print(" ");
lcd.print(value);

Have a nice day.

1 Like

He/she changed their post three times, adding and removing code tags as he/she went but they got there in the end