Embedded C Program for RFID Based Access Control System
UART, compares it with authorized IDs using if statements, and grants or denies access by controlling an output pin; for example, if(tag_id == authorized_id) { grant_access(); }.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <string.h> // Simulated function to read RFID tag ID unsigned long read_rfid() { // In real hardware, this reads from UART return 0x1A2B3C4D; // Example tag ID } void grant_access() { printf("Access Granted\n"); // Code to activate door lock relay here } void deny_access() { printf("Access Denied\n"); // Code to keep door locked here } int main() { unsigned long authorized_ids[] = {0x1A2B3C4D, 0x12345678}; unsigned long tag_id = read_rfid(); int authorized = 0; for(int i = 0; i < 2; i++) { if(tag_id == authorized_ids[i]) { authorized = 1; break; } } if(authorized) { grant_access(); } else { deny_access(); } return 0; }
Dry Run
Let's trace the example RFID tag ID 0x1A2B3C4D through the code.
Read RFID Tag
tag_id = 0x1A2B3C4D
Compare with authorized IDs
Check 0x1A2B3C4D == 0x1A2B3C4D (true), set authorized = 1
Grant or Deny Access
authorized == 1, call grant_access()
Output
Print 'Access Granted'
| Iteration | Authorized ID | Match Found | Authorized Flag |
|---|---|---|---|
| 1 | 0x1A2B3C4D | Yes | 1 |
| 2 | 0x12345678 | No | 1 |
Why This Works
Step 1: Reading RFID Tag
The program simulates reading an RFID tag ID using read_rfid(), which would normally get data from UART.
Step 2: Checking Authorization
It compares the read tag ID with stored authorized IDs using a for loop and if condition.
Step 3: Granting or Denying Access
If a match is found, grant_access() is called to unlock the door; otherwise, deny_access() keeps it locked.
Alternative Approaches
#include <stdio.h> // EEPROM read simulation unsigned long read_eeprom(int index) { unsigned long ids[] = {0x1A2B3C4D, 0x12345678}; return ids[index]; } unsigned long read_rfid() { return 0x12345678; } int main() { unsigned long tag_id = read_rfid(); int authorized = 0; for(int i=0; i<2; i++) { if(tag_id == read_eeprom(i)) { authorized = 1; break; } } if(authorized) printf("Access Granted\n"); else printf("Access Denied\n"); return 0; }
#include <stdio.h>
#include <stdbool.h>
bool check_authorized(unsigned long tag_id) {
// Simple hash simulation for demo
if(tag_id == 0x1A2B3C4D || tag_id == 0x12345678) return true;
return false;
}
unsigned long read_rfid() { return 0xFFFFFFFF; }
int main() {
unsigned long tag_id = read_rfid();
if(check_authorized(tag_id)) printf("Access Granted\n");
else printf("Access Denied\n");
return 0;
}Complexity: O(n) time, O(n) space
Time Complexity
The program checks each authorized ID once in a loop, so time grows linearly with the number of authorized IDs.
Space Complexity
Space is used to store authorized IDs in an array, proportional to the number of IDs.
Which Approach is Fastest?
Using a hash or direct checks is faster (O(1)) but less scalable; looping is simpler and flexible for small lists.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Linear Search (Loop) | O(n) | O(n) | Small to medium authorized lists |
| EEPROM Storage | O(n) | O(n) | Persistent storage, easy updates |
| Hash Table / Direct Checks | O(1) | O(1) | Fast lookup, few IDs |