Hi All,
I am using the following hardware,
Arduino Mega 2560
ITBD02 moudule
3.2" TFT touchscreen module
ID-12 RFID reader x 2 (The ID-12's are connected to serial 2 & 3)
Momentary switch x 2 (The switches are connected to pins 8 & 9)
Lets say I have an RFID reader and switch at the 'start' and the the same at the 'end'.
What I would like to be able to do is scan an RFID tag at the start and then activate a switch which logs the time interval the switch was activated against that specific tag.
At the end the reverse would happen, a switch would be activated and time logged then the system would wait for an RFID tag to be scanned which will then be associated with the time just logged.
Once an RFID tag has gone through both start and end procedure I would like the time taken to be displayed on the TFT screen in minute/second/milisecond format.
This is the code I have so far, all it does is prove my hardware works, I get time intervals displayed on the screen and RFIDs spat out over serial but im not sure what I need to do next to accomplish my objective.
Any and all feedback and advice much appreciated! Regards Jason.
const int startPin = 8;
const int finishPin = 9;
// variables will change:
int buttonState; // variable for reading the pushbutton status
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
#include <UTFT.h>
// Declare which fonts we will be using
extern uint8_t SmallFont[];
UTFT myGLCD(ITDB32S,38,39,40,41); // Remember to change the model parameter to suit your display module!
unsigned long time;
unsigned short minutes;
unsigned short seconds;
unsigned long startTime;
unsigned long stopTime;
unsigned long result;
boolean newresult = false;
void setup() {
// initialize the pushbutton pin as an input:
pinMode(startPin, INPUT);
pinMode(finishPin, INPUT);
// Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
Serial.begin(9600); // connect to the serial port for output to serial monitor
Serial2.begin(9600); // serial port of RFID reader at start
Serial3.begin(9600); // serial port of RFID reader at finish
}
void loop () {
// Clear the screen and draw the frame
time = millis();
seconds = time / 1000;
minutes = seconds / 60;
//result = start - millis();
/* myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 319, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 226, 319, 239);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
*/
// read the state of the switch into a local variable:
int startState = digitalRead(startPin);
int finishState = digitalRead(finishPin);
int reading = 0;
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
if (startState == HIGH){
startTime = time;
}
if (finishState == HIGH){
result = time - startTime;
newresult=true;
}
if (newresult==true){
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 319, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 226, 319, 239);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
// set the LED using the state of the button:
myGLCD.printNumI(result, CENTER, 1);
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial2.available() > 0) {
if((val = Serial2.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if( Serial2.available() > 0) {
val = Serial2.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // if 12 digit read is complete
// Serial.print("5-byte code: ");
String thisString;
if (code[0] < 16){
thisString = String(0);
thisString += String(code[0], HEX);
}
else {
thisString = String(code[0], HEX);
}
for (i=1; i<5; i++) {
if (code[i] < 16) thisString += 0;
thisString += String(code[i], HEX);
// Serial.print(code[i]);
// Serial.print(" ");
}
Serial.print(thisString);
Serial.println();
// Serial.print("Checksum: ");
// Serial.print(code[5], HEX);
// Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
// Serial.println();
}
bytesread = 0;
}
}
if(Serial3.available() > 0) {
if((val = Serial3.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if( Serial3.available() > 0) {
val = Serial3.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // if 12 digit read is complete
String thisString;
if (code[0] < 16){
thisString = String(0);
thisString += String(code[0], HEX);
}
else {
thisString = String(code[0], HEX);
}
for (i=1; i<5; i++) {
if (code[i] < 16) thisString += 0;
thisString += String(code[i], HEX);
}
Serial.print(thisString);
Serial.println();
}
bytesread = 0;
}
}
newresult=false;
}