Parking lot using Arduino UNO. Need help in coding

Hi, as my username describe me best.

So,my project is about sensing the parking lot availability. There are 2 force sensor for In and Out, a Servo, 1 led red and 1 led green, 1 CD4511 7segment decoder, 7-segment display, and 8 ultrasonic sensor which is three pin.

First, the Force sensor_In sense the car and open the boom gate which is connected to Servo if there is still parking bay available if not the boom gate won't open even it sense the car. Everytime the Force sensor_In is triggered, the number display reduce by 1. Then the Force sensor_out sense the car and open the boom gate, each time will add the number display by 1.

As for the Ultrasonic sensor, each parking bay is installed with one, so it can sense 8 parking lot in total. If all parking bay full, Led red is on and Led green is off. If any 1 parking bay is empty, Led red is off and Led green is on.

Here is my code:

#include <Servo.h>

Servo myservo;

int ledgreen = 9;
int ledred = 8;
int Servopin = 12;
int forcesensor_in = A0;
int forcesensor_out = A1;
int lot1 = 0;
int lot2 = 1;
int lot3 = 2;
int lot4 = 3;
int lot5 = 4;
int lot6 = 5;
int lot7 = 6;
int lot8 = 7;

int C;
int C2;
int C3;
int C4;
int C5;
int C6;
int C7;
int C8;

int BarUp = 95;
int BarDown = 177;
int forcevalue_in = 0;
int forcevalue_out = 0;
int Carpark = 8;

unsigned long duration = 0;
unsigned long duration2 = 0;
unsigned long duration3 = 0;
unsigned long duration4 = 0;
unsigned long duration5 = 0;
unsigned long duration6 = 0;
unsigned long duration7 = 0;
unsigned long duration8 = 0;

int segB = A2;
int segC = A3;
int segD = A4;
int segA = A5;

byte segments[10][4] =
// A,B,C,D
// 3,2,1,0
{
  {0,0,0,0},
  {1,0,0,0},
  {0,1,0,0},
  {1,1,0,0},
  {0,0,1,0},
  {1,0,1,0},
  {0,1,1,0},
  {1,1,1,0},
  {0,0,0,1},
  {1,0,0,1},
}; 

void setup() {
  // put your setup code here, to run once:
myservo.attach(Servopin);

pinMode(Servopin,OUTPUT);
pinMode(ledgreen,OUTPUT);
pinMode(ledred,OUTPUT);
pinMode(segA,OUTPUT);
pinMode(segB,OUTPUT);
pinMode(segC,OUTPUT);
pinMode(segD,OUTPUT);
pinMode(forcesensor_in,INPUT);
pinMode(forcesensor_out,INPUT);

Serial.begin(9600);
}

int Available = 8;

void loop() 
{
  loopPart1();
  loopPart2();
}

void loopPart1() 
{
  // put your main code here, to run repeatedly:
pinMode(lot1, OUTPUT);
digitalWrite(lot1,LOW);
delayMicroseconds(2);
digitalWrite(lot1,HIGH);
delayMicroseconds(5);
digitalWrite(lot1,LOW);

pinMode(lot1,INPUT);
duration = pulseIn(lot1,HIGH);
C = duration/58;

pinMode(lot2, OUTPUT);
digitalWrite(lot2,LOW);
delayMicroseconds(2);
digitalWrite(lot2,HIGH);
delayMicroseconds(5);
digitalWrite(lot2,LOW);

pinMode(lot2,INPUT);
duration2 = pulseIn(lot2,HIGH);
C2 = duration2/58;

pinMode(lot3, OUTPUT);
digitalWrite(lot3,LOW);
delayMicroseconds(2);
digitalWrite(lot3,HIGH);
delayMicroseconds(5);
digitalWrite(lot3,LOW);

pinMode(lot3,INPUT);
duration3 = pulseIn(lot3,HIGH);
C3 = duration3/58;

pinMode(lot4, OUTPUT);
digitalWrite(lot4,LOW);
delayMicroseconds(2);
digitalWrite(lot4,HIGH);
delayMicroseconds(5);
digitalWrite(lot4,LOW);

pinMode(lot4,INPUT);
duration4 = pulseIn(lot4,HIGH);
C4 = duration4/58;

pinMode(lot5, OUTPUT);
digitalWrite(lot5,LOW);
delayMicroseconds(2);
digitalWrite(lot5,HIGH);
delayMicroseconds(5);
digitalWrite(lot5,LOW);

pinMode(lot5,INPUT);
duration5 = pulseIn(lot5,HIGH);
C5 = duration5/58;

pinMode(lot6, OUTPUT);
digitalWrite(lot6,LOW);
delayMicroseconds(2);
digitalWrite(lot6,HIGH);
delayMicroseconds(5);
digitalWrite(lot6,LOW);

pinMode(lot6,INPUT);
duration6 = pulseIn(lot6,HIGH);
C6 = duration6/58;

pinMode(lot7, OUTPUT);
digitalWrite(lot7,LOW);
delayMicroseconds(2);
digitalWrite(lot7,HIGH);
delayMicroseconds(5);
digitalWrite(lot7,LOW);

pinMode(lot7,INPUT);
duration7 = pulseIn(lot7,HIGH);
C7 = duration7/58;

pinMode(lot8, OUTPUT);
digitalWrite(lot8,LOW);
delayMicroseconds(2);
digitalWrite(lot8,HIGH);
delayMicroseconds(5);
digitalWrite(lot8,LOW);

pinMode(lot8,INPUT);
duration8 = pulseIn(lot8,HIGH);
C8 = duration8/58;

forcevalue_in = analogRead(forcesensor_in);
int carweight_in = map(forcevalue_in,0,1023,0,255);
forcevalue_out = analogRead(forcesensor_out);
int carweight_out = map(forcevalue_out,0,1023,0,255);

Display(Available);

if(carweight_in > 100)
{
  if(Available != 0)
  {
    Available--;
    myservo.write(BarUp);
    delay(3000);
    myservo.write(BarDown);
  }
}
if(carweight_out > 100)
{
  if(Available != Carpark)
  {
    Available++;
    myservo.write(BarUp);
    delay(3000);
    myservo.write(BarDown);
  }
}
}

