help needed to modify RFID (wiegand - HID reader) door lock

Hi All,

I'm new to both arduino and programming..but have a strong background in electronics and computers.

i'm inspired with the rfid door lock project of 'bolsontanker' :

http://www.rfidtoys.net/forum/forum_posts.asp?TID=698&title=rfid-door-using-strike-plate-wiegand-interface

and want to implement with 3 additions.

  1. add status LED - if access is denied (to pin 11)
  2. add a switch to open door from inside
  3. run a light for 30 seconds when door is opened ( pin 10)

my modified code:

/* Basheer Olakara
 * Crazy People
 * By Mike Cook April 2009
 * Three RFID readers outputing 26 bit Wiegand code to pins:-
 * Reader A (Head) Pins 2 & 3
 * Interrupt service routine gathers Wiegand pulses (zero or one) until 26 have been recieved
 * Then a sting is sent to processing
 */
int validSiteCodes[] = {93, 196, 452};
int validSerialNumbers[] = {6032, 15809, 3285};// see if site code and serial number are in the lists...

volatile long reader1 = 0;
volatile int reader1Count = 0;

void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}

void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1;
}

void setup()
{
  Serial.begin(9600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  attachInterrupt(0, reader1Zero, RISING);//DATA0 to pin 2
  attachInterrupt(1, reader1One, RISING); //DATA1 to pin 3
  delay(10);
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 2; i<4; i++){
  pinMode(i, OUTPUT);
   digitalWrite(i, HIGH); // enable internal pull up causing a one
  digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
  pinMode(i, INPUT);
  digitalWrite(i, HIGH); // enable internal pull up
  }
  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  //digitalWrite(13, HIGH);  // show Arduino has finished initilisation
}

void loop()
{
  if(reader1Count >=26){
//Serial.print(" Reader 1 ");
//Serial.println(reader1,HEX);
 // Serial.println("A");
 //Serial.println(reader1& 0xfffffff);
 int serialNumber=(reader1 >> 1) & 0x3fff;
 int siteCode= (reader1 >> 17) & 0x3ff;

 Serial.print(siteCode);
 Serial.print("  ");
 Serial.println(serialNumber);
  reader1 = 0;
  reader1Count = 0;
if(IsTagValid(siteCode, serialNumber)
){
  digitalWrite(12,HIGH);
}
  else
    digitalWrite(12,LOW);
    delay(2000);
    digitalWrite(12,LOW);   // Open the door. It's cold out here!
}
     }

boolean IsTagValid(int siteCode, int serialNumber)
{
   boolean valid = false;

   // Determine how many valid tags there are
   int validTagCount = sizeof(validSiteCodes)/sizeof(int);

   // Loop through the arrays to see if siteCode
   // and serialNumber are present

   for(int t=0; t<validTagCount; t++)
   {
    if(validSiteCodes[t] == siteCode &&
       validSerialNumbers[t] == serialNumber)
    {
        valid = true;
        break;
    }
   }

   return valid;
}

