/** * Example of how to use the RadioShack part # 276-0033 Passive Infrared * (PIR) Motion Sensor Module and RadioShack part # 276-0032 RFID reader * with tags with the Arduino (Decimila) and BlinkM LED modules. * The PIR module has a single bit output, a high value indicates the * sensor detected a change in the ir patterns in the local environment, * which usually indicates motion. The sensor is made by parallax with a * paralax part #555-28027 * The RFID reader has an enable pin and a serial out pit which uses a * baud rate of 2400. it sends one character at a time of the rfid id to * the rx pin of the arduino. * This example circuit has 3 BlinkM module lights with it because I had * some and wanted an excuse to use them. * * This code will make the circuit perform the following tasks: * If it detects motion and the motion sensor is armed, turn on the set of * 3 LEDs hooked up to pins 2,3, and 5 and turn off the BlinkM LED modules. * If you swipe the world tag RFID tag in front of the rfid reader, * the PIR sensor gets ignored and nothing happens when it detects motion. * If you swipe the card RFID tag that came with the RFID package, you * re-activate the motion sensor and the behavior as described in the * first requirement is resumed. * * See http://b1towel.com/images/microcontroller/ for images of the circuit. * * @date 13 July 2008 * @author benkillin */ #include #include "BlinkM_funcs.h" byte addrs[] = { 0x09, 0x0A, 0x0B}; int yellow = 5; // pin the yellow led is hooked up to int blue = 3; // et cetera... int red = 2; int sensorPositive = 13; // the pin that supplies power to the PIR sensor int sensor = 12; // the signal pin for the PIR sensor int sensorState = 0; // is the PIR sensor detecting changes in the environment? boolean disableSensor = false; long previousMillis = 0; // will store last time LED was updated. long interval = 1000; // 1 second interval for RFID stuff /** stuff for RFID **/ byte val; // the current byte of the tag number char code[10]; // holds the currently read tag number //*************************************************************************** // NOTE: YOU WILL HAVE TO CHANGE THESE STRINGS TO BE THE ID OF YOUR TWO TAGS! //*************************************************************************** char worldTagID[] = "0000000000"; char cardTagID[] = "FFFFFFFFFF"; char* lastUsedTag; int bytesread = 0; int rfidEnable = 4; // pin the rfid enable pin is connected to int rfidSout = 0; // the serial rx pin that the sout is connected to //Serial Serial1; boolean strcmp(char* str1, char* str2, int strlen1, int strlen2) { if(strlen1 != strlen2) { return false; } for(int i = 0; i < strlen1 && i < strlen2; i++) { if(str1[i] != str2[i]) { return false; } } return true; } void setup() { pinMode(yellow, OUTPUT); pinMode(blue, OUTPUT); pinMode(red, OUTPUT); // Set the pin the positive pin of the sensor is plugged into to high so it can supply the sensor with power pinMode(sensorPositive, OUTPUT); digitalWrite(sensorPositive, HIGH); pinMode(sensor, INPUT); BlinkM_beginWithPower(); BlinkM_setFadeSpeed(addrs[0], 20); BlinkM_setFadeSpeed(addrs[1], 20); BlinkM_setFadeSpeed(addrs[2], 20); // Serial1 = new Serial(this, Serial.list()[rfidSout], 2400); Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps pinMode(rfidEnable, OUTPUT); // Set the digital pin that the rfid enable pin is connected to as OUTPUT to connect it to the RFID /ENABLE pin digitalWrite(rfidEnable, LOW); // activate rfid reader } void loop() { sensorState = digitalRead(sensor); if(!disableSensor && sensorState == HIGH) { digitalWrite(yellow, HIGH); digitalWrite(blue, HIGH); digitalWrite(red, HIGH); for(int i = 0; i < 10; i++) { digitalWrite(yellow, HIGH); digitalWrite(blue, HIGH); digitalWrite(red, HIGH); delay(50); digitalWrite(yellow, LOW); digitalWrite(blue, LOW); digitalWrite(red, LOW); } digitalWrite(yellow, HIGH); digitalWrite(blue, HIGH); digitalWrite(red, HIGH); /*BlinkM_fadeToRGB(addrs[0], 0x00, 0x00, 0x00); BlinkM_fadeToRGB(addrs[1], 0x00, 0x00, 0x00); BlinkM_fadeToRGB(addrs[2], 0x00, 0x00, 0x00);*/ for(int i = 0; i < 3; i++) { BlinkM_fadeToRandomRGB(addrs[i], 0xAA, 0xBB, 0xCC); delay(5); } } else { digitalWrite(yellow, LOW); digitalWrite(blue, LOW); digitalWrite(red, LOW); BlinkM_fadeToRGB(addrs[0], 0x33, 0x00, 0x00); BlinkM_fadeToRGB(addrs[1], 0x00, 0x33, 0x00); BlinkM_fadeToRGB(addrs[2], 0x00, 0x00, 0x33); } if(millis() - previousMillis > interval) // wait for a second { previousMillis = millis(); // remember last time we did this digitalWrite(rfidEnable, LOW); // Activate the RFID reader if(Serial.available() > 0) { // if data available from reader if((val = Serial.read()) == 10) { // check for header bytesread = 0; 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 } } if(bytesread == 10) { // if 10 digit read is complete Serial.print("TAG code is: "); // possibly a good TAG Serial.println(code); // print the TAG code if(strcmp(code, cardTagID, 10, 10)) { lastUsedTag = cardTagID; disableSensor = true; } if(strcmp(code, worldTagID, 10, 10)) { lastUsedTag = worldTagID; disableSensor = false; } } bytesread = 0; digitalWrite(rfidEnable, HIGH); // deactivate the RFID reader } } } }