/
Ben_Working0408.cpp
160 lines (128 loc) · 5.18 KB
/
Ben_Working0408.cpp
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
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(12, 11, 5, 4, 7, 8); //Testing code for pin switch
SoftwareSerial RFID(2, 3); // RX and TX
//SoftwareSerial RFID(8, 9); // Testing code for pin switch
//If the array is full, and a tag is removed and readded, it overwrites the first tag.
char tag_id;
String id_string = "";
String id_array[5];
int id_marker[5];
int index = 0;
int no_match = 0;
int space_left = 5;
void setup()
{
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
lcd.begin(16, 2);
//lcd output
lcd.clear();
lcd.print("There are "); //start printing the result
lcd.print(space_left); //then how many spaces
lcd.print(" spaces left."); //words
lcd.println(" "); //spaces
for (int i = 0; i < 5; i++)
{
id_marker[i] = 0;
}
Serial.println("Ready!");
//sets everything in the array equal to blank
for(int i=0; i<5; i++)
{
id_array[i] = String("");
}
}
void loop()
{
int i;
while (RFID.available() <= 0); // force wait until rfid present
if(RFID.available() > 0) //does rfid exist?
{
i = 0;
while (i < 14)
{
tag_id = RFID.read(); //Reads character, puts into tag_id
if (tag_id == '-1') //Wait for scan to read
continue;
id_string = String(id_string + tag_id); //Make it a string
//TESTING OUTPUT
//For some reason, getting rid of this makes the tag not read correctly
Serial.print("Read: ");
Serial.print(tag_id);
Serial.print("\nid_string is now ");
Serial.print(id_string);
i++; //counts up unil 14
}
//TESTING OUTPUT
Serial.print("\nid_string is now ");
Serial.print(id_string);
for(int index=0; index<5; index++) //first we check to see if there are any current matches
{
if(id_string == id_array[index]) //if the id matches something in the array at location "i"
{
Serial.println("Match"); //print that it matches.
id_array[index] = String(""); //set the array location of the removed tag to blank
if (id_marker[index] == 1) //if the id marker is equal to one
//else //Removing this else because I dont think it makes sense.
{
// space_left++; //this isnt properly increasing the spaces left
//Commented out space left above because theres also one below.
// not sure if this is necessary
for (int j = index; j < 4; j++) //what purpose does j serve? Increments the search?
{
id_string[j] = id_string[j + 1];
id_marker[j] = id_marker[j + 1];
Serial.println(id_marker[index]); //test print for the current marker.
}
index--; //this might actually only subtract the last place, and not the proper location
space_left++;
no_match = 1;
}
}
}//End of for loop
//I added the ifs below to attempt to refine the location of the write location in the array. they dont work completely
if (no_match == 0)
{
if (id_array[index] != String("")) //if the array location is not blank, then
{
index ++; //increment to the next spot
Serial.println(i);
}
if (index == 5) //if we're at the 5th spot
{
index = 0; //start back at zero
}
//this needs to be able to detect black values
id_array[index] = id_string;
id_marker[index]++;
space_left--;
}
}
Serial.println(" "); // prints a space
Serial.println(id_array[index]); // print the array item
Serial.println("Finished Reading ID."); //prints out this on a line
Serial.print("There are "); //start printing the result
Serial.print(space_left); //then how many spaces
Serial.print(" spaces left."); //words
Serial.println(" "); //spaces
//lcd output
lcd.clear();
lcd.print("There are "); //start printing the result
lcd.print(space_left); //then how many spaces
lcd.print(" spaces left."); //words
lcd.println(" "); //spaces
for(int i=0; i<5; i++)
{
//TEST OUTPUT: DISPLAY ARRAY. I put this here because its more accurate
Serial.print("\nid_array[");
Serial.print(i);
Serial.print("] = ");
Serial.print(id_array[i]);
//Serial.print("\nid_string is ");
//Serial.print(id_string);
}
no_match = 0;
id_string = String(""); //clear out the string
}