Components used:

  1. arduino uno clone with an ATmega8A-PU chip
  2. HID Reader ((wiegand type - proxpoint plus 6005BGB00 )
  3. 12v Open Frame Type Actuator Door Lock
  4. 5v /1A, 12v /3A SMPS power supply

any help appreciated,

TNX :slight_smile:

my modified code:

Which does something. What does it do? What does it do that you don't want? What does it not do that you do want?

Thank you for the reply,

it works, i mean it read the valid cards and pin 12 get HIGH for 2 seconds. I'm waiting for the 12V door lock to come from ebay and planing to drive via a transistor circuit.

my HID reader has built in dual color LED (RED, GREEN) and a buzzer to feedback. Buzzer beeps for any cards and the GREEN LED will lit from the PIN 12 output. Now I need the RED to lit 2 seconds for wrong keys .

My door lock will be fixed on the door frame, so need a push button to open from inside (while pushing the button, pin 12 must go HIGH for 2 seconds) .

Also another pin has to be HIGH with pin 12 for 30 seconds to switch on a light inside while entering home.... :slight_smile:

I hope you understand, sorry for my poor English

Thanks.,

My door lock will be fixed on the door frame, so need a push button to open from inside (while pushing the button, pin 12 must go HIGH for 2 seconds) .

Reading the state of a pin that a properly wired switch is attached to is trivial. Using an if statement hardly requires more than a few functioning brain cells.

What problems are you having doing those two simple tasks?

Turning another pin on, when you turn pin 12 on, is trivial. Recording when you did that, using millis() is simple.

Periodically, checking that the light pin is on, and determining how long it has been on doesn't really qualify as difficult. Turning the pin off if it has been on long enough doesn't, either.

Everything you need is in the blink without delay sketch that you certainly should have studied by now.

any help appreciated,

...

it works,

It's not clear what you need help with.

i got my arduino only 4 days back and barley know how to run it.

i know how to work with a push button , turning another pin with 30 second delay - but don't know how to mix all these sketches without conflict.

i thought some arduino 'Gurus' will help me to add the simple codes to existing. Now i feel i can do it only after i learn myself.

Thank you,

Now i feel i can do it only after i learn myself.

There are plenty of examples provided with the IDE. Reading the state of a switch is one line of code:

   int pinState = digitalRead(pinNumber);

Turning a pin on or off is one line of code:

   digitalWrite(lightPin, lightState);

Creating a simple sketch that turns a pin on when a switch is pressed is something you should try. Then, you can confirm that the switch is wired correctly and that the LED is wired correctly.

Then, you can get more complicated. Turn the LED on when the switch is pressed the first time. Turn it off when the switch is pressed again. To do that, you'll discover that you need to learn when the switch BECOMES pressed. The state change detection example shows how to do that.

The examples show how to do discrete things. Combining the discrete things into a functioning application is a matter of understanding what the discrete things are doing.

Having someone else write the code for you means that you won't learn anything. If you didn't want to learn, you should have bought a turn-key system. Or hired someone.

olakara:
i thought some arduino 'Gurus' will help me to add the simple codes to existing. Now i feel i can do it only after i learn myself.

A lot of gurus have written posts and maintain sites explaining this sort of thing. All you have to do is read them.

thanks for all the help,

PUSH TO ON is working after adding below:

const int buttonPin = 7;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
pinMode(buttonPin, INPUT_PULLUP);


void loop(){
 buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // button door open:
    digitalWrite(12, HIGH);
  }
  else {
    // button door closed:
    digitalWrite(12, LOW);
  }

when I add below to lit RED LED for wrong access keys, it is okay. but this LED also ON when i use vaild keys !, don't know what is wrong in my if-else statement....

if(IsTagValid(siteCode, serialNumber)
){
  digitalWrite(12,HIGH);
  digitalWrite(13,LOW);   
}
  else
    digitalWrite(12,LOW);
    digitalWrite(13,HIGH);   // wrong key, RED LED ON !
    delay(2000);
    digitalWrite(12,LOW);   // Open the door. It's cold out here!
    digitalWrite(13,LOW);
}
     }
if(IsTagValid(siteCode, serialNumber)
){

Have you EVER seen an example where the ) was on a new line?

The else block does not have curly braces. If statements and else statements ALWAYS should have curly braces.

I don't understand this bit: … oh! actually I do understand it, it's just that you haven't hit the "format my code" key and the indenting is a little screwy.

    if (IsTagValid(siteCode, serialNumber)) {
      digitalWrite(12, HIGH);
    }
    else
      digitalWrite(12, LOW);
    delay(2000);
    digitalWrite(12, LOW);  // Open the door. It's cold out here!

If the tag is valid, unlock the door. If it isn't, lockj it (even though presumably it's already locked) and then, irrespective of whether the tag was valid or not, lock it again after 2 seconds.

  1. add status LED - if access is denied (to pin 11)

Easy as.

[code]
    if (IsTagValid(siteCode, serialNumber)) {
      digitalWrite(12, HIGH);
    }
    else {
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
    }
    delay(2000);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);  // Open the door. It's cold out here!
  1. add a switch to open door from inside

Ok - let's put that on pin 5 (because why not?) You want to do this outside the reader count. So AFTER the if(readerCount) {} , add something to the effect:

if(digitalRead(5)==HIGH) {
    digitalWrite(12, HIGH);
    delay(2000);
    digitalWrite(12, LOW);  // Open the door. It's cold out here!
}
  1. run a light for 30 seconds when door is opened ( pin 10)

Well, we have two choices here. The simplest solution is:

    digitalWrite(12, HIGH);
    digitalWrite(10, HIGH);
    delay(2000);
    digitalWrite(12, LOW);  // Open the door. It's cold out here!
    delay(8000);
    digitalWrite(10, LOW);

but we need that in two different places - open the door because switch, and open the door because card. So we break out 'open the door' into a function.

void openTheDoor() {
    digitalWrite(12, HIGH);
    digitalWrite(10, HIGH);
    delay(2000);
    digitalWrite(12, LOW);  // Open the door. It's cold out here!
    delay(8000);
    digitalWrite(10, LOW);
}

and replace the two "open the door" sections of code with just a call to that function:

 if (reader1Count >= 26) {
   ...;
   if (IsTagValid(siteCode, serialNumber)) {
     openTheDoor();
   }
   else {
     ...
   }
  ...
  }

  if(digitalRead(5)==HIGH) {
    openTheDoor();
  }

Now, an issue here is that your ten-second light will block - you won't be able to do anything else (except read the card) when the light is on. If you don't want this behaviour, then you need to examine the "blink without delay" example in the arduino IDE.
[/code]

thanks for writing the codes

now only the LIGHT left :slight_smile:

actually I need the light only for cards (no need for push button) for minimum 30 seconds. I know if I use "delay" it will freeze everything for 32 seconds.

PaulS also mentioned to use millis()

let me try ...

thanks again

finally done :slight_smile:

/* Basheer Olakara
 * Crazy People
 * By Mike Cook April 2009
 * Three RFID readers outputing 26 bit Wiegand code to pins:-
 * Reader A (Head) Pins 2 & 3
 * Interrupt service routine gathers Wiegand pulses (zero or one) until 26 have been recieved
 * Then a sting is sent to processing
 */
int validSiteCodes[] = {93, 196, 452};
int validSerialNumbers[] = {6032, 15809, 3285};// see if site code and serial number are in the lists...
const int buttonPin = 7;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status
const int lightPin =  10;      // the number of the Light pin
unsigned long off_time;

boolean ledState = false;

int button1State = 0;         // variable for reading the lock status

volatile long reader1 = 0;
volatile int reader1Count = 0;

void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}

void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1;
}

void setup()
{ // initialize the Light pin as an output:
  pinMode(lightPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  digitalWrite(lightPin, LOW);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  attachInterrupt(0, reader1Zero, RISING);//DATA0 to pin 2
  attachInterrupt(1, reader1One, RISING); //DATA1 to pin 3
  delay(10);
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for (int i = 2; i < 4; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // enable internal pull up causing a one
    digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
    pinMode(i, INPUT);
    digitalWrite(i, HIGH); // enable internal pull up
  }
  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  //digitalWrite(13, HIGH);  // show Arduino has finished initilisation
}

void loop() {
  if ((ledState) && (millis() >= off_time)) /* is it on and is it later or equal to off_time */
  {
    digitalWrite(lightPin, LOW);
    ledState = false;
  }
  else if (!ledState) /* is it off? */
  {
    button1State = digitalRead(13);
    if (button1State == HIGH)
    {
      digitalWrite(lightPin, HIGH);
      ledState = true;
      off_time = millis() + 30000;
    }
  }
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // button door open:
    digitalWrite(13, HIGH);
  }
  else {
    // button door closed:
    digitalWrite(13, LOW);
  }

  if (reader1Count >= 26) {
    //Serial.print(" Reader 1 ");
    //Serial.println(reader1,HEX);
    // Serial.println("A");
    //Serial.println(reader1& 0xfffffff);
    int serialNumber = (reader1 >> 1) & 0x3fff;
    int siteCode = (reader1 >> 17) & 0x3ff;

    Serial.print(siteCode);
    Serial.print("  ");
    Serial.println(serialNumber);
    reader1 = 0;
    reader1Count = 0;
    if (IsTagValid(siteCode, serialNumber)) {
      digitalWrite(13, HIGH);
    }
    else {
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
    }
    delay(2000);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);  // Open the door. It's cold out here!
  }
}
boolean IsTagValid(int siteCode, int serialNumber)
{
  boolean valid = false;

  // Determine how many valid tags there are
  int validTagCount = sizeof(validSiteCodes) / sizeof(int);

  // Loop through the arrays to see if siteCode
  // and serialNumber are present

  for (int t = 0; t < validTagCount; t++)
  {
    if (validSiteCodes[t] == siteCode &&
        validSerialNumbers[t] == serialNumber)
    {
      valid = true;
      break;
    }
  }

  return valid;
}

Thanks all

We gave someone some advice, they actyally took it and solved their own problem.

My faith in humanity is briefly restored :slight_smile: