Arduino car parking lot

Hello fellas,
I’m quite new the Arduino world and I’ve recently started working on a project as the title suggests, it is a car parking lot which consists: Barrier(Simple Servo motor), Sensor(TCRT5000) and LCD to show how many empty places are left (if any).

I encountered an annoying bug where if a vehicle is standing in front of the barrier and the sensor recognizes it, it lifts the barrier up and every 3 seconds checks it’s position again, if it is still there it won’t lower the barrier until the vehicle passes, however, every 3 seconds that the same vehicle is still standing there my “empty places counter” is reducing by 1.
Attaching my code, thanks in advance.

arduinoservolcd5.ino (689 Bytes)

Of course it does!

Every round through loop, you check if there’s a car, if so, you move the servo, delay for 3 seconds, go into an infinite loop if there are no slots (which seems like incorrect behavior too, assuming this parking lot allows vehicles to leave), and then decrement the number of slots.

if (tcrt5000 < 300)
  
  {
    
servo.write(160);    
    delay(3000);
   
    
   while (slots==0)
   
   {
    
   servo.write(67);
   lcd.setCursor(0,0);
   lcd.print("GARAGE IS FULL!");
   
  }
  slots--;
  }

You haven’t defined what behavior you want - but I think you’re going to need a few more global or static variables to track the state of things; you only want to decrement the number of slots once the car moves off the sensor (assuming it’s in front of the gate), right? So track when a car is there, and when there isn’t one, and when it changes from there being a car there to not being there, then, and only then, do you decrement the number of slots.

The arduino parking lot seems to be a popular school assignment, just like the traffic light…

mhmm.. To be honest I struggle understanding what you just said, could you elaborate? Also I believe I have done a mistake using the while loop as I plan to put the 2nd sensor after this one is completed and that's when the real struggle will begin because I used this loop.

MichaelThe2nd: mhmm.. To be honest I struggle understanding what you just said, could you elaborate?

How about if you investigate instead?

aarg:
How about if you investigate instead?

I would love to but the problem is that the teacher is really bad and he doesn’t know Arduino himself, English is not my 1st language so I also have problems with watching tutorials.

Does the teacher know C or C++ (or a similar language)?

Here's a hint: get your sketch to tell you what it can see, and what it is doing.

AWOL: Here's a hint: get your sketch to tell you what it can see, and what it is doing.

Translation: Imagine that you are the processor, moving through the code. At each step, think about what has changed and what has stayed the same. It's called a "walk through".

One more thing - try to define the entire problem before you start programming. Understand and sketch the logic flow on the back of an old envelope before you begin. It makes dealing with the little details easier.

...but add some debug prints too.

MichaelThe2nd: I also have problems with watching tutorials.

The better tutorials tend to be the written ones. Those you can read and re-read, you can copy/paste bits of code, etc. Much better than video tutorials which are either boringly slow, or going too fast.

AWOL: Here's a hint: get your sketch to tell you what it can see, and what it is doing.

To elaborate: that means "add Serial.println() statements. Lots of them." The favourite debugging method of programmers big and small. Just print out the value of a variable every time you do something with it, and you often very soon discover where it goes wrong.

MichaelThe2nd: English is not my 1st language so I also have problems with watching tutorials.

Your English is good.

We see far worse on here from people who have English as their first language, it seems they just cannot be bothered to post in a style where they will be clearly understood.