Advertisement
safwan092

[CLEAN] Arduino + UHF RFID KLM900S + Ultrasonic + Servo - [NO DELAY]

Jul 25th, 2022
1,497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.60 KB | None | 0 0
  1. /*
  2.   Connections:
  3.   ------------------------------
  4.   Arduino Uno <---> RF-KLM900S
  5. ******************************
  6.     3.3V    <--->     EN
  7.     5V      <--->     5V
  8.     GND     <--->     GND
  9.     3       <--->     TTL_R
  10.     2       <--->     TTL_T
  11.   ------------------------------
  12. */
  13.  
  14. // Libraries
  15. #include <SoftwareSerial.h>
  16. #include <Servo.h>
  17.  
  18. // Pin Definitions
  19. #define trigPin   8
  20. #define echoPin   9
  21. #define rxPin     2
  22. #define txPin     3
  23. #define servoPin  11
  24.  
  25. // Objects and Communication Chanel
  26. Servo servo1;
  27. SoftwareSerial SSerial(rxPin, txPin); // RX, TX
  28.  
  29. // Command to Read RFID Tags Using RF-KLM900S TTL Connector
  30. // Single Read  BB 00 22 00 00 22 7E
  31. byte A[] = {0xBB, 0x00, 0x22, 0x00, 0x00, 0x22, 0x7E};
  32.  
  33. // Saved RFID Tags in one String Array
  34. // [1] Card  Tag   UID : 30 08 33 B2 DD D9 01 40 00 00 00 00
  35. // [2] Smart Tag 1 UID : E2 80 68 94 00 00 50 20 91 08 58 7E
  36. // [3] Smart Tag 2 UID : E2 80 68 94 00 00 50 20 91 08 58 88
  37. String TagNo[] = {"30 08 33 B2 DD D9 01 40 00 00 00 00",
  38.                   "E2 80 68 94 00 00 50 20 91 08 58 7E",
  39.                   "E2 80 68 94 00 00 50 20 91 08 58 88"
  40.                  };
  41.  
  42. // Variables and Flags
  43. String TagID_RX = "";
  44. static String inputString;
  45. static unsigned long currentTime = 0;
  46. static unsigned long lastTime = 0;
  47. static unsigned long lastTime2 = 0;
  48. static unsigned long lastCharArrivalTime = 0;
  49. long distance;
  50. long duration;
  51. int flag = 0;     // Tag was Identifyed before  [Flag]
  52. int flag3min = 0; // Show Timer Started Message [Flag]
  53. int count = 0;
  54. int numOfTags = 3;
  55.  
  56. void setup()
  57. {
  58.   // Communication Chanel Between Arduino and RF-KLM900S RFID Reader
  59.   SSerial.begin(115200);
  60.  
  61.   // Communication Chanel Between Arduino and PC/Laptop/Android Phone
  62.   Serial.begin(115200);
  63.  
  64.   // Assign Inputs and Outputs
  65.   pinMode(trigPin, OUTPUT);
  66.   pinMode(echoPin, INPUT);
  67.   servo1.attach(servoPin);
  68.  
  69.   // Close Servo Gate
  70.   servo1.write(0);
  71.  
  72.  
  73.   delay(2000);
  74. }
  75.  
  76. void loop() {
  77.  
  78.   // Messure Distance from Ultrasonic Sensor to Object
  79.   ultra();
  80.  
  81.   lastCharArrivalTime = 0;
  82.  
  83.   //////////////////////////////////////////////////////////////////////////////////////////////
  84.   //------------------------[Send Read Command To RFID Reader]----------------------------------
  85.   //////////////////////////////////////////////////////////////////////////////////////////////
  86.   if (millis() - lastTime > 50 && inputString == "") {
  87.     SSerial.write(A, sizeof(A));
  88.     lastTime = millis();
  89.   }
  90.   //////////////////////////////////////////////////////////////////////////////////////////////
  91.   //------------------------[Read RFID DATA and Convert to HEX]---------------------------------
  92.   //////////////////////////////////////////////////////////////////////////////////////////////
  93.   if (SSerial.available())
  94.   {
  95.     byte incomingByte = SSerial.read();
  96.     inputString += "0123456789ABCDEF"[incomingByte / 16];
  97.     inputString += "0123456789ABCDEF"[incomingByte % 16];
  98.     inputString += ' ';
  99.     lastCharArrivalTime = millis();
  100.   }
  101.   //////////////////////////////////////////////////////////////////////////////////////////////
  102.   //--------------------------[When Read is Done Analyze Data]----------------------------------
  103.   //////////////////////////////////////////////////////////////////////////////////////////////
  104.   if (lastCharArrivalTime != 0 && millis() - lastCharArrivalTime >= 10) {
  105.  
  106.     // Extract Tag UID from Received RFID Data Stream
  107.     TagID_RX = inputString.substring(24, 59);
  108.  
  109.     // Loop To Compare All Saved Tag UIDs to the Received tag UID
  110.     for (int i = 0; i < numOfTags; i++) {
  111.  
  112.       // Compare Saved Tag UID to the Received tag UID (AND) Distance must be less or equal to 200cm
  113.       if (!TagID_RX.compareTo(TagNo[i]) && distance <= 200) {
  114.         Serial.print("Tag No [");
  115.         Serial.print(i + 1);
  116.         Serial.println("] Found.");
  117.         Serial.println("------------------");
  118.  
  119.         // Open Servo Gate
  120.         servo1.write(90);
  121.  
  122.         delay(1000);
  123.  
  124.         // Activate Tag was Identifyed before [Flag]
  125.         flag = 1;
  126.  
  127.         // Rest Flags and timer
  128.         flag3min = 0;
  129.         count = 0;
  130.         lastTime2 = millis();
  131.       }
  132.  
  133.       // If tag not found (OR) Distance more than 200cm
  134.       else {
  135.         // If Tag not Received by the reader for 2,5 Seconds (AND) Tag was Identifyed before
  136.         if (count > 50 && flag == 1) {
  137.  
  138.           // If Tag is removed from Reader for more than 2,5 Seconds Show message ONE time
  139.           if (flag3min == 0) {
  140.             Serial.println("3 min Timer Started !!");
  141.  
  142.             // Activate Show Timer Started Message [Flag]
  143.             flag3min = 1;
  144.           }
  145.  
  146.           // If the current time after the Timer is activated is more than 3 Minutes Do Commands
  147.           currentTime = millis();
  148.           if (currentTime - lastTime2 > 180000) { // 180000 = 180,000 milliSecond = 180 Seconds = 3 Minutes
  149.  
  150.             // Close Servo Gate
  151.             servo1.write(0);
  152.  
  153.             // Rest Flags and timer
  154.             count = 0;
  155.             flag = 0;
  156.             flag3min = 0;
  157.             lastTime2 = millis();
  158.           }
  159.         }
  160.  
  161.         // Increase the count every 50 ms
  162.         count++;
  163.  
  164.       }
  165.     }
  166.  
  167.     // Rest Variable and timer
  168.     inputString = "";
  169.     lastCharArrivalTime = 0;
  170.  
  171.   }
  172.   //////////////////////////////////////////////////////////////////////////////////////////////
  173. }
  174.  
  175. void ultra() {
  176.   digitalWrite(trigPin, LOW);
  177.   delayMicroseconds(2);
  178.   digitalWrite(trigPin, HIGH);
  179.   delayMicroseconds(10);
  180.   digitalWrite(trigPin, LOW);
  181.   duration = pulseIn(echoPin, HIGH);
  182.   distance = duration * 0.034 / 2;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement