vrijdag 11 februari 2011

My first Arduino project - part 2

Initial setup

I have put the Data logger shield onto the Arduino board and checked the pin usage of this shield on the shieldlist website.

Next I hooked up the Parallax RFID to the Arduino board via the connectors I soldered onto the data logger shield.  I based myself on the documentation found at the Arduino playground.  I did however modify the given examples to make sure the pins used by the parallax RFID reader did not interfere with the pins used by the data logger and I restructured the code a bit.

One additional component I added was a LED to indicate whether to door should get unlocked or not.

Current functionality

The code below allows reading and displaying RFID codes.  It will 'unlock the door' (power the LED) when an RFID code is read that is different from the last read RFID code. If the same RFID token is read several times in succession, it will only 'unlock the door' the first time it is read. Later I'll adjust to code to make sure this is time boxed. But first I'll see if I get the data logger working properly

The source code


int LED = 7;
int RFID_ENABLE = 2;
int CODE_SIZE = 10;
int ARRAY_SIZE = 11;


int bytesRead = 0;
char lastReadRFID[11];
char code[11];


//===================================================================================
// SETUP METHOD
//===================================================================================
void setup() { 
  
  Serial.begin(2400);     // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
  pinMode(RFID_ENABLE,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
  pinMode(LED, OUTPUT);   // Set digital pin 13 as OUTPUT to connect it to LED
  activateRFID();
}  


//===================================================================================
// MAIN APP LOOP
//===================================================================================
void loop() { 
  activateRFID();
  readRFID();
  if (bytesRead == CODE_SIZE) {
    deActivateRFID();
    handleReadRFID();
  }
}


//===================================================================================
// handle the read RFID tag
//===================================================================================
void handleReadRFID(){
  if (codesAreDifferent() ) {
    Serial.print("TAG code is: ");       // possibly a good TAG 
    Serial.print(code);                  // print the TAG code 
    Serial.print(" while old tag is ");  // print the TAG code 
    Serial.println(lastReadRFID);        // print the TAG code 
    deActivateRFID();
    digitalWrite(LED, HIGH);             // Switch on the LED
    delay(2000);                         // wait for 2 seconds
    setLastReadRFID();
    digitalWrite(LED, LOW);              // Switch off the LED
  } else {
    Serial.print("new (");               // possibly a good TAG 
    Serial.print(code);                  // print the TAG code 
    Serial.print(") equals new (");      // print the TAG code 
    Serial.print(lastReadRFID);          // print the TAG code 
    Serial.println(")");                 // print the TAG code 
  }
}


//===================================================================================
// checks wether the current code is different from the previously read code
//===================================================================================
boolean codesAreDifferent(){
  for (int i = 0; i < CODE_SIZE; i++) {
    if (code[i] != lastReadRFID[i]) {
      return true;
    }
  }
  return false;
}
  
//===================================================================================
// Activate the RFID reader
//===================================================================================
void activateRFID() {
    digitalWrite(RFID_ENABLE, LOW);// Activate the RFID reader
}
  
//===================================================================================
// DeActivate the RFID reader
//===================================================================================
void deActivateRFID() {
    digitalWrite(RFID_ENABLE, HIGH);// Deactivate the RFID reader
}
  
//===================================================================================
// read the code from the RFID reader
//===================================================================================
void readRFID(){
  bytesRead = 0;
  int val = 0;
    
  if(Serial.available() > 0) {          // if data available from reader 
    if((val = Serial.read()) == CODE_SIZE) {   // check for header 
      while(bytesRead<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          }
          code[bytesRead] = val;         // add the digit           
          bytesRead++;                   // ready to read next digit  
        } 
      } 
    }
  }
}


//===================================================================================
// set the lastReadRFID code
//===================================================================================
void setLastReadRFID(){
  for (int i = 0; i < CODE_SIZE; i++) {
    lastReadRFID[i] = code[i];        // copy the content of the last read RFID code into lastReadRFID
  }
  lastReadRFID[10] = 0;
  code[10] = 0;
}





I'll see in the future if I can host this test code on a public repository somewhere

Stats
compiler
Binary sketch size: 3176 bytes (of a 30720 byte maximum)
So this still leaves me 27544 bytes room, I'll see what will be left after I implemented all data logger logic.

pins
Currently I'm using digital pins 0, 2, 7, 10, 11, 12, 13; analog pins 4 and 5 and the 5V pin.
So 6 digital pins left and a few analog pins

Geen opmerkingen:

Een reactie posten