-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoCode.ino
146 lines (125 loc) · 2.62 KB
/
ArduinoCode.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <Servo.h> //includes the servo library
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //includes LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo;
#define ir_enter 2
#define ir_back 4
#define ir_car1 5
#define ir_car2 6
#define ir_car3 7
#define ir_car4 8
int S1=0, S2=0, S3=0, S4=0 ;
int flag1=0, flag2=0;
int slot = 6;
void setup(){
Serial.begin(9600);
// initialize digital pins as input.
pinMode(ir_car1, INPUT);
pinMode(ir_car2, INPUT);
pinMode(ir_car3, INPUT);
pinMode(ir_car4, INPUT);
pinMode(ir_enter, INPUT);
pinMode(ir_back, INPUT);
myservo.attach(9); // Servo motor pin connected to D9
myservo.write(90); // sets the servo at 0 degree position
// Print text on display
lcd.begin(20, 4);
lcd.setCursor (0,1);
lcd.print(" Smart Car ");
lcd.setCursor (0,2);
lcd.print(" Parking System ");
delay (2000);
lcd.clear();
Read_Sensor();
int total = S1+S2+S3+S4;
slot = slot-total;
}
void loop()
{
Read_Sensor();
lcd.setCursor (0,0);
lcd.print(" Have Slot: ");
lcd.print(slot);
lcd.print(" ");
lcd.setCursor (0,1);
if(S1==1)
{
lcd.print("S1:Fill ");
}
else
{
lcd.print("S1:Empty");
}
lcd.setCursor (10,1);
if(S2==1)
{
lcd.print("S2:Fill ");
}
else
{
lcd.print("S2:Empty");
}
lcd.setCursor (0,2);
if(S3==1)
{
lcd.print("S3:Fill ");
}
else
{
lcd.print("S3:Empty");
}
lcd.setCursor (10,2);
if(S4==1)
{
lcd.print("S4:Fill ");
}
else
{
lcd.print("S4:Empty");
}
/* Servo Motor Control
***********************/
if(digitalRead (ir_enter) == 0 && flag1==0) // read degital data from IR sensor1
{
if(slot>0)
{
flag1=1;
if(flag2==0)
{
myservo.write(180);
slot = slot-1;
}
}
else
{
lcd.setCursor (0,0);
lcd.print(" Sorry Parking Full ");
delay(1500);
}
}
if(digitalRead (ir_back) == 0 && flag2==0) // read degital data from IR sensor2
{
flag2=1;
if(flag1==0)
{
myservo.write(180); // sets the servo at 180 degree position
slot = slot+1;
}
}
if(flag1==1 && flag2==1)
{
delay (1000);
myservo.write(90); // sets the servo at 90 degree position
flag1=0, flag2=0;
}
delay(1);
}
void Read_Sensor()
{
S1=0, S2=0, S3=0, S4=0;
if(digitalRead(ir_car1) == 0){S1=1;} // read degital data from IR sensor3
if(digitalRead(ir_car2) == 0){S2=1;} // read degital data from IR sensor4
if(digitalRead(ir_car3) == 0){S3=1;} // read degital data from IR sensor5
if(digitalRead(ir_car4) == 0){S4=1;} // read degital data from IR sensor6
}