-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode(Arduino\C++)
313 lines (268 loc) · 5.68 KB
/
Code(Arduino\C++)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//Define light
#define Light_1 52
#define Light_2 50
#define Light_3 48
#define Light_4 46
#define buzzer 9
//Define PIR
#define PIR_1 10
#define PIR_2 11
#define PIR_3 12
#define PIR_4 13
#define Test_L1 31
#define Test_L2 33
#define Test_L3 35
#define Test_L4 37
#define Test_L5 39
bool Armed = false;
const int flashSpeed = 500;
bool PIR_A1 = false;
bool PIR_A2 = false;
bool PIR_A3 = false;
bool PIR_A4 = false;
int S1 = 0;
int S2 = 0;
int S3 = 0;
int S4 = 0;
int BuzzState = 0;
int inbyte = 0;
bool PIR1_State = false;
bool PIR2_State = false;
bool PIR3_State = false;
bool PIR4_State = false;
boolean roomL1_state = false;
boolean roomL2_state = false;
boolean roomL3_state = false;
boolean roomL4_state = false;
unsigned long tmp_count1 = 0;
unsigned long tmp_count2 = 0;
unsigned long tmp_count3 = 0;
unsigned long tmp_count4 = 0;
int PIRS_1 = 0;
int PIRS_2 = 0;
int PIRS_3 = 0;
int PIRS_4 = 0;
char strPIR[5] = "zxcv";
char strTrueStatePIR[5] = "uiop";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//Light mode declaration
pinMode(Light_1, OUTPUT);
pinMode(Light_2, OUTPUT);
pinMode(Light_3, OUTPUT);
pinMode(Light_4, OUTPUT);
// PIR warm up reading
digitalRead(PIR_1);
digitalRead(PIR_2);
digitalRead(PIR_3);
digitalRead(PIR_4);
pinMode(Test_L1, OUTPUT);
pinMode(Test_L2, OUTPUT);
pinMode(Test_L3, OUTPUT);
pinMode(Test_L4, OUTPUT);
pinMode(Test_L5, OUTPUT);
digitalWrite(Test_L1, 1);
digitalWrite(Test_L2, 1);
digitalWrite(Test_L3, 1);
digitalWrite(Test_L4, 1);
digitalWrite(Test_L5, 1);
delay(6000);
digitalWrite(Test_L1, 0);
digitalWrite(Test_L2, 0);
digitalWrite(Test_L3, 0);
digitalWrite(Test_L4, 0);
digitalWrite(Test_L5, 0);
}
void loop() {
if (Serial.available() > 0)
{
inbyte = Serial.read();
}
//Send state of PIR
Serial.print(strTrueStatePIR);
if (inbyte == 'A') //system is armed
{
Armed = true;
}
if (Armed == true)
{
PIR_A1 = true;
PIR_A2 = true;
PIR_A3 = true;
PIR_A4 = true;
///////////////////////////////////////////////
//true PIR states
//pir1 true state
if ((digitalRead(PIR_1) == 1))
{
strTrueStatePIR[0] = 'U';
digitalWrite(Test_L1, 1);
}
if ((digitalRead(PIR_1) == 0))
{
strTrueStatePIR[0] = 'u';
digitalWrite(Test_L1,0);
}
//pir2 true state
if ((digitalRead(PIR_2) == 1))
{
strTrueStatePIR[1] = 'I';
digitalWrite(Test_L2, 1);
}
if ((digitalRead(PIR_2) == 0))
{
strTrueStatePIR[1] = 'i';
digitalWrite(Test_L2, 0);
}
//pir3 true state
if ((digitalRead(PIR_3) == 1))
{
strTrueStatePIR[2] = 'O';
digitalWrite(Test_L3, 1);
}
if ((digitalRead(PIR_3) == 0))
{
strTrueStatePIR[2] = 'o';
digitalWrite(Test_L3, 0);
}
//pir4 true state
if ((digitalRead(PIR_4) == 1))
{
strTrueStatePIR[3] = 'P';
digitalWrite(Test_L4, 1);
}
if ((digitalRead(PIR_4) == 0))
{
strTrueStatePIR[3] = 'p';
digitalWrite(Test_L4, 0);
}
}
// // Send PIR state if armed and triggered
// if ((PIR_A1 == true) && (digitalRead(PIR_1) == 1))
// {
// digitalWrite(Test_L2, 1);
// //set PIR1 Tx data
// strPIR[0] = 'Z';
// // if PIR1 cam test fails
// if (inbyte == 'H')
// {
// //set PIR state back to off or small letter
// strPIR[0] = 'z';
// }
//
// }
//
// if ((PIR_A2 == true) && (digitalRead(PIR_2) == 1))
// {
// strPIR[1] = 'X';
// if (inbyte == 'J')
// {
// strPIR[1] = 'x';
// }
//
// }
//
// if ((PIR_A3 == true) && (digitalRead(PIR_3) == 1))
// {
// strPIR[2] = 'C';
// if (inbyte == 'K')
// {
// strPIR[2] = 'c';
// }
// }
//
// if ((PIR_A4 == true) && (digitalRead(PIR_4) == 1))
// {
// strPIR[3] = 'V';
// if (inbyte == 'L')
// {
// strPIR[3] = 'v';
// }
// }
//
// if (Armed == true)
// {
// //print 'zxcv'
// Serial.write(strPIR);
//
// }
//alarms triggered sent from matlab
if (inbyte == 'Q')
{
S1 = 1;
}
if (inbyte == 'W')
{
S2 = 1;
}
if (inbyte == 'E')
{
S3 = 1;
}
if (inbyte == 'R')
{
S4 = 1;
}
//LIGHT FUNCTION
if (((millis() - tmp_count1) >= flashSpeed) && (S1 == 1))
{
digitalWrite(Light_1, !roomL1_state);
roomL1_state = !roomL1_state;
tmp_count1 = millis();
}
if (((millis() - tmp_count2) >= flashSpeed) && (S2 == 1))
{
digitalWrite(Light_2, !roomL2_state);
roomL2_state = !roomL2_state;
tmp_count2 = millis();
}
if (((millis() - tmp_count3) >= flashSpeed) && (S3 == 1))
{
digitalWrite(Light_3, !roomL3_state);
roomL3_state = !roomL3_state;
tmp_count3 = millis();
}
if (((millis() - tmp_count4) >= flashSpeed) && (S4 == 1))
{
digitalWrite(Light_4, !roomL4_state);
roomL4_state = !roomL4_state;
tmp_count4 = millis();
}
if (((S1 == 1) || (S2 == 1) || (S3 == 1) || (S4 == 1)) && (BuzzState == 0))
{
tone(buzzer, 1000);
BuzzState = 1;
}
if (inbyte == 'a')
{
// reset or diable armed state
Armed = false;
PIR_A1 = false;
PIR_A2 = false;
PIR_A3 = false;
PIR_A4 = false;
//turn buzzer off
noTone(buzzer);
BuzzState = 0;
//switch all lights off
digitalWrite(Light_1, LOW);
digitalWrite(Light_2, LOW);
digitalWrite(Light_3, LOW);
digitalWrite(Light_4, LOW);
//reset alarm trigger off
S1 = 0 ;
S2 = 0 ;
S3 = 0 ;
S4 = 0 ;
//Reset for light flashing variables
roomL1_state = false;
roomL2_state = false;
roomL3_state = false;
roomL4_state = false;
tmp_count1 = 0;
tmp_count2 = 0;
tmp_count3 = 0;
tmp_count4 = 0;
}
}