-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tugas11
102 lines (86 loc) · 2.28 KB
/
Tugas11
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
#include "MFRC522.h"
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
byte readCard[4];
String tagID = "";
int check=0;
const int Set=3;
String Tag[Set] = {"43EE3FFE","ID2","ID3"};
#define g_led 2 // Green LED Pin
#define y_led 3 // Yellow LED Pin
#define r_led 4 // Red LED Pin
#define servo 7
void setup() {
Serial.begin(9600);// Initiating
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
pinMode(g_led, OUTPUT);
pinMode(y_led, OUTPUT);
pinMode(r_led, OUTPUT);
pinMode(servo, OUTPUT);
servoPulse(servo, 30);
digitalWrite(g_led, 0);
digitalWrite(y_led, 0);
digitalWrite(r_led, 1);
delay(500);
}
void loop() {//Wait until new tag is available
while(getID()){
Serial.println(tagID);
check = 0;
for (int i=0; i<Set; i++){
if(tagID == Tag[i]){check=1;
digitalWrite(g_led, 1);
digitalWrite(y_led, 0);
digitalWrite(r_led, 0);
for(int angle=30; angle<=110; angle++){servoPulse(servo, angle);}
delay(500);
digitalWrite(g_led, 0);
digitalWrite(y_led, 1);
digitalWrite(r_led, 0);
delay(500);
for(int angle=110; angle>=30; angle--){servoPulse(servo, angle);}
i=Set;
}
}
if(check==0){
for(int x=0; x<10; x++){
digitalWrite(g_led, 0);
digitalWrite(y_led, 0);
digitalWrite(r_led, 0);
delay(500);
digitalWrite(y_led, 1);
delay(500);
}
}
digitalWrite(g_led, 0);
digitalWrite(y_led, 0);
digitalWrite(r_led, 1);
}
delay(10);
}
//Read new tag if available
boolean getID(){// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}
void servoPulse (int pin, int angle){
int pwm = (angle*11) + 500; // Convert angle to microseconds
digitalWrite(pin, HIGH);
delayMicroseconds(pwm);
digitalWrite(pin, LOW);
delay(30); // Refresh cycle of servo
}