-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHouse_System_program.c
More file actions
253 lines (215 loc) · 8.12 KB
/
House_System_program.c
File metadata and controls
253 lines (215 loc) · 8.12 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifdef F_CPU
#undef F_CPU
#define F_CPU 1000000UL // for performance reasons in proteus
#endif
#include "STD_TYPES.h"
#include "BIT_MATH.h"
#include "DIO_interface.h"
#include "LCD_interface.h"
#include "Keypad_config.h"
#include "Keypad_interface.h"
#include "ADC_interface.h"
#include "LM35_ADC.h"
#include "LDR_interface.h"
#include "TIMER_INTERFACE.h"
#include "House_System.h"
#include <util/delay.h> //for delays
#include <string.h> // For the strcmp function
// Flags
int pinEntered = 0, pinCorrect = 0, Door_state = 0, light_state = 0, failed_count = 0;
int Auto_Light_flag = 0;
void houseSystem(void)
{
char enteredPin[5];
while (1)
{
// If a PIN has not been entered, ask for the PIN
if (!pinEntered)
{
LCD_voidClearScreen();
LCD_voidGoToRowColumn(0, 0);
LCD_voidSendString("PIN:");
// Read the entered PIN from the user
for (int i = 0; i < 4; i++)
{
u8 key;
do
{
key = KPD_VidGetPressedKey();
} while (key == KPD_NOT_Pressed); // Ignore KPD_NOT_Pressed
enteredPin[i] = key;
LCD_voidSendData('*'); // Display an asterisk for each entered digit
}
enteredPin[4] = '\0'; // Null-terminate the string
// Set the flag to indicate that a PIN has been entered
pinEntered = 1;
}
// Compare the entered PIN with the correct PIN
if (strcmp(enteredPin, "1234") == 0 || pinCorrect == 1)
{
LM35_Motor_Buzzer();
pinCorrect = 1;
DIO_VidSetPinValue(Port_B, Pin5, PinHigh); // logged in status
LCD_voidClearScreen();
LCD_voidSendString("1-Temp 2-Lock");
LCD_voidGoToRowColumn(1, 0);
LCD_voidSendString("3-Lights 4-more.");
u8 selectedOption, second_screen_option;
while (1)
{
do
{
LM35_Motor_Buzzer();
selectedOption = KPD_VidGetPressedKey();
} while (selectedOption == KPD_NOT_Pressed);
// Perform the corresponding action
switch (selectedOption)
{
case '1':
// displayTemperature();
LCD_voidClearScreen();
LCD_voidSendString("Temprature : ");
LCD_voidPrintNumber(LM35_ADC_Get_Temp());
LCD_voidSendString(" C");
_delay_ms(1000);
LCD_voidClearScreen();
houseSystem();
break;
case '2':
// toggleDoorLock();
LCD_voidClearScreen();
if (Door_state == 0)
{
Door_state = 1;
DIO_VidSetPinValue(Port_B, Pin4, PinHigh);
LCD_voidSendString("Door opened. ");
_delay_ms(500);
houseSystem();
}
else
{
Door_state = 0;
DIO_VidSetPinValue(Port_B, Pin4, PinLow);
LCD_voidSendString("Door Closed. ");
_delay_ms(500);
houseSystem();
}
break;
case '3':
// toggleRoomLights();
LCD_voidClearScreen();
if (light_state == 0)
{
light_state = 1;
DIO_VidSetPinValue(Port_B, Pin2, PinHigh);
LCD_voidSendString("Lights ON.");
_delay_ms(500);
houseSystem();
}
else
{
light_state = 0;
DIO_VidSetPinValue(Port_B, Pin2, PinLow);
LCD_voidSendString("Lights Off.");
_delay_ms(500);
houseSystem();
}
break;
case '4':
while (1)
{
LCD_voidClearScreen();
LCD_voidSendString("1-AutoLight 2-Lo");
LCD_voidGoToRowColumn(1, 0);
LCD_voidSendString("gout 3-back.");
do
{
LM35_Motor_Buzzer();
second_screen_option = KPD_VidGetPressedKey();
} while (second_screen_option == KPD_NOT_Pressed);
switch (second_screen_option)
{
case '1':
// Toggle Auto Light
LCD_voidClearScreen();
if (Auto_Light_flag == 0)
{
Auto_Light_flag = 1;
LCD_voidSendString("Auto Lights ON.");
_delay_ms(500);
}
else
{
Auto_Light_flag = 0;
LCD_voidSendString("Auto Lights Off.");
_delay_ms(500);
}
break;
case '2':
// logout
pinCorrect = 0;
pinEntered = 0;
LCD_voidClearScreen();
LCD_voidSendString("Logging out.");
DIO_VidSetPinValue(Port_B, Pin5, PinLow); // logged in status
_delay_ms(500);
houseSystem();
case '3':
// go back
houseSystem();
break;
default:
LCD_voidClearScreen();
LCD_voidSendString("Invalid option");
_delay_ms(500);
LCD_voidClearScreen();
break;
}
}
default:
LCD_voidClearScreen();
LCD_voidSendString("Invalid option");
_delay_ms(500);
LCD_voidClearScreen();
houseSystem();
break;
}
}
}
else
{
if (failed_count == 2)
{
// Invalid PIN, display error message and reset the flag
LCD_voidClearScreen();
LCD_voidSendString("You have been");
LCD_voidGoToRowColumn(1, 0);
LCD_voidSendString("blocked for 30s");
_delay_ms(30000);
pinEntered = 0;
failed_count = 0;
houseSystem();
}
else
{
// Invalid PIN, display error message and reset the flag
LCD_voidClearScreen();
LCD_voidSendString("Invalid PIN");
_delay_ms(500);
pinEntered = 0;
failed_count++;
}
}
}
}
void House_init()
{
LCD_voidInit();
KPD_VidInit();
ADC_VidInit();
FAST_PWM_NON_INVERTED_INIT();
Buzzer_init();
DIO_VidSetPinDirection(Port_B, Pin2, Output); // Light
DIO_VidSetPinDirection(Port_B, Pin4, Output); // Door lock
DIO_VidSetPinDirection(Port_B, Pin5, Output); // Login status
}