Flickering LCD

I’m doing an automated car park for our project in electronics. My project basically lets cars in until an x amount of time and when the maximum is reached it will stop letting cars in until another car leaves the parking.

I’m using 2 LDRs for entrance and exit. The program works apart from the LCD flickering when the first loop is done verifying. The first loop is when it counts how many cars have come in and pausing the operation until another car comes out.

The “Car Park Full” message is the one that flickers, but the second loop which is the “SPACES AVAILABLE” doesn’t.

Anything I’m doing wrong in here?

#include <LiquidCrystal.h>
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12);
#include <Servo.h>
Servo servo1;                 //declaring the name of the servo


#define ServoM      5         //servo connected to pin 5
#define ledred      4         //red LED connected to pin 4
#define ledgreen    3         //green LED connected to pin 3
#define In          A0        //LDR 1(entrance) connected to A0
#define Exit        A1        //LDR 2(exit) connected to A1
#define BarLow      110       //servo starting postion
#define BarUp       20        //servo open position
#define CAPACITY     2        //car park maximum capacity

void setup(){
  Serial.begin(9600);   
  servo1.attach(5);           //declaring servo is attached to pin 5
  pinMode(In, INPUT);         // 
  pinMode(Exit, INPUT);
  pinMode(ledred, OUTPUT);
  pinMode(ledgreen, OUTPUT);
  servo1.write(BarLow);       // default position of servo
  
  
  digitalWrite(ledred, HIGH);   //default status of the LED
  lcd.begin(16,2);
  lcd.print("CAR PARK");        //
  delay(1000);
 
  }


  int Available;
  
  void loop(){
    
    if(analogRead(In)<450)
      
    {
      if(Available != CAPACITY)       //
      {
        Available++;                  //LDR counting how many cars have come in
       
        lcd.begin(16,2);
        lcd.print("SPACES AVAILABLE");
        
        servo1.write(BarUp);
      
        digitalWrite(ledgreen, HIGH);
        
        digitalWrite(ledred, LOW);
        delay(2000);
        servo1.write(BarLow);
        digitalWrite(ledred, HIGH);
        digitalWrite(ledgreen, LOW);
      }
      }
      if(Available == CAPACITY)
      {
       servo1.write(BarLow);
        digitalWrite(ledred, HIGH);
        digitalWrite(ledgreen, LOW);
        lcd.begin(16,2);
       lcd.print("Car Park Full");      //keeps flickering when this code is loaded
    }
      
      
      

      
    if(analogRead(Exit)<450)
    {
      if(Available != 0)  //change to 0
      {
        Available--;              // change to --
        servo1.write(BarUp);
        digitalWrite(ledgreen,HIGH);
        digitalWrite(ledred, LOW);
        delay(2000);
        servo1.write(BarLow);
        digitalWrite(ledred, HIGH);
        digitalWrite(ledgreen, LOW);  
      
    }
    if(Available < CAPACITY)
      {
       servo1.write(BarLow);
        digitalWrite(ledred, HIGH);
        digitalWrite(ledgreen, LOW);
        lcd.begin(16,2);
       lcd.print("SPACES AVAILABLE");
    }
    }
    }

You only need to use lcd.begin() once in setup(), not each time through loop(). Personally I would change the program to only print the message only when the status changes then not again.

In the code fragment in loop():

      if(Available != CAPACITY)       //
      {
        Available++;                  //LDR counting how many cars have come in
       
        lcd.begin(16,2);                           // OPPS!
        lcd.print("SPACES AVAILABLE");
        
        servo1.write(BarUp);
      
        digitalWrite(ledgreen, HIGH);
        
        digitalWrite(ledred, LOW);
        delay(2000);
        servo1.write(BarLow);
        digitalWrite(ledred, HIGH);
        digitalWrite(ledgreen, LOW);
      }

as soon as the program finds the closing brace of the if statement block at the bottom of the snippet, the lcd object goes out of scope...it no longer exists. Place the statement lcd.begin() in setup() and remove it from loop().

If you look at Nick Gammon's post at the topic of this Forum of Useful Links, under C++ programming is a discussion of Scope that may help if you're not familiar with it.

as soon as the program finds the closing brace of the if statement block at the bottom of the snippet, the lcd object goes out of scope

No, it doesn't. The lcd object is NOT created in that snippet, so it does not go out of scope when the snippet ends.

PaulS:
No, it doesn't. The lcd object is NOT created in that snippet, so it does not go out of scope when the snippet ends.

No, you're right, it doesn't...I'd have sworn i saw it defined there, too, but maybe I was just seeing things. Still, placing the begin() method call in loop() is not the best place to put it. setup() would make more sense.

luckyuno:
Anything I’m doing wrong in here?

lcd.begin(16,2) HAS ONLY TO BE CALLED ONCE in your program, so put it in the setup() function!

For clearing the lcd lines use lcd.clear() when needed.
But normally no lcd.clear() is needed, when doing fixed length output formatting and cursor positioning using lcd.setCursor() before overwriting old contents with new lcd.print() command.