-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.ino
179 lines (155 loc) · 3.45 KB
/
reader.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
#include <SoftwareSerial.h>
SoftwareSerial RfidReader(2, 3);
int num;
int count = 0;
int led = 7;
int data1 = 0;
int buttonPin = 5;
int newtag[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int STATE = 0;
const int STATE_INITIAL = 0;
const int STATE_PRESSED = 1;
const int STATE_FIRST_TAG = 2;
const int STATE_SECOND_TAG = 3;
const int STATE_RELEASED = 4;
const int STATE_WRONG = 5;
int ACTION_PRESS_BUTTON = 1;
int ACTION_RELEASE_BUTTON = 2;
int ACTION_INPUT_TAG = 3;
bool BUTTON_PRESSED = false;
int tags[][14] = {
{2, 48, 57, 48, 48, 50, 49, 53, 53, 50, 49, 53, 67, 3 }, // yellow
{2, 48, 69, 48, 48, 49, 51, 55, 70, 52, 69, 50, 67, 3 } // blue
};
int read_tags[2] = {-1, -1};
int process(int state, int key) {
// Serial.print(state);
// Serial.print(' ');
// Serial.println(key);
int new_state = STATE_WRONG;
switch (state) {
case STATE_INITIAL:
if (BUTTON_PRESSED) {
new_state = STATE_PRESSED;
} else {
new_state = STATE_INITIAL;
}
break;
case STATE_PRESSED:
if (BUTTON_PRESSED) {// THINK
if (key != -1) {
new_state = STATE_FIRST_TAG;
}
}
break;
case STATE_FIRST_TAG:
if (BUTTON_PRESSED == true) {
if (key != -1) {
new_state = STATE_SECOND_TAG;
}
}
break;
case STATE_SECOND_TAG:
if (BUTTON_PRESSED == false) {
new_state = STATE_RELEASED;
}
}
Serial.println(new_state);
delay(1000);
return new_state;
}
bool comparetag(int aa[14], int bb[14]){
bool ff = false;
int fg = 0;
for (int cc = 0 ; cc < 14 ; cc++){
if (aa[cc] == bb[cc]){
fg++;
}
}
if (fg == 14){
ff = true;
}
return ff;
}
void print_tag(int tag[14]) {
for (int t = 0; t < 14; t++) {
Serial.print(tag[t]);
Serial.print(", ");
}
Serial.println(";");
}
void setup(){
pinMode(buttonPin, INPUT);
Serial.begin(9600);
RfidReader.begin(9600);
Serial.print("Starting...");
}
void loop() {
int buttonState = digitalRead(buttonPin);
// Serial.println(BUTTON_PRESSED);
if (buttonState == HIGH) {
if (BUTTON_PRESSED == false) {
BUTTON_PRESSED = true;
STATE = process(STATE, -1);
}
}
else {
if (BUTTON_PRESSED == true) {
BUTTON_PRESSED = false;
STATE = process(STATE, -1);
}
}
if (RfidReader.available() > 0) {
Serial.println("read tag");
for (int x = 0 ; x < 14 ; x++) {
data1 = RfidReader.read();
newtag[x] = data1;
}
RfidReader.flush();
print_tag(newtag);
for(int i = 0; i < sizeof(tags) ; i++){
if (comparetag(newtag, tags[i]) == true){
STATE = process(STATE, i);
break;
}
}
}
switch (STATE){
case STATE_INITIAL:
analogWrite(7, 0);
analogWrite(8, 0);
analogWrite(9, 0);
analogWrite(10, 0);
break;
case STATE_PRESSED:
analogWrite(7, 0);
analogWrite(8, 0);
analogWrite(9, 0);
analogWrite(10, 255);
break;
case STATE_FIRST_TAG:
analogWrite(7, 0);
analogWrite(8, 0);
analogWrite(9, 255);
analogWrite(10, 255);
break;
case STATE_SECOND_TAG:
analogWrite(7, 0);
analogWrite(8, 255);
analogWrite(9, 255);
analogWrite(10, 255);
break;
case STATE_RELEASED:
analogWrite(7, 255);
analogWrite(8, 255);
analogWrite(9, 255);
analogWrite(10, 255);
break;
case STATE_WRONG:
analogWrite(7, 0);
analogWrite(8, 0);
analogWrite(9, 0);
analogWrite(10, 0);
break;
};
}