123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /*
- Very simple RFID Door Opener
-
- Input: RFID Antenna, outputs TLL UART level, 9600 8N1, 5bytes ID in binary
- Input: pushbutton to activate output
- Output: Relais connected to Solenoid controlling door lock
- */
-
- #include <SoftwareSerial.h>
-
- #define DOOR_OPEN LOW /* when correct id transmitted */
- #define DOOR_CLOSED HIGH /* idle or after timeout */
- #define DOOR_OPEN_TIME (2 * 1000) /* how long to keep door open with rfid tag */
- #define DOOR_OPEN_TIME_MANUAL (1 * 1000) /* how long to keep door open with button */
- #define BUFFER_CLEAR_TIME 250 /* bigger than time between serial input characters */
- #define ID_LENGTH 5 /* length of ID to read and compare */
- #define BUFFER_LEN (2 * ID_LENGTH) /* slightly longer than ID, just in case */
-
- // pin assignments
- static const int rfid_rx = 5;
- static const int door_out = 6;
- static const int button_in = 7;
-
- // known RFID ID codes
- static const unsigned char known_codes[][ID_LENGTH] = {
- { 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x00, 0x00, 0x00, 0x00, 0x00 }
- };
-
- unsigned char read_buffer[BUFFER_LEN];
- int buffer_pos = 0;
- unsigned long door_time = 0;
- unsigned long manual_time = 0;
- unsigned long buffer_time = 0;
- SoftwareSerial rfid_serial(rfid_rx, LED_BUILTIN);
-
- void setup() {
- pinMode(rfid_rx, INPUT);
- pinMode(button_in, INPUT);
- pinMode(door_out, OUTPUT);
-
- digitalWrite(door_out, DOOR_CLOSED);
- digitalWrite(button_in, HIGH); // enable pull-up
-
- rfid_serial.begin(9600);
- rfid_serial.println("RFID Door initialized");
-
- Serial.begin(115200);
- Serial.println("RFID Door initialized");
- }
-
- void loop() {
- // read from RFID antenna
- if (rfid_serial.available()) {
- unsigned char one = rfid_serial.read();
- read_buffer[buffer_pos] = one;
- if (buffer_pos < (BUFFER_LEN - 1)) {
- buffer_pos++;
- } else {
- buffer_pos = 0; // reset on overflow
- return;
- }
-
- // dump to USB so we can add new codes later if we need to
- Serial.print("Got byte: ");
- Serial.println(one, HEX);
-
- // store time of last received byte
- buffer_time = millis();
-
- // check for matching ID
- if (buffer_pos >= ID_LENGTH) {
- for (int o = 0; o < (buffer_pos - ID_LENGTH); o++) {
- for (int c = 0; c < (sizeof(known_codes) / sizeof(known_codes[0])); c++) {
- int match = 1;
- for (int i = 0; i < ID_LENGTH; i++) {
- if (read_buffer[o + i] != known_codes[c][i]) {
- match = 0;
- break;
- }
- }
- if (match) {
- digitalWrite(door_out, DOOR_OPEN);
- door_time = millis();
- buffer_pos = 0;
- buffer_time = 0;
- return;
- }
- }
- }
- }
- }
-
- if (digitalRead(button_in) == LOW) {
- digitalWrite(door_out, DOOR_OPEN);
- manual_time = millis();
- door_time = 0;
- }
-
- // if enough time without data, clear buffer
- if ((buffer_time != 0) && ((millis() - buffer_time) >= BUFFER_CLEAR_TIME)) {
- buffer_pos = 0;
- buffer_time = 0;
- }
-
- // if enough time since door opened, close it
- if (((door_time != 0) && ((millis() - door_time) >= DOOR_OPEN_TIME))
- || ((manual_time != 0) && ((millis() - manual_time) >= DOOR_OPEN_TIME_MANUAL))) {
- digitalWrite(door_out, DOOR_CLOSED);
- door_time = 0;
- manual_time = 0;
- }
- }
|