Pages: [1]   Go Down
Author Topic: RFID turning off motion detector  (Read 607 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on a project where if a RFID tag is present, the motion sensor will turn off. When the RFID tag is removed, the motion detector is turned on. I'm using a digital pin as power for the sensor. Any idea what I'm doing wrong here?

Code:
/*
  RFID Eval 13.56MHz Shield example sketch v10
 
  Aaron Weiss, aaron at sparkfun dot com
  OSHW license: http://freedomdefined.org/OSHW
 
  works with 13.56MHz MiFare 1k tags

  Based on hardware v13:
  D7 -> RFID RX
  D8 -> RFID TX
  D9 -> XBee TX
  D10 -> XBee RX
 
  Note: RFID Reset attached to D13 (aka status LED)
 
  Note: be sure include the NewSoftSerial lib, http://arduiniana.org/libraries/newsoftserial/
 
  Usage: Sketch prints 'Start' and waits for a tag. When a tag is in range, the shield reads the tag,
  blinks the 'Found' LED and prints the serial number of the tag to the serial port
  and the XBee port.

*/
#include <SoftwareSerial.h>

SoftwareSerial rfid(7, 8);

int pinPIR = 2;
int pinPower = 10;

//Prototypes
void check_for_notag(void);
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(boolean);
void PIR(void);

//Global var
boolean flag = false;
int Str1[11];

//INIT
void setup() 
{
  Serial.begin(9600);
  Serial.println("Start");
 
  pinMode(pinPIR, INPUT);
  pinMode(pinPower, OUTPUT);
  // set the data rate for the NewSoftSerial ports
  rfid.begin(19200);
  delay(10);
  halt();
}

//MAIN
void loop()                 
{
  read_serial();
 
}

void check_for_notag()
{
  seek();
  delay(10);
  parse();
  set_flag();
 
  if(flag = 1){
    seek();
    delay(10);
    parse();
  }
}

void halt()
{
 //Halt tag
  rfid.write(255);
  rfid.write(byte(0));
  rfid.write(1);
  rfid.write(147);
  rfid.write(148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    Serial.print(Str1[8], HEX);
    Serial.print(Str1[7], HEX);
    Serial.print(Str1[6], HEX);
    Serial.print(Str1[5], HEX);
    Serial.println();
    delay(100);
    //check_for_notag();
  }
}

void read_serial()
{
  seek();
  delay(10);
  parse();
  set_flag();
  PIR();
  print_serial();
  delay(100);
}

void seek()
{
  //search for RFID tag
  rfid.write(255);
  rfid.write(byte(0));
  rfid.write(1);
  rfid.write(130);
  rfid.write(131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag = true;
  }
  if(Str1[2] == 2){
    flag = false;
  }
}

void PIR()
{
  if(rfid.available() > 0)
  digitalWrite(pinPower, HIGH);
 
  else
 
  digitalWrite(pinPower, LOW);
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33314
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any idea what I'm doing wrong here?
No, how can anyone, you haven't told us what you see when running this code. You have the hardware not us.

Quote
I'm using a digital pin as power for the sensor.
Not normally a good idea, do you know what current your sensor draws? Anything over 40mA and you damage your arduino's output pin.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Apart from the fact that RFID devices are not proximity detectors.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if(flag = 1){
You've been told that this is wrong. Why do you persist in not fixing this?

Code:
 while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }
If there is one byte of serial data to read, and that one byte happens to be 255, there is no guarantee that there are 10 more bytes. Do not assume that there are.

Why are you putting the first character in Str1[ 1 ]?

The presence or absence of data from waving a tag by the reader is not what you want to use to decide whether to turn the sensor on. The fact that you read a specific tag would be. Read that same tag again to set the pin the other way.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The RFID reader I have does not return multiple readings for the same card. You would have to walk away, and then come back to get a second reading. Therefore it would be wrong to assume that the second reading means the card is no longer present. (Nor does the absence of a reading). However some RFID readers might give multiple readings.
Logged

Pages: [1]   Go Up
Jump to: