-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdaptive Cruise Control.m
161 lines (142 loc) · 4.91 KB
/
Adaptive Cruise Control.m
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
% Clear all variables in the workspace
clc;
clear;
% Creation of variable for Arduino
ar = arduino('COM5','Uno','Libraries',{'Ultrasonic','ExampleLCD/LCDAddOn'},'ForceBuildOn',true);
% Initializes a connection with an Arduino board connected to COM5 port
% and specifies the libraries to be used (Ultrasonic and ExampleLCD/LCDAddOn)
% Creation of variable for Ultrasonic Sensor
ul = ultrasonic(ar,'D10','D8');
% Initializes an ultrasonic sensor connected to digital pins D10 (trigger)
% and D8 (echo) of the Arduino
lcd = addon(ar,"ExampleLCD/LCDAddOn",'RegisterSelectPin','D7','EnablePin','D6','DataPins',{'D5','D4','D3','D2'});
% Initializes an LCD display connected to the Arduino using specific pins
initializeLCD(lcd);
% Initializes the LCD display
clearLCD(lcd);
% Clears the content displayed on the LCD
printLCD(lcd, 'WELCOME TO');
printLCD(lcd, 'ACC PROJECT');
% Prints "WELCOME TO" on the first line of the LCD display
% Prints "ACC PROJECT" on the second line of the LCD display
pause(5);
% Pauses MATLAB execution for 5 seconds
clearLCD(lcd);
% Clears the content displayed on the LCD
printLCD(lcd, 'Group 32');
printLCD(lcd, 'Amey,Brano,Nandu');
% Prints "Group 32" on the first line of the LCD display
% Prints "Amey,Brano,Nandu" on the second line of the LCD display
pause(5);
% Pauses MATLAB execution for 5 seconds
% Declaration of Variables
speed = 0;
increase_speed = 0;
decrease_speed = 0;
cancel = 0;
set_speed = 0;
adaptive_cruise_speed = 0;
distance = 0;
mode = 0;
while true
% Infinite loop to continuously monitor and update the speed
% Get inputs from user
increase_speed = readVoltage(ar,'A0');
% Read the voltage value from analog input pin A0
decrease_speed = readVoltage(ar,'A1');
% Read the voltage value from analog input pin A1
cancel = readVoltage(ar,'A2');
% Read the voltage value from analog input pin A2
set_speed = readVoltage(ar,'A3');
% Read the voltage value from analog input pin A3
adaptive_cruise_speed = readVoltage(ar,'A4');
% Read the voltage value from analog input pin A4
% Get the input from the ultrasonic sensor as distance
distance = readDistance(ul);
if cancel >= 4
mode = 0;
% If the cancel button is pressed (voltage value >= 4),
% set mode to 0 (Normal Mode)
elseif set_speed >= 4
mode = 1;
% If the set speed button is pressed (voltage value >= 4),
% set mode to 1 (Cruise Control Mode)
elseif adaptive_cruise_speed >= 4
mode = 2;
constant = speed;
% If the adaptive cruise control button is pressed (voltage value >= 4),
% set mode to 2 (Adaptive Cruise Control Mode) and store the current speed in 'constant'
end
% Normal Mode
if mode == 0
if increase_speed >= 4
writeDigitalPin(ar, 'D13', 1);
writeDigitalPin(ar, 'D12', 0);
speed = speed + 1;
pause(0.1);
elseif decrease_speed >= 4
speed = speed - 1;
pause(0.1);
else
speed = speed - 1;
pause(1.5);
end
if speed < 0
writeDigitalPin(ar, 'D13', 0);
writeDigitalPin(ar, 'D12', 1);
speed = 0;
end
printLCD(lcd,'Vehicle Speed: ');
% Prints "Vehicle Speed: " on the LCD display
printLCD(lcd,[strcat(num2str(speed))]);
% Prints the current speed on the LCD display
% Cruise Control Mode
elseif mode == 1
if increase_speed >= 4
writeDigitalPin(ar, 'D13', 1);
writeDigitalPin(ar, 'D12', 0);
speed = speed + 1;
pause(0.1);
elseif decrease_speed >= 4
speed = speed - 1;
pause(0.1)
end
if speed < 0
writeDigitalPin(ar, 'D13', 0);
writeDigitalPin(ar, 'D12', 1);
speed = 0;
end
printLCD(lcd,'Cruise mode: ');
% Prints "Cruise mode: " on the LCD display
printLCD(lcd,[strcat(num2str(speed))]);
% Prints the current speed on the LCD display
% Adaptive Cruise Control Mode
elseif mode == 2
clearLCD(lcd);
pause(0.5);
% Clears the LCD display and pauses for 0.5 seconds (blinking effect)
initializeLCD(lcd);
% Reinitializes the LCD display
writeDigitalPin(ar, 'D13', 1);
writeDigitalPin(ar, 'D12', 0);
if distance < 0.3
disp(distance);
speed = speed - 1;
else
disp(distance);
speed = speed + 1;
end
if speed > constant
speed = constant;
end
if speed < 0
writeDigitalPin(ar, 'D13', 0);
writeDigitalPin(ar, 'D12', 1);
speed = 0;
end
printLCD(lcd,'Adap_Cruise_mode');
% Prints "Adap_Cruise_mode" on the LCD display
printLCD(lcd,[strcat(num2str(speed))]);
% Prints the current speed on the LCD display
end
end