Advertisement
Guest User

Untitled

a guest
Aug 12th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <Servo.h>
  3. #include <Wire.h>
  4. SoftwareSerial RFID(2, 3); // RX and TX
  5.  
  6. int data1 = 0;
  7. int ok = -1;
  8. int yes = 13;
  9. int no = 12;
  10. Servo myservo; // create servo object to control a servo
  11. int pos=1; // variable to store the servo position
  12.  
  13. // use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
  14. int tag1[14] = {2,52,48,48,48,56,54,66,49,52,70,51,56,3};
  15. int tag2[14] = {2,52,48,48,48,56,54,67,54,54,66,54,66,3};
  16. int newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
  17.  
  18. void setup()
  19. {
  20. RFID.begin(9600); // start serial to RFID reader
  21. Serial.begin(9600); // start serial to PC
  22. pinMode(yes, OUTPUT); // for status LEDs
  23. pinMode(no, OUTPUT);
  24. }
  25.  
  26. boolean comparetag(int aa[14], int bb[14])
  27. {
  28. boolean ff = false;
  29. int fg = 0;
  30. for (int cc = 0 ; cc < 14 ; cc++)
  31. {
  32. if (aa[cc] == bb[cc])
  33. {
  34. fg++;
  35. }
  36. }
  37. if (fg == 14)
  38. {
  39. ff = true;
  40. }
  41. return ff;
  42. }
  43.  
  44. void checkmytags() // compares each tag against the tag just read
  45. {
  46. ok = 0; // this variable helps decision-making,
  47. // if it is 1 we have a match, zero is a read but no match,
  48. // -1 is no read attempt made
  49. if (comparetag(newtag, tag1) == true)
  50. {
  51. ok++;
  52. }
  53. if (comparetag(newtag, tag2) == true)
  54. {
  55. ok++;
  56. }
  57. }
  58.  
  59. void readTags()
  60. {
  61. ok = -1;
  62.  
  63. if (RFID.available() > 0)
  64. {
  65. // read tag numbers
  66. delay(100); // needed to allow time for the data to come in from the serial buffer.
  67.  
  68. for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
  69. {
  70. data1 = RFID.read();
  71. newtag[z] = data1;
  72. }
  73. RFID.flush(); // stops multiple reads
  74.  
  75. // do the tags match up?
  76. checkmytags();
  77. }
  78.  
  79. // now do something based on tag type
  80. if (ok > 0) // if we had a match
  81. {
  82.  
  83.  
  84. void setup() {
  85.  
  86. }
  87.  
  88. void loop() {
  89. if(pos==1){
  90. myservo.attach(3); // attaches the servo on pin 3 to the servo object "Digital 3"
  91. myservo.writeMicroseconds(2000);
  92. pos=2;
  93. delay(400);
  94. }
  95. if(pos==3){
  96. myservo.attach(3); // attaches the servo on pin 3 to the servo object "Digital 3"
  97. myservo.writeMicroseconds(580);
  98. pos=4;
  99. delay(400);
  100. }
  101. if(pos==2){
  102. myservo.detach();
  103. }
  104. if(pos==4){
  105. myservo.detach();
  106. delay(2000);
  107. }
  108. if(pos!=4){
  109. pos=3;
  110. }
  111. if(pos==4){
  112. pos=1;
  113. }
  114. }
  115. }
  116. ok = -1;
  117. }
  118. else if (ok == 0) // if we didn't have a match
  119. {
  120. Serial.println("Rejected");
  121. digitalWrite(no, HIGH);
  122. delay(1000);
  123. digitalWrite(no, LOW);
  124.  
  125. ok = -1;
  126. }
  127. }
  128.  
  129. void loop()
  130. {
  131. readTags();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement