-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerial.ino
238 lines (181 loc) · 4.77 KB
/
Serial.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
/*
* Define pins for connection with RFID controls
*/
#define SDA_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SDA_PIN,RST_PIN);
/*
* Initialize LCD with following connections
*/
LiquidCrystal lcd(12, 11, 22, 23, 24, 25);
/*
* Define pins for connection with barriers (servo motors)
*/
#define ENTRY_IR 6
#define EXIT_IR 7
/*
* Define variables for barrier control
*/
int state;
int entry_init_state;
int exit_init_state;
byte data;
int status;
int overloaded;
/*
Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
/*
* Barrier initialization
*/
pinMode(ENTRY_IR, INPUT);
pinMode(EXIT_IR,INPUT);
entry_init_state = digitalRead(ENTRY_IR);
exit_init_state = digitalRead(EXIT_IR);
/*
* Barrier initialization
*/
lcd.begin(20, 4);
lcd.setCursor(0, 1);
}
void loop() {
lcd.clear();
/*
* monitor entry barrier
*/
monitor_barrier(ENTRY_IR, entry_init_state);
/*
* Display first welcome note and delay for 300ms
*/
display_notification("WELCOME..");
delay(1000);
/*
* Clear screen an display tag scanning notification
*/
lcd.clear();
display_notification("SCANNING TAG");
lcd.setCursor(0, 2);
display_notification("PLEASE WAIT...");
delay(1000);
read_tag_id();
//Get validation status
status = get_status();
if(status == 1) {
lcd.clear();
display_notification("Truck Authorized");
delay(1000);
lcd.setCursor(0, 1);
display_notification("Measuring GVM....");
measure_gvm();
lcd.setCursor(0, 2);
display_notification("34.98 tonnes");
delay(1000);
//get overloaded status
overloaded = get_status();
if (overloaded == 1) {
measure_gvm();
lcd.clear();
display_notification("Truck is overloaded");
lcd.setCursor(0, 2);
display_notification("Enter holding area");
delay(1000);
} else {
//Display goodbye note
lcd.clear();
display_notification("Truck is OK");
lcd.setCursor(0, 2);
display_notification("Have a safe trip");
delay(1000);
monitor_barrier(EXIT_IR, exit_init_state);
}
} else {
lcd.clear();
display_notification("Invalid Truck");
lcd.setCursor(0, 2);
display_notification("Enter holding area");
delay(1000);
}
lcd.clear();
display_notification("SCANNING TAG");
delay(1000);
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? "0" : "");
Serial.print(buffer[i], HEX);
}
}
void read_tag_id() {
// Serial.println("wait...");
while ( ! mfrc522.PICC_IsNewCardPresent()) {}
while ( ! mfrc522.PICC_ReadCardSerial()) {}
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println("");
Serial.flush();
mfrc522.PICC_HaltA(); //stop reading
}
int get_status() {
while( ! Serial.available()) {}
return Serial.parseInt();
}
/*
* Function to monitor if a truck crossed the barrier
*/
void monitor_barrier(int ir, int ir_state) {
/*
* Read the current state of push button
*/
int current_state = digitalRead(ir);
/*
* Monitor for any change of ir barrier state
*/
while(current_state == ir_state) {
delay(100);
/*
* Check ir state again
*/
current_state = digitalRead(ir);
}
/*
* Check if initial state of ir barrier is restored
*/
while(current_state != ir_state) {
delay(100);
/*
* Check button state again
*/
current_state = digitalRead(ir);
}
}
void measure_gvm() {
float f = 40.65;
byte * b = (byte *) &f;
Serial.write(b[0]);
Serial.write(b[1]);
Serial.write(b[2]);
Serial.write(b[3]);
Serial.println();
}
/*
* Function to display notification
*/
void display_notification(String message) {
lcd.print(message);
}