#include <ID20Reader.h>
#include <SoftwareSerial.h>
#include <Servo.h>

Servo miServo;
const int LUZ_CASA = 3;
const int LUZ_CAMARA = 2;
int inByte = 0;
int RFIDResetPin = 13; //Pin que resetea el lector RFID
char tag = 0;
int flag = 0; 

//Numeros de tarjetas RFID 
//char tag1[6] = {0x4E,0x00,0x35,0x3E,0xC6,0x83};
//char tag2[6] = {0x4E,0x00,0x35,0x31,0x16,0x5C};
char tag1[13] = "4E00353EC683";
char tag2[13] = "4E003531165C";

// software serial Rx,Tx
SoftwareSerial RFID(11,10);
 
void setup(){
    Serial.begin(9600); //Abre el puerto serial USB
    RFID.begin(9600); //puerto serial RFID
    
    
    
    miServo.attach(9);  //Servo encargado seguridad puerta
    miServo.write(10);
    
    
    pinMode(11, INPUT);
    
    pinMode(LUZ_CASA, OUTPUT); //Luz
    digitalWrite(LUZ_CASA, LOW);   
    
    pinMode(LUZ_CAMARA, OUTPUT); //Luz
    digitalWrite(LUZ_CAMARA, LOW); 
    
    pinMode(RFIDResetPin, OUTPUT); //Lector Rfid 
    digitalWrite(RFIDResetPin, LOW);
  }
 
void loop(){
    
    
    if(Serial.available() > 0){
 
        inByte = Serial.read();
                
        if(inByte=='0')
            miServo.write(1);
        else if(inByte=='1') 
            miServo.write(90); 
        else if(inByte=='2')
            digitalWrite(LUZ_CASA, HIGH);  
        else if(inByte == '3')
            digitalWrite(LUZ_CASA, LOW);    
        else if(inByte == '4')
            digitalWrite(LUZ_CAMARA, HIGH);
        else if(inByte=='5')
            digitalWrite(LUZ_CAMARA, LOW);   //a tag has been read 
        if (inByte == '6'){
                        delay(50);    //new                             
                        digitalWrite(RFIDResetPin, HIGH);
                        delay(50);    //new
                        
                        char tagString[12];
                        int index = 0;
                        int readByte=0;
                    
                        //if(RFID.available() > 0 ){
                          
                             readByte = RFID.read();
                             RFID.flush();
                             while (readByte != 0x02)
                               {  //On Successful read, first byte will always be 0x02
                                readByte = RFID.read();
                                delay(300);
                               }
                                      
                                      tagString[0] = RFID.read(); //1st byte
                                      tagString[1] = RFID.read(); //2nd byte
                                      tagString[2] = RFID.read(); //3rd byte
                                      tagString[3] = RFID.read(); //4rd byte
                                      tagString[4] = RFID.read(); //5th byte
                                      tagString[5] = RFID.read(); //6th byte
                                      tagString[6] = RFID.read(); //3rd byte
                                      tagString[7] = RFID.read(); //4rd byte
                                      tagString[8] = RFID.read(); //5th byte
                                      tagString[9] = RFID.read(); //6th byte
                                      tagString[10] = RFID.read(); //5th byte
                                      tagString[11] = RFID.read(); //6th byte
                                      //tagString[12] = RFID.read(); //5th byte
                                      RFID.flush();
                                      
                                      
                                      
                                      //readByte = RFID.read();
                                      //readByte = RFID.read();
                                     
                                      //} //FIN IF
                                      
                                      checkTag(tagString); //Check if it is a match
                                    
                                      //Serial.println(tagString);
                                      //Serial.println("A"); 
                                      
                                      
                                      //clearTag(tagString); //Clear the char of all value
                                      //resetReader(); //eset the RFID reader
                                      
                                      digitalWrite(RFIDResetPin, LOW);
                                     
                        
                        }
                       
                    //Serial.listen();    
          
        }

            
}
        

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){ // if matched tag1, do this
    Serial.println(tag1); 
    

  }else if(compareTag(tag, tag2)){ //if matched tag2, do this
    Serial.println(tag2); 

  }else{
    Serial.println("Buscando..."); //read out any unknown tag
  }

}

void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
  Serial.println(pin);

  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}     





