ขั้นตอนนี้คือการเตรียมส่วนของการขับเคลื่อน
วันเสาร์ที่ 17 สิงหาคม พ.ศ. 2556
วันอังคารที่ 6 สิงหาคม พ.ศ. 2556
Arduino กับ RFID
/* * rfidrelay.c * * Signals HIGH voltage to a relay when a proper code is received from an RFID * swipe event, and switches the voltage back to LOW when the RFID card is * swiped again. * * Authors: Blake Beaupain & Andrew Stucky */ #include <SoftwareSerial.h> #define rxPin 8 #define txPin 9 #define RFID_PIN 2 #define RELAY_PIN 5 #define NUM_CODES 2 // The transmitting flag int TRANSMITTING = 0; // The RFID chip code char *expected_codes[NUM_CODES]; /* * Sets up the device */ void setup() { Serial.begin(2400); // Hardware serial for Monitor 2400bps expected_codes[0] = "8400336CFF"; expected_codes[1] = "00005D7E5C"; // Activate the RFID reader pinMode(RFID_PIN, OUTPUT); digitalWrite(RFID_PIN, LOW); // Activate the relay pin pinMode(RELAY_PIN, OUTPUT); analogWrite(RELAY_PIN, 0); } /* * Main logic loop */ void loop() { SoftwareSerial RFID = SoftwareSerial(rxPin,txPin); RFID.begin(2400); // Read data from the chip if (RFID.available() > 0) { // Read the full code char code[10]; int amount = 0; while (amount < 10) { int data = RFID.read(); if (data == 10 || data == 13) { Serial.println("Incomplete packet"); return; }// Incomplete packet code[amount++] = data; } // Authenticate the code int i, n, found = 0; for (i = 0; i < NUM_CODES; i++) { char *c = expected_codes[i]; for (n = 0; n < 10; n++) if (code[n] != c[n]) continue; found = 1; Serial.println("Pass"); //Passed Auth } if (found == 0) Serial.println("Fail"); //Failed Auth // Toggle the transmitting flag TRANSMITTING ^= 1; // Apply the proper voltage to the relay pin if (TRANSMITTING) { // Set to high voltage analogWrite(RELAY_PIN, 255); } else { // Set to low voltage analogWrite(RELAY_PIN, 0); } // Disable the RFID chip temporarily digitalWrite(RFID_PIN, HIGH); delay(2000); digitalWrite(RFID_PIN, LOW); } }
ที่มา
http://forum.arduino.cc/index.php/topic,87305.0.html
######################################################################## ## Modified code to work with Arduino 1.0.1 / ## ## Credits to Petushka, http://www.instructables.com/member/Petushka/ ## ## ## ######################################################################## #include <SoftwareSerial.h> #define ADD_TAG_CODE "210014DFE309" //change this ID with your own card TAG #define DEL_TAG_CODE "210014E2BD6A" //change this ID with your own card TAG SoftwareSerial rfid = SoftwareSerial(5, 6); String msg; String ID ; //string to store allowed cards void setup() { Serial.begin(9600); Serial.println("Serial Ready"); rfid.begin(9600); Serial.println("RFID Ready"); } char c; void loop(){ while(rfid.available()>0){ c=rfid.read(); msg += c; Serial.println(msg); Serial.println(msg.length()); } msg=msg.substring(1,13); if(msg.indexOf(ADD_TAG_CODE)>=0) add(); else if(msg.indexOf(DEL_TAG_CODE)>=0) del(); else if(msg.length()>10) verifica(); msg=""; } void add(){ Serial.print("What TAG do you wanna grant access?: "); msg=""; while(msg.length()<13){ while(rfid.available()>0){ c=rfid.read(); msg += c; } } if(ID.indexOf(msg)>=0) { Serial.println("\nAccess already granted for this card."); msg=""; } else{ Serial.print("Card: "); Serial.println(msg); ID += msg; ID += ","; //Serial.print("ID: "); // Serial.println(ID); msg=""; Serial.println("Access granted for this card."); } } void del(){ msg=""; Serial.print("What TAG do you wanna deny access?: "); while(msg.length()<13){ while(rfid.available()>0){ c=rfid.read(); msg += c; } } msg=msg.substring(1,13); if(ID.indexOf(msg)>=0){ Serial.println(msg); Serial.println("TAG found. Access for this card denied."); //ID.replace(card,""); int pos=ID.indexOf(msg); msg=""; msg += ID.substring(0,pos); msg += ID.substring(pos+15,ID.length()); ID=""; ID += msg; //Serial.print("ID: "); //Serial.println(ID); } else Serial.println("\nTAG not found or already denied"); msg=""; } void verifica(){ msg=msg.substring(1,13); if(ID.indexOf(msg)>=0) Serial.println("Access granted."); else Serial.println("Access denied."); }ที่มาhttp://www.instructables.com/files/orig/FM4/KU9W/H91LGBA6/FM4KU9WH91LGBA6.txt#include <SoftwareSerial.h> #include <SimpleTimer.h> #define TAG_CODE "0800683B9BC0" //change this ID with your own card TAG int led3 = 3; int wd_timer_id; // The transmitting flag int TRANSMITTING = 0; SoftwareSerial rfid = SoftwareSerial(8, 9); String msg; String ID ; //string to store allowed cards SimpleTimer timer; // function to be called just once void OnceOnlyTask() { Serial.println("This timer only triggers once"); digitalWrite(led3, LOW); } void setup() { Serial.begin(9600); Serial.println("Serial Ready"); rfid.begin(9600); Serial.println("RFID Ready"); pinMode(led3, OUTPUT); } char c; void loop(){ timer.run(); if(rfid.available()>0){ while(rfid.available()>0){ c=rfid.read(); msg += c; // Serial.println(msg); // Serial.println(msg.length()); } msg=msg.substring(1,13); if(msg.length()==12) Serial.println(msg); ID = msg; msg=""; if (ID.indexOf(TAG_CODE)>=0){ Serial.println(ID); digitalWrite(led3, HIGH); timer.deleteTimer(wd_timer_id); delay(1000); TRANSMITTING = 1; } }else{ //Serial.println(ID); if(TRANSMITTING){ if(timer.getNumTimers()<=0){ timer.enable(wd_timer_id); timer.setTimeout(5000, OnceOnlyTask); } TRANSMITTING = 0; } } }
สมัครสมาชิก:
บทความ (Atom)