void loopPart2()
{
  if( C <= 100 && C2 <= 100 && C3 <= 100 && C4 <= 100 && C5 <= 100 && C6 <= 100 && C7 <= 100 && C8 <= 100)
{
  digitalWrite(ledgreen, LOW);
  digitalWrite(ledred, HIGH);
}
  else
{
  digitalWrite(ledred, LOW);
  digitalWrite(ledgreen, HIGH);
}
}

long microsecondsToInches( long microseconds)
{
  return microseconds /74 /2;
}

long microsecondsToCentimeters( long microseconds)
{
  return microseconds /29 / 2;
}

void Display(int number)
{
byte segs =  ~segments[number][4];        //"~" is used for commom anode.

digitalWrite(segD, bitRead(segs, 0) );
digitalWrite(segC, bitRead(segs, 1) );
digitalWrite(segB, bitRead(segs, 2) );
digitalWrite(segA, bitRead(segs, 3) );
}

Here is my connection:

Pic URL if can't see clearly

In the connection, i replaces the Force sensor to Potential meter cause the website does not have force sensor.

But when i run, the Force sensor_In and Force sensor_Out does not respond, in this case is potentialmeter_In and potentialmeter_Out. The Servo is open every 5 second and close and open again until forever. The 7-segment display can't work. The only working thing is the ultrasensor, which turn on Led red when its full and Led green when its not full.

So, something must be wrong in the code but i can't figure it out. I also notice that pin 0 and pin 1 is not working as well for Arduino UNO.

Is it coding wrong? If yes, please enlighten me.
Is it connection weird? If yes, please enlighten me again.
Why pin 0 and pin 1 can't work? Please enlighten me more! :smiley:

Sorry for the long post.

Erm, do you guys need the simulation? I can post the url, but i think the website need to sign up. The website is "circuit.io"

if there are 8 spaces, count cars in. and cars that exit. no need to monitor if a car is in a space.
if you have two cars that just entered and the lot has 6 cars parked, one that is just pulling out, but not out yet, then the sensor will show 2 open spaces. but in a minute, the one will exit and the other two will take those spots and after that, the lot is full.

if you have a multi story, then you could show how many spaces are open on each level. ....

Thanks for the comment dave-in-nj.

I did the number display is triggered with Force sensor as such that force sensor_In then the number will decrease until 0, force sensor_Out the number will increase until 8. the ultrasonic sensor is required for the project, it is a must. So i let the ultrasonic sensor functioned if all parking is full, red led turn on. This is my intention.

But,i can't get the display work, the servo ain't working as well as force sensor.

Hi,

Welcome to the forum.

How did you develop this code.
Have you started by coding EACH function separately and got them working before combining them?

What are you using as a power supply because there is no way the arduino will be able to provide adequate current for all your peripherals, especially a servo?

Tom... :slight_smile:

Thanks Tom.

I checked online and write it based on my own little knowledge since uni only teach void loop and void setup.

No, i did not try to code separately. Is it a must to run the code one by one and then compile together?
Sorry for asking beginner question here but I'm seriously newbie to arduino and coding.

I added battery 9v to the circuit once connected to the that long bar of dots i don know what it call, the code did not work at all then.

Sorry for unprofessional term.

freakingblurcase:
But,i can't get the display work, the servo ain't working as well as force sensor.

break down the project into bits.

you have a display. can you copy your code into a new sketch and then have it change vaules when you only have 1 sensor ? just show open or closed or high / low or 0/1 when the value of the sensor changes ?

if you can get it to show a change, tehn the display is working, but the program needs to be addressed.

if the display is not working at all, then the display is wired wrong or the sketch to run it is wrong.

you should be able to sum the number of sensors to show how many are high.

Thanks again dave-in-nj.

I did what you recommend. The code did not work. For the 7-segment, only the G-segment is on, the other is off. When i trigger the sensor once, the G-segment did not off as the number should be 7 and for number 7, G-segment do not on.

In short, the number did not reduce and the code did not work as planned.

Here's the code for the display and one force sensor:

int forcesensor_in = A0;
//int forcesensor_out = A1;

int forcevalue_in = 0;
//int forcevalue_out = 0;

int Carpark = 8;

int segB = A2;
int segC = A3;
int segD = A4;
int segA = A5;

byte segments[10][4] =
// A,B,C,D
// 3,2,1,0
{
  {0,0,0,0},
  {1,0,0,0},
  {0,1,0,0},
  {1,1,0,0},
  {0,0,1,0},
  {1,0,1,0},
  {0,1,1,0},
  {1,1,1,0},
  {0,0,0,1},
  {1,0,0,1},
}; 

void setup() {
 
pinMode(segA,OUTPUT);
pinMode(segB,OUTPUT);
pinMode(segC,OUTPUT);
pinMode(segD,OUTPUT);
pinMode(forcesensor_in,INPUT);
//pinMode(forcesensor_out,INPUT);

Serial.begin(9600);
}

int Available = 8;

void loop() 
{forcevalue_in = analogRead(forcesensor_in);
int carweight_in = map(forcevalue_in,0,1023,0,255);
//forcevalue_out = analogRead(forcesensor_out);
//int carweight_out = map(forcevalue_out,0,1023,0,255);

Display(Available);

if(carweight_in > 100)
{
  if(Available != 0)
  {
    Available--;
   // myservo.write(BarUp);
   // delay(3000);
   // myservo.write(BarDown);
  }
}
//if(carweight_out > 100)
//{
 // if(Available != Carpark)
 // {
  //  Available++;
   // myservo.write(BarUp);
   // delay(3000);
   // myservo.write(BarDown);
//  }
//}
}

void Display(int number)
{
byte segs =  ~segments[number][4];        //"~" is used for commom anode.

digitalWrite(segD, bitRead(segs, 0) );
digitalWrite(segC, bitRead(segs, 1) );
digitalWrite(segB, bitRead(segs, 2) );
digitalWrite(segA, bitRead(segs, 3) );
}

For the CD4511 decoder i actually has no idea how to connect it but from what i learn from one of the site, the 3 pin which is LT, BI, and LE should connect to 5v But again, different site show different connection. For the 7-segment display, i pretty sure i connected i right for common anode as i tried working counting down the number before.. Forget about this, i tried each pin connected to ground and i display all segment.But the decoder over current. Still figuring how. Hahahahaha. Over-confident it seem.

For update, the servo and the force sensor work after i change the ultrasonic sensor pin 0 and pin 1 to pin 10 and pin 11.

Here's the general code:

#include <Servo.h>

Servo myservo;

int ledgreen = 9;
int ledred = 8;
int Servopin = 12;
int forcesensor_in = A0;
int forcesensor_out = A1;
int lot1 = 10;
int lot2 = 11;
int lot3 = 2;
int lot4 = 3;
int lot5 = 4;
int lot6 = 5;
int lot7 = 6;
int lot8 = 7;

int C;
int C2;
int C3;
int C4;
int C5;
int C6;
int C7;
int C8;

int BarUp = 95;
int BarDown = 177;
int forcevalue_in = 0;
int forcevalue_out = 0;
int Carpark = 8;

unsigned long duration = 0;
unsigned long duration2 = 0;
unsigned long duration3 = 0;
unsigned long duration4 = 0;
unsigned long duration5 = 0;
unsigned long duration6 = 0;
unsigned long duration7 = 0;
unsigned long duration8 = 0;

int segB = A2;
int segC = A3;
int segD = A4;
int segA = A5;

byte segments[10][4] =
// A,B,C,D
// 3,2,1,0
{
  {0,0,0,0},
  {1,0,0,0},
  {0,1,0,0},
  {1,1,0,0},
  {0,0,1,0},
  {1,0,1,0},
  {0,1,1,0},
  {1,1,1,0},
  {0,0,0,1},
  {1,0,0,1},
}; 

void setup() {
  // put your setup code here, to run once:
myservo.attach(Servopin);

pinMode(Servopin,OUTPUT);
pinMode(ledgreen,OUTPUT);
pinMode(ledred,OUTPUT);
pinMode(segA,OUTPUT);
pinMode(segB,OUTPUT);
pinMode(segC,OUTPUT);
pinMode(segD,OUTPUT);
pinMode(forcesensor_in,INPUT);
pinMode(forcesensor_out,INPUT);

Serial.begin(9600);
}

int Available = 8;

void loop() 
{
  loopPart1();
  loopPart2();
}

void loopPart1() 
{
  // put your main code here, to run repeatedly:
pinMode(lot1, OUTPUT);
digitalWrite(lot1,LOW);
delayMicroseconds(2);
digitalWrite(lot1,HIGH);
delayMicroseconds(5);
digitalWrite(lot1,LOW);

pinMode(lot1,INPUT);
duration = pulseIn(lot1,HIGH);
C = duration/58;

pinMode(lot2, OUTPUT);
digitalWrite(lot2,LOW);
delayMicroseconds(2);
digitalWrite(lot2,HIGH);
delayMicroseconds(5);
digitalWrite(lot2,LOW);

pinMode(lot2,INPUT);
duration2 = pulseIn(lot2,HIGH);
C2 = duration2/58;

pinMode(lot3, OUTPUT);
digitalWrite(lot3,LOW);
delayMicroseconds(2);
digitalWrite(lot3,HIGH);
delayMicroseconds(5);
digitalWrite(lot3,LOW);

pinMode(lot3,INPUT);
duration3 = pulseIn(lot3,HIGH);
C3 = duration3/58;

pinMode(lot4, OUTPUT);
digitalWrite(lot4,LOW);
delayMicroseconds(2);
digitalWrite(lot4,HIGH);
delayMicroseconds(5);
digitalWrite(lot4,LOW);

pinMode(lot4,INPUT);
duration4 = pulseIn(lot4,HIGH);
C4 = duration4/58;

pinMode(lot5, OUTPUT);
digitalWrite(lot5,LOW);
delayMicroseconds(2);
digitalWrite(lot5,HIGH);
delayMicroseconds(5);
digitalWrite(lot5,LOW);

pinMode(lot5,INPUT);
duration5 = pulseIn(lot5,HIGH);
C5 = duration5/58;

pinMode(lot6, OUTPUT);
digitalWrite(lot6,LOW);
delayMicroseconds(2);
digitalWrite(lot6,HIGH);
delayMicroseconds(5);
digitalWrite(lot6,LOW);

pinMode(lot6,INPUT);
duration6 = pulseIn(lot6,HIGH);
C6 = duration6/58;

pinMode(lot7, OUTPUT);
digitalWrite(lot7,LOW);
delayMicroseconds(2);
digitalWrite(lot7,HIGH);
delayMicroseconds(5);
digitalWrite(lot7,LOW);

pinMode(lot7,INPUT);
duration7 = pulseIn(lot7,HIGH);
C7 = duration7/58;

pinMode(lot8, OUTPUT);
digitalWrite(lot8,LOW);
delayMicroseconds(2);
digitalWrite(lot8,HIGH);
delayMicroseconds(5);
digitalWrite(lot8,LOW);

pinMode(lot8,INPUT);
duration8 = pulseIn(lot8,HIGH);
C8 = duration8/58;

forcevalue_in = analogRead(forcesensor_in);
int carweight_in = map(forcevalue_in,0,1023,0,255);
forcevalue_out = analogRead(forcesensor_out);
int carweight_out = map(forcevalue_out,0,1023,0,255);

Display(Available);

if(carweight_in > 100)
{
  if(Available != 0)
  {
    Available--;
    myservo.write(BarUp);
    delay(3000);
    myservo.write(BarDown);
  }
}
if(carweight_out > 100)
{
  if(Available != Carpark)
  {
    Available++;
    myservo.write(BarUp);
    delay(3000);
    myservo.write(BarDown);
  }
}
}

void loopPart2()
{
  if( C <= 100 && C2 <= 100 && C3 <= 100 && C4 <= 100 && C5 <= 100 && C6 <= 100 && C7 <= 100 && C8 <= 100)
{
  digitalWrite(ledgreen, LOW);
  digitalWrite(ledred, HIGH);
}
  else
{
  digitalWrite(ledred, LOW);
  digitalWrite(ledgreen, HIGH);
}
}

long microsecondsToInches( long microseconds)
{
  return microseconds /74 /2;
}

long microsecondsToCentimeters( long microseconds)
{
  return microseconds /29 / 2;
}

void Display(int number)
{
byte segs =  ~segments[number][4];        //"~" is used for commom anode.

digitalWrite(segD, bitRead(segs, 0) );
digitalWrite(segC, bitRead(segs, 1) );
digitalWrite(segB, bitRead(segs, 2) );
digitalWrite(segA, bitRead(segs, 3) );
}

Thanks and the display still figuring.

concentrate on only getting the display to work. that seems to be the main problem.
once you have that, the rest is easy.

there are lots of shematics on the web that show how to connect and how to code.

get the display to show when a sensor is reading or not. Once you have it reading 1 or 0

connect all the sensors.

spaces_taken = (sensor1 + sensor2 + sensor3 + sensor4 + sensor5 + sensor6 + sensor7 + sensor8 )

then display spaces_taken
this should count up as you trigger each sensor, and count down as you move the car away from one.

of course you want spaces available so
available = 8 - spaces_taken
display available

Each LED needs a resistor. use anything between 1k and 220 ohm
Looks like you are trying to power all the sensors and the LED from the 5v of the Arduino.
This is not a good thing. you are close to the maximum power you can provide.

Thank you dave-in-nj.

I got the all segment light on but it wont reduce when the sensor trigger. Still wondering whats wrong.

And from your code:

spaces_taken = (sensor1 + sensor2 + sensor3 + sensor4 + sensor5 + sensor6 + sensor7 + sensor8)

Is this describe that there are 8 HIGH value from the input if all occupied???

If I code that:

Space_taken = (sensor1);

Means I only have 1 HIGH value from input if the one occupied???

Sorry for asking fundamental question.

Hi,
What is the part number of your 7seg digit?

Thanks.. Tom.. :slight_smile:

Thanks Tom and sorry for late reply.

Sorry i did not get you mean by part number of 7 segment display. I run the arduino purely on this website called circuit.io cause i don have enough or suitable real component with me in this moment. So i fully depend on the website to check my code working or not.

For you information, i was using BCD4511 decoder and connect to common anode 7-segment display. Good new is the display is on but it won't change the number when I triggered the sensor which suppose to minus the number by one each time triggered.

can you just change the number every 3 seconds with a program ?

please re-post your current code that has the LED working

freakingblurcase:
Thank you dave-in-nj.

I got the all segment light on but it wont reduce when the sensor trigger. Still wondering whats wrong.

And from your code:
Is this describe that there are 8 HIGH value from the input if all occupied???

If I code that:

Space_taken = (sensor1);

Means I only have 1 HIGH value from input if the one occupied???

Sorry for asking fundamental question.

space_taken = (s1 + s2 + s3... s8 )

this will show the total of spaces taken.
then
avail = 8 - space_taken

will show how many are available.
I think you want to display this ?

Thanks dave-in-nj.

ya I would like to display this but at that moment I can't figure out the connection to decoder and display. I tried to worked the code bits by bits, when i only connect sensor and the 7 segment display, it's worked by reduce one by one or increase one by one. But when the decoder comes in, the number stand still.

By the way, today finally got it worked. It seems that because I have low power supply to ALL components. But i use the 5v from arduino and a 9v battery to run the components. I don't know what happened to my circuit but when i use lager battery (suggest by my assessor), its work. And i also change the display code like this:

void Display()
{
    if (Available == 0) //write 0000
    {
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
     
    if (Available == 1) //write 0001
    {
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
     
    if (Available == 2) //write 0010
    {
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
     
    if (Available == 3) //write 0011
    {
      digitalWrite(A, HIGH);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
     
    if (Available == 4) //write 0100
    {
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
}

Seems that the decoder BCD4511 have to connect to a common cathode display while 7447 decoder connect to a common anode display. The code above is send to 7447 decoder to common anode display. If need to connect to common cathode, just change the code to :

if (Available == 0) //write 0000
    {
      digitalWrite(A, HIGH);
      digitalWrite(B, HIGH);
      digitalWrite(C, HIGH);
      digitalWrite(D, HIGH);
    }

Again, please allow me to thanks for everyone who helped me a lot in my project although we never met before or we barely know each other. But seriously, without you guys helps, I can't get this project works. Appreciation from the bottom of my heart.

Hi,
If your battery is one of the smoke detector type of 9V battery, than it will never supply the current you need, specially if you are using a 7seg display.

Do you have a DMM to measure voltages and currents.

Tom.... :slight_smile: