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! ![]()
Sorry for the long post.