Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <string.h>
  3.  
  4. int bluetoothTx = 3; // TX-O pin of bluetooth mate, Arduino D2
  5. int bluetoothRx = 2; // RX-I pin of bluetooth mate, Arduino D3
  6. SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
  7. int connd = 0;
  8. int enable = 1;
  9.  
  10. void setup()
  11. {
  12. Serial.begin(9600); // Begin the serial monitor at 9600bps
  13. char inChar;
  14. bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
  15. delay(100); // Short delay, wait for the Mate to send back CMD
  16. bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
  17. bluetooth.begin(9600); // Start bluetooth serial at 9600
  18. /* bluetooth.print("AT+ROLE0");
  19. delay(50);
  20. bluetooth.print("AT+MODE1");
  21. delay(50);
  22. bluetooth.print("AT+TYPE0");
  23. delay(50);
  24. bluetooth.print("AT+NOTI1");
  25. delay(50);
  26. bluetooth.print("AT+PASS123456");
  27. delay(50);
  28. bluetooth.print("AT+FILT0");
  29. delay(50);
  30. bluetooth.print("AT+ERASE");
  31. delay(50);
  32. bluetooth.print("AT+CLEAR");
  33. delay(50);
  34. bluetooth.print("AT+POWE3");
  35. delay(50);
  36. bluetooth.print("AT+NAMELOCK");
  37. delay(50);
  38. bluetooth.print("AT+RESET"); */
  39. delay(300);
  40. bluetooth.print("AT+START");
  41. delay(100);
  42. int i = 0;
  43. String inString = String("");
  44.  
  45. while (bluetooth.available()) {
  46. inString += (char)bluetooth.read();
  47. }
  48. //Serial.print(inString);
  49. Serial.print(" Setup Complete. ");
  50. }
  51.  
  52. void pollDist(){
  53. delay(3000);
  54. String distance;
  55. int startRead;
  56. char inChar;
  57. String inString = String("");
  58. while(connd){
  59. delay(2000);
  60. if (readyTs){
  61. bluetooth.print("AT+RSSI?");
  62. }
  63. distance = String("");
  64. inString = String("");
  65. while (bluetooth.available()){
  66. inChar = (char)bluetooth.read();
  67. inString += inChar;
  68. if (inChar == '-'){
  69. startRead = 1;
  70. distance = String("");
  71. }
  72. else if(startRead == 1 && inChar != 'O') {
  73. distance += inChar;
  74. }
  75. else if (startRead ==1 && inChar == 'O') {
  76. startRead = 0;
  77. }
  78. }
  79.  
  80. int intDistance = 0;
  81. intDistance = String(distance).toInt();
  82. if (inString.indexOf("OK+LOST") != -1) {
  83. connd = 0;
  84. Serial.println(" connection lost ");
  85. return;
  86. }
  87.  
  88.  
  89. if (inString.indexOf("connectionStatus.") != -1) {
  90. bluetooth.print("good.");
  91. }
  92.  
  93.  
  94. if (inString.indexOf("!disable.") != -1) {
  95. enable = 0;
  96. Serial.println("disable pressed");
  97. }
  98. if (inString.indexOf("!enable.") != -1) {
  99. enable = 1;
  100. Serial.println("enable pressed");
  101. }
  102.  
  103. if (intDistance < 45 && enable && intDistance > 10) {
  104. Serial.println(" distance " + distance + "Access Granted. ");
  105. }
  106. if (intDistance > 65) {
  107. Serial.println(" distance " + distance + "DOOR LOCKED BRO.");
  108. }
  109. else if (!enable) {
  110. Serial.println("yo junk has been disabled");
  111. }
  112. }
  113. }
  114.  
  115.  
  116.  
  117. void loop()
  118. {
  119. char inChar;
  120. String inString;
  121. while (bluetooth.available()) {
  122. delay(50);
  123. inChar = (char)bluetooth.read();
  124.  
  125. if (inChar == '+' || inChar == '!'){
  126. inString = String("");
  127. }
  128. else {
  129. inString += inChar;
  130.  
  131. }
  132. if (inString == "CONN") {
  133. Serial.print(" Connected Now ");
  134. connd = 1;
  135. pollDist();
  136.  
  137. }
  138. if (inString == "LOST") {
  139. connd = 0;
  140. }
  141. if (inString == "connectionStatus.") {
  142. bluetooth.print("good.");
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement