Advertisement
Guest User

Untitled

a guest
Mar 26th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. SoftwareSerial mySerial(10, 11); // Define SoftwareSerial pin RX, TX
  4.  
  5. int gsmDriverPin[3] = {3,4,5}; // The default digital driver pins for the GSM and GPS mode
  6. //If you want to change the digital driver pins
  7. //or you have a conflict with D3~D5 on Arduino board,
  8. //you can remove the J10~J12 jumpers to reconnect other driver pins for the module!
  9. int ledpin = 8;
  10. char inchar;
  11. int c, rfidArray[10];
  12. int res = 0;
  13. int passwordArray[10] = {50,53,49,49,56,52,49,50,13,10}; //Store the password elements here
  14. int ledPin = 12;
  15.  
  16.  
  17. void InitGsm()
  18. {
  19.  
  20. for(int i = 0 ; i < 3; i++){
  21. pinMode(gsmDriverPin[i],OUTPUT);
  22. }
  23.  
  24. pinMode(ledpin,OUTPUT);
  25. digitalWrite(5,HIGH); //Output GSM Timing
  26. delay(1500);
  27. digitalWrite(5,LOW);
  28. digitalWrite(3,LOW); //Enable the GSM mode
  29. digitalWrite(4,HIGH); //Disable the GPS mode
  30. delay(5000); //call ready
  31. delay(5000);
  32. mySerial.println("AT+CMGD=1,4"); //Delete all SMS in box
  33.  
  34. }
  35.  
  36.  
  37. void setup()
  38. {
  39.  
  40. Serial.begin(9600);
  41. mySerial.begin(9600); //set SoftwareSerial port baud rate
  42. InitGsm(); //initialize GSM module
  43. digitalWrite(7,LOW);
  44.  
  45. pinMode(7,OUTPUT);
  46. pinMode(ledPin, OUTPUT);
  47. pinMode(13,OUTPUT);
  48. pinMode(2,OUTPUT);//attach pin 2 of Ultrasonic Sensor to vcc
  49. pinMode(5,OUTPUT);//attach pin 5 of Ultrasonic Sensor to GND
  50.  
  51. /*--- RFID PART BEGINS ---*/
  52. while(res==0){
  53. for(int i = 0; i< 10; i++){
  54. while(!(Serial.available()));// Start collecting the RFID information
  55. c = Serial.read();
  56. rfidArray[i] = c;
  57. //Serial.println(rfidArray[i]);
  58. // Wait for next character to arrive at Serial
  59. }
  60.  
  61. // So now we have all the data from the RFID card.
  62. // Let's compare with passwordArray
  63.  
  64. for(int i = 0; i< 10; i++){
  65. if(rfidArray[i]!=passwordArray[i]){
  66. res = 0;
  67. break;
  68. }
  69. else
  70. res = 1;
  71. }
  72.  
  73. // Let's check res, and light up the green LED & run Motor if the right card was swiped
  74.  
  75. if(res == 0)
  76. {
  77. digitalWrite(ledPin, LOW);
  78. }
  79.  
  80. else
  81. {
  82. digitalWrite(ledPin, HIGH);
  83. delay(3000);
  84. digitalWrite(ledPin, LOW);
  85. }
  86. }
  87. /*--- If wrong RFID, go back, else come out and start checking Ultrasonic ---*/
  88. }
  89.  
  90. void loop() // run over and over
  91. {
  92. /*--BEGIN ULTRASONIC Code--*/
  93. digitalWrite(2, HIGH);
  94. // establish variables for duration of the ping,
  95. // and the distance result in inches and centimeters:
  96. long duration, inches, cm;
  97.  
  98. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  99. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  100. pinMode(3, OUTPUT);// attach pin 3 to Trig
  101. digitalWrite(3, LOW);
  102. delayMicroseconds(2);
  103. digitalWrite(3, HIGH);
  104. delayMicroseconds(5);
  105. digitalWrite(3, LOW);
  106.  
  107. // The same pin is used to read the signal from the PING))): a HIGH
  108. // pulse whose duration is the time (in microseconds) from the sending
  109. // of the ping to the reception of its echo off of an object.
  110. pinMode (4, INPUT);//attach pin 4 to Echo
  111. duration = pulseIn(4, HIGH);
  112.  
  113. // convert the time into a distance
  114. inches = microsecondsToInches(duration);
  115. cm = microsecondsToCentimeters(duration);
  116.  
  117. if(inches < 4)
  118. digitalWrite(13,HIGH);
  119. else
  120. digitalWrite(13,LOW);
  121. /*---- end of ultrasonic code -----*/
  122. /*---- GSM Part ----*/
  123. if(mySerial.available()>0)
  124. {
  125. digitalWrite(7,HIGH);
  126. inchar=mySerial.read();
  127. //Serial.print(inchar); //Display the GPS/GPRS/GSM Module status
  128. if(inchar=='T')
  129. {
  130. delay(10);
  131. inchar=mySerial.read();
  132. if (inchar=='I') //When the GSM module get the message, it will display the sign '+CMTI "SM", 1' in the SoftwareSerial port
  133. {
  134. delay(10);
  135. mySerial.println("AT+CMGR=1"); //When Arduino read the sign, send the "read" AT command to the module
  136. delay(10);
  137. }
  138. }
  139. else if (inchar=='A')
  140. {
  141. delay(10);
  142. inchar=mySerial.read();
  143. if (inchar=='C') //Thw SMS("LH") was display in the SoftwareSerial port, and Arduino has recognize it.
  144. {
  145. delay(10);
  146. digitalWrite(ledpin,HIGH); //Turn on led
  147. delay(50);
  148. mySerial.println("AT+CMGD=1,4"); //Delete all message
  149. delay(500);
  150. }
  151. if (inchar=='O') //Thw SMS("LH") was display in the SoftwareSerial port, and Arduino has recognize it.
  152. {
  153. delay(10);
  154. digitalWrite(ledpin,LOW); //Turn off led
  155. delay(50);
  156. mySerial.println("AT+CMGD=1,4");//Delete all message
  157. delay(500);
  158. }
  159. }
  160. }
  161. }
  162.  
  163. long microsecondsToInches(long microseconds)
  164. {
  165. // According to Parallax's datasheet for the PING))), there are
  166. // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  167. // second). This gives the distance travelled by the ping, outbound
  168. // and return, so we divide by 2 to get the distance of the obstacle.
  169. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  170. return microseconds / 74 / 2;
  171. }
  172.  
  173. long microsecondsToCentimeters(long microseconds)
  174. {
  175. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  176. // The ping travels out and back, so to find the distance of the
  177. // object we take half of the distance travelled.
  178. return microseconds / 29 / 2;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement