-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (89 loc) · 3.74 KB
/
Copy pathmain.cpp
File metadata and controls
93 lines (89 loc) · 3.74 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
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600); // connect to the serial port
myservo.attach(13);
}
void loop () {
myservo.write(0); // tell servo to go to position in variable 'pos'
delay(100);
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if( Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // if 12 digit read is complete
Serial.print("5-byte code: ");
for (i=0; i<5; i++) {
if (code[i] < 16) Serial.print("0");
Serial.print(code[i], HEX);
Serial.print(" ");
}
Serial.print(code[5]);
Serial.println();
if (code[5]==10) {
Serial.print("Welcome K.Grillas");
Serial.println();
myservo.write(70);
delay(1500);
myservo.write(0);
delay(2500);}
if (code[5]==53) {
Serial.print("Welcome E.Ketsetzis");
Serial.println();
myservo.write(70);
delay(1500);
myservo.write(0);
delay(2500);}
if (code[5]==241) {
Serial.print("We are sorry A.Vaitsi.");
Serial.println();
Serial.print("Access denied");
Serial.println();}
if (code[5]==54) {
Serial.print("We are sorry E.Nalmpanti.");
Serial.println();
Serial.print("Access denied");
Serial.println();}
if (code[5]!=54 && code[5]!=241 && code[5]!=53 && code[5]!=10 ) {
Serial.print("Unknown.");
Serial.println();
Serial.print("Access denied");
Serial.println();
}
}
bytesread = 0;
}
}
}