LCD down counter not removing last 0

Hello I did an up and down counter according to the push buttons but on my way down after going over 10 the 0 is not removed so it says 90, 80, 70 etc. How can I fix it? its my only problem! Thanks :slight_smile:
(The lcd print is almost at the end)

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int fotoEnt=A0;
int fotoSal=A1;

int valEntrada= 0;
int valSalida= 0;

const int botonEnt = 9; // numero del pin del boton
const int botonSal = 13;
const int ledCerrado = 6; //numero del pin del LED
const int ledAbierto = 7;
const int ledCupo = 10;
const int ledSinCupo = 8;

int cupoMax = 15;

int estadoBEnt = 0; // variable para leer estado de pushbuttons
int estadoBSal = 0;
int contador = 0;
int estadoUltimoSal = 0;
int estadoUltimoEnt = 0;

void setup() {
Serial.begin(9600);
lcd.begin(16,2);

// se inicializan LEDS como entradas
pinMode(ledCerrado,OUTPUT);
pinMode(ledAbierto,OUTPUT);

// se inicializan pushbuttons como inputs
pinMode(botonEnt, INPUT);
pinMode(botonSal, INPUT);
}

void loop() {
delay(200);
valEntrada=analogRead(fotoEnt); //Lee valor del foto de Salida
valSalida=analogRead(fotoSal); //Lee valor del foto de Entrada

estadoBEnt = digitalRead(botonEnt);
estadoBSal = digitalRead(botonSal);

if (estadoBEnt != estadoUltimoEnt) {
if((valEntrada<100) && (estadoBEnt == HIGH) && contador<cupoMax)
{
digitalWrite(ledAbierto,HIGH);//LEd se prende.
contador=contador+1;
}
}
else
{
digitalWrite(ledAbierto,LOW);//Led se apaga
}

estadoUltimoEnt = estadoBEnt;
if (estadoBSal != estadoUltimoSal) {
if((valSalida<100) && (estadoBSal == HIGH) && (contador > 0))
{
digitalWrite(ledCerrado,HIGH);//Led se prende
contador=contador-1;
}
}
else
{
digitalWrite(ledCerrado,LOW);//led se apaga
}
estadoUltimoSal = estadoBSal;

/// codigo de cupo
if(contador == cupoMax){
digitalWrite(ledSinCupo,HIGH);//Led de cupo max enciende
} else{
digitalWrite(ledSinCupo,LOW);//Led cupo max se mantiene apagado
}

if(contador < cupoMax){
digitalWrite(ledCupo,HIGH);//Led de cupo max enciende
} else{
digitalWrite(ledCupo,LOW);//Led cupo max se mantiene apagado
}

lcd.setCursor(0,0);
lcd.print("Counter: ");
lcd.setCursor(10,0);
lcd.print(contador); //HERE IS THE PROBLEM

if(contador == cupoMax){
lcd.setCursor(0,1);
lcd.print(“No hay cupo :(”);
}
else{
lcd.setCursor(0,1);
lcd.print(“Si hay cupo :)”);
}
}

Try this:
Put this before your lcd.setCursor(0,0); it will blank the area before the numbers are added. If it blinks use fewer spaces.
lcd.setCursor(0,0);
lcd.print(" "); // 9 spaces extra for bigger numbers

1 Like

it worked! Thankyou very much :slight_smile:

1 Like

You are Welcome! That was an easy one as I have had it happen to me several times. When the line gets to long it will wrap around in a different line, probably not the one expected.