Pronto, já fiz umas alterações, tenho ligado por hardware serial... o que surge agora é que o butão é lido ao inicio... depois fica a espera q seja apresentada uma tag e dps é q volta a ler o butão.
Como é que faço para ele fazer as duas coisas "ao mesmo tempo" ?
Muito agradecido.
#include <LCD4Bit.h>
/**
* author Benjamin Eckel
* date 10-17-2009
*
*/
#define RFID_ENABLE 10 //to RFID ENABLE
#define CODE_LEN 10 //Max length of RFID tag
#define VALIDATE_TAG 1 //should we validate tag?
#define VALIDATE_LENGTH 200 //maximum reads b/w tag read and validate
#define ITERATION_LENGTH 2000 //time, in ms, given to the user to move hand away
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
char tag[CODE_LEN];
char validos[11] = "0F0302867B";
char validos2[11] = "0415D95D69";
int switchPin = 14; // digital input pin for a switch
int ledpin = 13;
int RED = 4;
int GREEN = 2;
int BLUE = 3;
int doorPin = 12;
LCD4Bit lcd = LCD4Bit(2);
int switchState = 0; // the state of the switch
void setup() {
Serial.begin(2400);
pinMode(RFID_ENABLE,OUTPUT);
pinMode(ledpin, OUTPUT);
pinMode(switchPin, INPUT); // set the switch pin to be an input
pinMode(doorPin, OUTPUT);
digitalWrite(doorPin, LOW); // lock door
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
inicialize();
lcd.init();
lcd.commandWrite(0x0F);
lcd.printIn("BEM-VINDO!");
delay(3000);
lcd.clear();
lcdMessage();
}
void loop() {
interrupts();
lerSwitch();
enableRFID();
getRFIDTag();
if(isCodeValid()) {
disableRFID();
sendCode();
eValido();
delay(ITERATION_LENGTH);
} else {
disableRFID();
Serial.println("Got some noise");
}
Serial.flush();
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(BLUE, HIGH);
clearCode();
}
void lcdMessage(){
lcd.printIn(" Apresente ");
lcd.cursorTo(2, 0);
lcd.printIn(" ID! ");
}
void lerSwitch() {
switchState = digitalRead(switchPin);
Serial.println("switch:");
Serial.println(switchState);
if (switchState==1) { // if the switch is closed
digitalWrite(doorPin, HIGH);
digitalWrite(ledpin, HIGH);
delay(9000);
digitalWrite(ledpin, LOW);
digitalWrite(doorPin, LOW);
}
}
void eValido() {
if (checkTag(validos) || checkTag(validos2)) {
lcd.clear();
lcd.printIn(" ID ");
lcd.cursorTo(2, 0);
lcd.printIn(" VALIDA! ");
sucess();
lcd.clear();
lcdMessage();
} else {
lcd.clear();
lcd.printIn(" ID ");
lcd.cursorTo(2, 0);
lcd.printIn(" INVALIDA! ");
failed();
lcd.clear();
lcdMessage();
}
}
boolean checkTag(char *validos){
for (int x=0;x<10;x++){
if( validos[x] != tag[x]){
return false;
}
}
return true;
}
void failed()
{
digitalWrite(doorPin, LOW);
digitalWrite(GREEN, LOW); // Make sure green LED is off
digitalWrite(BLUE, LOW); // Make sure blue LED is off
// Blink red fail LED 3 times to indicate failed key
for(int i=0; i<5; i++) {
digitalWrite(RED, HIGH); // Turn on red LED
delay(500);
digitalWrite(RED, LOW); // Turn off red LED
delay(500);
}
}
void sucess()
{
digitalWrite(doorPin, HIGH);
digitalWrite(RED, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, HIGH);
delay(9000);
digitalWrite(GREEN, LOW);
digitalWrite(doorPin, LOW);
}
void inicialize() {
for(int i=0; i<8; i++)
{
digitalWrite(BLUE, HIGH);
delay(100);
digitalWrite(BLUE, LOW);
delay(150);
}
digitalWrite(BLUE, HIGH);
}
/**
* Clears out the memory space for the tag to 0s.
*/
void clearCode() {
for(int i=0; i<CODE_LEN; i++) {
tag[i] = 0;
}
}
/**
* Sends the tag to the computer.
*/
void sendCode() {
Serial.print("TAG:");
//Serial.println(tag);
for(int i=0; i<CODE_LEN; i++) {
Serial.print(tag[i]);
}
}
/**************************************************************/
/******************** RFID Functions ***********************/
/**************************************************************/
void enableRFID() {
digitalWrite(RFID_ENABLE, LOW);
}
void disableRFID() {
digitalWrite(RFID_ENABLE, HIGH);
}
/**
* Blocking function, waits for and gets the RFID tag.
*/
void getRFIDTag() {
byte next_byte;
while(Serial.available() <= 0) {}
if((next_byte = Serial.read()) == START_BYTE) {
byte bytesread = 0;
while(bytesread < CODE_LEN) {
if(Serial.available() > 0) { //wait for the next byte
if((next_byte = Serial.read()) == STOP_BYTE) break;
tag[bytesread++] = next_byte;
}
}
}
}
/**
* Waits for the next incoming tag to see if it matches
* the current tag.
*/
boolean isCodeValid() {
byte next_byte;
int count = 0;
while (Serial.available() < 2) { //there is already a STOP_BYTE in buffer
delay(1); //probably not a very pure millisecond
if(count++ > VALIDATE_LENGTH) return false;
}
Serial.read(); //throw away extra STOP_BYTE
if ((next_byte = Serial.read()) == START_BYTE) {
byte bytes_read = 0;
while (bytes_read < CODE_LEN) {
if (Serial.available() > 0) { //wait for the next byte
if ((next_byte = Serial.read()) == STOP_BYTE) break;
if (tag[bytes_read++] != next_byte) return false;
}
}
}
return true;
}