Advertisement
Guest User

Untitled

a guest
May 18th, 2014
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. /* HC-SR04 Sensor
  2. https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
  3.  
  4.  
  5. This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
  6.  
  7. distance to the closest object in range. To do this, it sends a pulse
  8.  
  9. to the sensor to initiate a reading, then listens for a pulse
  10. to return. The length of the returning pulse is proportional to
  11.  
  12. the distance of the object from the sensor.
  13.  
  14.  
  15. The circuit:
  16. * VCC connection of the sensor attached to +5V
  17.  
  18. * GND connection of the sensor attached to ground
  19. * TRIG connection of the sensor attached to digital pin 2
  20.  
  21. * ECHO connection of the sensor attached to digital pin 4
  22.  
  23.  
  24.  
  25. Original code for Ping))) example was created by David A. Mellis
  26.  
  27. Adapted for HC-SR04 by Tautvidas Sipavicius
  28.  
  29.  
  30. This example code is in the public domain.
  31. */
  32.  
  33.  
  34.  
  35. const int trigPin = 2;
  36. const int speakerPin = 13;
  37. const int echoPin = 4;
  38.  
  39.  
  40. void setup() {
  41.  
  42. // initialize serial communication:
  43. Serial.begin(9600);
  44. //pinMode(led, OUTPUT);
  45. pinMode(speakerPin,OUTPUT);
  46.  
  47. }
  48.  
  49. void loop()
  50.  
  51. {
  52. // establish variables for duration of the ping,
  53.  
  54. // and the distance result in inches and centimeters:
  55. long duration, inches, cm;
  56.  
  57.  
  58. // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  59.  
  60. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  61.  
  62. pinMode(trigPin, OUTPUT);
  63.  
  64. digitalWrite(trigPin, LOW);
  65.  
  66. delayMicroseconds(2);
  67.  
  68. digitalWrite(trigPin, HIGH);
  69.  
  70. delayMicroseconds(10);
  71.  
  72. digitalWrite(trigPin, LOW);
  73.  
  74.  
  75. // Read the signal from the sensor: a HIGH pulse whose
  76.  
  77. // duration is the time (in microseconds) from the sending
  78. // of the ping to the reception of its echo off of an object.
  79.  
  80. pinMode(echoPin, INPUT);
  81.  
  82. duration = pulseIn(echoPin, HIGH);
  83.  
  84.  
  85. // convert the time into a distance
  86.  
  87. // inches = microsecondsToInches(duration);
  88.  
  89. cm = microsecondsToCentimeters(duration);
  90.  
  91.  
  92. // Serial.print(inches);
  93.  
  94. // Serial.print("in, ");
  95.  
  96.  
  97.  
  98. Serial.print(cm);
  99.  
  100. Serial.print("cm");
  101.  
  102. Serial.println();
  103.  
  104. if (cm < 110) { // This is where the LED On/Off happens
  105. // digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  106. tone(speakerPin, 800);
  107. delay(10000);
  108. // digitalWrite(led2,LOW);
  109. }
  110. else {
  111. // digitalWrite(led,LOW);
  112. // digitalWrite(led2,HIGH);
  113. noTone(speakerPin);
  114. }
  115. if (cm >= 200 || cm <= 0){
  116. Serial.println("Out of range");
  117. }
  118.  
  119.  
  120. delay(100);
  121.  
  122. }
  123.  
  124. long microsecondsToInches(long microseconds)
  125.  
  126. {
  127. // According to Parallax's datasheet for the PING))), there are
  128.  
  129. // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  130.  
  131. // second). This gives the distance travelled by the ping, outbound
  132. // and return, so we divide by 2 to get the distance of the obstacle.
  133.  
  134. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  135.  
  136. return microseconds / 74 / 2;
  137.  
  138. }
  139.  
  140. long microsecondsToCentimeters(long microseconds)
  141.  
  142. {
  143. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  144.  
  145. // The ping travels out and back, so to find the distance of the
  146.  
  147. // object we take half of the distance travelled.
  148. return microseconds / 29 / 2;
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement