IR and LCD Bidirectional Counter

Hi,

I am making a bidirectional counter. I want the screen to display two pieces of information, A total visit count on the first line and a current visit count on the second. for example:

“Total Entries: 100
Current People in room: 10”

However I can not get the two to display simultaneously, each time the IR sensor detects an object. The “Total entry” text disappears. I have attached the code that just displays “person in room” on the second line. If someone could please let me know how to have the two messages displayed at the same time it would be greatly appreciated.

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

#define in 14
#define out 19
#define relay 2

int count=0;



void IN()
{
    count++;
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Person In Room:");
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Person In Room:");
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  
}
void loop()
{
  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();
  
  
  else
    digitalWrite(relay, HIGH);

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Your program spends nearly all of the time in the delay()s doing nothing. The begiiner's guide to millis() shows how to use millis() for timing, making the program much much more responsive. The several things at a time tutorial may help you. as well.

The IN() and OUT() functions would be called over and over while the sensor pin remains high. If it were not for the delay()s the count would increase or decrease rapidly. Use the state change detection method to detect the transition from low to high only once per activation of the sensor.

Don't use lcd.clear() in the loop() code. The clear() function can cause flickering. Instead print spaces to overwrite old data, shift the cursor back and write the new data. Note: Person In Room: is 15 characters leaving only 1 place for count. You may want to shorted that so more characters are available for the count. Then pad the line with spaces to overwrite old data.

void IN()
{
    count++;
    //lcd.clear();   *** get rid of clear
    lcd.setCursor(0,1);
    lcd.print("Person In Room: ");  // Person In Room: is 15 characters leaving only 1 place for count
    lcd.setCursor(15,1);
    lcd.print(count);
    //delay(1000);  // get rid of delay()
}

And while we are at it, go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code using the “</>” icon, not “quote”. It is harder to read - and use - unless correctly posted.

@chris369, do not cross-post. Other thread removed.