-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgss.ino
166 lines (145 loc) · 3.09 KB
/
sgss.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
/*
Sensor
Ultrasonic 5 5V
PIR Motion 3 5V
Magnetic Switch 4
RFID 2 53 52 51 50 3.3V
Actuators
LED RGB 12 11 10 5V
Buzzer 6
LCD SDA/SCL 20/21 5V
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RFID.h>
#include <SPI.h>
#define ultraPin 5
#define pirPin 3
#define magPin 4
// RFID pin
#define rstPin 2
#define ssPin 53
#define buzPin 6
RFID rfid(ssPin, rstPin);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int ultra;
bool mag, latest_mag, access, pir, latest_pir;
String nama;
int cards[][5] = {
{41,173,125,194,59},
{136,4,41,29,184}
};
String names[] = {
"Odin",
"Ali"
};
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buzPin, OUTPUT);
pinMode(magPin, INPUT_PULLUP);
pinMode(pirPin, INPUT);
SPI.begin();
rfid.init();
lcd.init();
lcd.backlight();
}
void loop() {
readSignalUltra();
readSignalMag();
readSignalPir();
readSignalRfid();
Serial.print("U ");
Serial.println(ultra);
if (ultra < 100){
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
delay(500);
}
void readSignalRfid(){
if (rfid.isCard()){
if(rfid.readCardSerial()){
digitalWrite(buzPin, HIGH);
delay(100);
digitalWrite(buzPin, LOW);
for(int x = 0; x < sizeof(cards); x++){
for(int i = 0; i < sizeof(rfid.serNum); i++ ){
if(rfid.serNum[i] != cards[x][i]) {
access = false;
break;
} else {
nama = names[x];
access = true;
}
}
if(access) break;
}
}
if(access){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Authenticated");
lcd.setCursor(0,1);
lcd.print("Welcomeback ");
lcd.print(nama);
Serial.print("R ");
Serial.print("1 ");
Serial.println(nama);
} else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Unauthenticated");
lcd.setCursor(0,1);
lcd.print("Please move back!");
Serial.print("R ");
Serial.print("0 ");
Serial.print(rfid.serNum[0]);
Serial.print(rfid.serNum[1]);
Serial.print(rfid.serNum[2]);
Serial.print(rfid.serNum[3]);
Serial.println(rfid.serNum[4]);
}
delay(1000);
}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Smart Gate");
lcd.setCursor(0,1);
lcd.print("Security Systems");
}
}
void readSignalMag(){
mag = digitalRead(magPin);
if (latest_mag != mag){
latest_mag = mag;
boolean buz = latest_mag;
if(access){
buz = false;
access = false;
}
digitalWrite(buzPin, buz);
Serial.print("M ");
Serial.println(latest_mag);
}
}
void readSignalPir(){
pir = digitalRead(pirPin);
if (latest_pir != pir){
latest_pir = pir;
Serial.print("P ");
Serial.println(latest_pir);
}
}
void readSignalUltra(){
pinMode(ultraPin, OUTPUT);
digitalWrite(ultraPin, LOW);
delayMicroseconds(2);
digitalWrite(ultraPin, HIGH);
delayMicroseconds(2);
digitalWrite(ultraPin, LOW);
pinMode(ultraPin, INPUT);
ultra = pulseIn(ultraPin, HIGH) * 0.01723 ;
}