-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codeschloss_Teil4_Controller_A.ino
386 lines (361 loc) · 9 KB
/
Codeschloss_Teil4_Controller_A.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Codeschloss Tobias Kuch 2020 GPL 3.0 tobias.kuch@googlemail.com
#include <Keypad.h>
#include <SoftwareSerial.h>
#define RGBLED_R 11
#define RGBLED_G 10
#define RGBLED_B 9
#define RGBFadeInterval1 10 // in ms
#define KeybModeTimeInterval1 5000 // in ms
#define PIEZOSUMMER A1
#define CyclesInBlackMax 20
#define RGBOFF 0
#define RGBSHORTBLACK 8
#define RGBRED 1
#define RGBGREEN 2
#define RGBBLUE 3
#define RGBWHITE 4
#define RGBYELLOW 5
#define RGBCYAN 6
#define RGBMAGENTA 7
const byte ROWS = 4;
const byte COLS = 4;
const byte MaxPinCodeLength = 20;
SoftwareSerial mySerial(12, 13); // RX, TX
char keys[ROWS][COLS] = {
{1,2,3,13},
{4,5,6,14},
{7,8,9,15},
{10,11,12,16},
};
byte colPins[COLS] = {A0,8,7,6}; //A0,8,7,6;
byte rowPins[ROWS]= {5,4,3,2}; // 5,4,3,2}
byte RGBValue_R = 0;
byte RGBValue_G = 0;
byte RGBValue_B = 0;
byte RGBFadeValue_R = 0;
byte RGBFadeValue_G = 0;
byte RGBFadeValue_B = 0;
bool RGBFadeDir_R = true;
bool RGBFadeDir_G = true;
bool RGBFadeDir_B = true;
byte key = 0;
bool InSync = true;
bool CodeEnterSeqence = false;
bool CodeEnterSeqenceOLD = false;
bool InputBlocked = false;
bool PinEnteredFalseBefore = false;
bool RGBFadeEnabled = true;
long previousMillis = 0;
long previousMillisKeyBoard = 0;
byte EnCodedKeyStroke = 0;
byte inByte = 0;
int CyclesInBlack = 0;
byte RecInititalKeyLength = 0;
unsigned long InititalKey = 0;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
union foo {
byte as_array[4];
long as_long;
} d;
void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
pinMode(RGBLED_G,OUTPUT); // Ausgang RGB LED Grün
pinMode(RGBLED_R,OUTPUT); // Ausgang RGB LED Rot
pinMode(RGBLED_B,OUTPUT); // Ausgang RGB LED Blau
pinMode(PIEZOSUMMER,OUTPUT); // Ausgang RGB LED Blau
digitalWrite(PIEZOSUMMER,LOW); // Ausgang RGB LED Blau
RGBControl(RGBWHITE,false); // NORMAL MODE
RecInititalKeyLength = 0;
do
{
if (mySerial.available())
{
inByte = mySerial.read();
d.as_array[RecInititalKeyLength]=inByte; //little Endian
RecInititalKeyLength++;
}
} while (RecInititalKeyLength < 4);
InititalKey = d.as_long;
randomSeed(InititalKey);
RGBControl(RGBBLUE,true); // NORMAL MODE
}
void RGBControl(byte function, bool fadeit)
{
if (function == RGBOFF)
{
RGBValue_R = 0;
RGBValue_G = 0;
RGBValue_B = 0;
RGBFadeValue_R = 0;
RGBFadeValue_G = 0;
RGBFadeValue_B = 0;
RGBFadeDir_R = true;
RGBFadeDir_G = true;
RGBFadeDir_B = true;
}
if (function == RGBRED)
{
RGBValue_R = 255;
RGBValue_G = 0;
RGBValue_B = 0;
RGBFadeValue_R = 255;
RGBFadeValue_G = 0;
RGBFadeValue_B = 0;
RGBFadeDir_R = false;
RGBFadeDir_G = true;
RGBFadeDir_B = true;
}
if (function == RGBGREEN)
{
RGBValue_R = 0;
RGBValue_G = 255;
RGBValue_B = 0;
RGBFadeValue_R = 0;
RGBFadeValue_G = 255;
RGBFadeValue_B = 0;
RGBFadeDir_R = true;
RGBFadeDir_G = false;
RGBFadeDir_B = true;
}
if (function == RGBBLUE)
{
RGBValue_R = 0;
RGBValue_G = 0;
RGBValue_B = 255;
RGBFadeValue_R = 0;
RGBFadeValue_G = 0;
RGBFadeValue_B = 255;
RGBFadeDir_R = true;
RGBFadeDir_G = true;
RGBFadeDir_B = false;
}
if (function == RGBWHITE)
{
RGBValue_R = 255;
RGBValue_G = 255;
RGBValue_B = 255;
RGBFadeValue_R = 255;
RGBFadeValue_G = 255;
RGBFadeValue_B = 255;
RGBFadeDir_R = false;
RGBFadeDir_G = false;
RGBFadeDir_B = false;
}
if (function == RGBCYAN)
{
RGBValue_R = 0;
RGBValue_G = 255;
RGBValue_B = 255;
RGBFadeValue_R = 0;
RGBFadeValue_G = 255;
RGBFadeValue_B = 255;
RGBFadeDir_R = true;
RGBFadeDir_G = false;
RGBFadeDir_B = false;
}
if (function == RGBYELLOW)
{
RGBValue_R = 255;
RGBValue_G = 255;
RGBValue_B = 0;
RGBFadeValue_R = 0;
RGBFadeValue_G = 0;
RGBFadeValue_B = 0;
RGBFadeDir_R = true;
RGBFadeDir_G = true;
RGBFadeDir_B = true;
}
if (function == RGBMAGENTA)
{
RGBValue_R = 255;
RGBValue_G = 0;
RGBValue_B = 255;
RGBFadeValue_R = 255;
RGBFadeValue_G = 0;
RGBFadeValue_B = 255;
RGBFadeDir_R = false;
RGBFadeDir_G = true;
RGBFadeDir_B = false;
}
if (function == RGBSHORTBLACK)
{
analogWrite(RGBLED_R, 0);
analogWrite(RGBLED_G, 0);
analogWrite(RGBLED_B, 0);
}
RGBFadeEnabled = fadeit;
if (!(RGBFadeEnabled))
{
analogWrite(RGBLED_R, RGBValue_R);
analogWrite(RGBLED_G, RGBValue_G);
analogWrite(RGBLED_B, RGBValue_B);
}
}
void SerialHandler ()
{
if (mySerial.available())
{
inByte = mySerial.read();
if (inByte == 30) // Eingabe gesperrt Zeitschloss aktiv
{
InputBlocked = true;
RGBControl(RGBRED,true);
}
if (inByte == 40) // Eingabe entsperrt Zeitschloss deaktiviert
{
RGBControl(RGBMAGENTA,true);
InputBlocked = false;
tone(PIEZOSUMMER, 880, 100);
delay(120);
}
if (inByte == 20) // Code Correct
{
RGBControl(RGBGREEN,false);
tone(PIEZOSUMMER, 1200, 200);
delay(2000);
PinEnteredFalseBefore = false;
RGBControl(RGBBLUE,true); // NORMAL MODE
} else
if (inByte == 21) // Code falsch
{
analogWrite(RGBLED_R, 255);
analogWrite(RGBLED_G, 0);
analogWrite(RGBLED_B, 0);
tone(PIEZOSUMMER, 400, 300);
delay(500);
RGBControl(RGBRED,true);
InputBlocked = true;
PinEnteredFalseBefore = true;
}
if (inByte == 25) // Out of Sync
{
RGBControl(RGBYELLOW,true);
InSync = false;
InititalKey = 0; // Delete Encryption Key
}
if (inByte == 23) //Clear ausgeführt
{
inByte = 0;
}
if (inByte == 22) // EIngabe azeptiert
{
inByte = 0;
}
}
}
void TimeMgmnt ()
{
if ((millis() - previousMillisKeyBoard > KeybModeTimeInterval1) & CodeEnterSeqence & InSync) // Auto Reset KEyboard Input
{
previousMillisKeyBoard = millis();
tone(PIEZOSUMMER, 988, 100);
delay(110);
if (PinEnteredFalseBefore)
{
RGBControl(RGBMAGENTA,true); // NORMAL MODE - Pin entered false before
} else
{
RGBControl(RGBBLUE,true); // NORMAL MODE
}
CodeEnterSeqence = false;
previousMillisKeyBoard = millis();
byte randNumber = random(0, 254);
EnCodedKeyStroke = 10 ^ randNumber;
mySerial.write(EnCodedKeyStroke);
}
if (millis() - previousMillis > RGBFadeInterval1) //Fadint LEd's
{
if (RGBFadeEnabled)
{
previousMillis = millis(); // aktuelle Zeit abspeichern
if (RGBValue_B > 0)
{
if (RGBFadeDir_B)
{
RGBFadeValue_B++;
if ( RGBFadeValue_B >= RGBValue_B) {RGBFadeDir_B = false; }
} else
{
RGBFadeValue_B--;
if ( RGBFadeValue_B < 1) {RGBFadeDir_B = true; }
}
} else { RGBFadeValue_B = 0; }
if (RGBValue_R > 0)
{
if (RGBFadeDir_R)
{
RGBFadeValue_R++;
if ( RGBFadeValue_R >= RGBValue_R) {RGBFadeDir_R = false; }
} else
{
RGBFadeValue_R--;
if ( RGBFadeValue_R < 1) {RGBFadeDir_R = true; }
}
} else { RGBFadeValue_R = 0; }
if (RGBValue_G > 0)
{
if (RGBFadeDir_G)
{
RGBFadeValue_G++;
if ( RGBFadeValue_G >= RGBValue_G) {RGBFadeDir_G = false; }
} else
{
RGBFadeValue_G--;
if ( RGBFadeValue_G < 1) {RGBFadeDir_G = true; }
}
} else { RGBFadeValue_G = 0; }
analogWrite(RGBLED_R, RGBFadeValue_R);
analogWrite(RGBLED_G, RGBFadeValue_G);
analogWrite(RGBLED_B, RGBFadeValue_B);
}
}
}
void KeyboardHandler(bool NotEnabled)
{
key = keypad.getKey();
if((key)) // Key Entered
{
if (!NotEnabled)
{
byte randNumber = random(0, 254);
EnCodedKeyStroke = key ^ randNumber;
mySerial.write(EnCodedKeyStroke);
if((key == 10) | (key == 12))
{
RGBControl(RGBSHORTBLACK,true);
tone(PIEZOSUMMER, 988, 100);
delay(120);
CodeEnterSeqence = false;
if(key == 10)
{
if (PinEnteredFalseBefore)
{
RGBControl(RGBMAGENTA,true); // NORMAL MODE - Pin entered false before
} else
{
RGBControl(RGBBLUE,true); // NORMAL MODE
}
}
} else
{
RGBControl(RGBSHORTBLACK,true);
tone(PIEZOSUMMER, 880, 100);
delay(120);
CodeEnterSeqence = true;
RGBControl(RGBCYAN,true);
previousMillisKeyBoard = millis();
}
}
}
}
void loop()
{
if (InSync)
{
KeyboardHandler(InputBlocked);
}
TimeMgmnt ();
SerialHandler ();
}