4
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int visitor=0;  //variable for counting visitor number

unsigned long timer_1,timer_2;  //variables for recording time for ir_1 and ir_2

void setup() {
   lcd.begin(20,4);
   pinMode(A0,INPUT);
   pinMode(A1,INPUT);
}


void loop(){
 lcd.setCursor(0,4);
   lcd.print("visitor=");
   lcd.setCursor(8, 4);

   lcd.print(visitor);

   if(digitalRead(A0)==1 && digitalRead(A1)==0)
  {
    timer_1= millis();
  }
  if(digitalRead(A0)==0 && digitalRead(A1)==1)
  {
    timer_2= millis();
  }
  if(timer_1>timer_2)
  {
   lcd.setCursor(8,4);
   visitor=visitor+1;
   lcd.print(visitor);
   delay(2000);
   timer_1=0;
   timer_2=0;
  } 
 else if(timer_1<timer_2)
  {
   lcd.setCursor(8,4);
   visitor=visitor-1;
    if (visitor<=0)
  { 
    visitor=0;
  }
   lcd.print(visitor);
   delay(2000);
   timer_1=0;
   timer_2=0;

  } 

}

Two IR sensors are placed in A0 and A1.

While the value of visitor is decreasing from 10 to 9 the LCD is showing 90 because the '0' of previous 10 is here. How can I remove it?

Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
Rappy Saha
  • 43
  • 7

1 Answers1

2

A simple option would be to call lcd.clear() at the start of loop(). That will clear the entire LCD display. Everything else should work fine because it looks like you're outputting the entire display contents every time round.

Alternatively, you could remove the extra digit by printing a space over it, e.g.:

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

If visitor is a 1-digit number (i.e. less than 10), it will output a space at the start, getting rid of any underlying digits leftover from a previous iteration. If you want the number to be left-aligned then you could print the space after the number instead.

Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87