Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Ans)
Measuring distance with ultrasonic sensor :
COMPONENTS AND SUPPLIES :
1. Ultrasonic Sensor HC - SR04 - 1
2. Juper wires - 3
3. Ardiuno UNO - 1
This is the code for the project Ardiuno :
CODE :
1. /* 2. Ping))) Sensor 3. 4. This sketch reads a PING))) ultrasonic rangefinder and returns the distance 5. to the closest object in range. To do this, it sends a pulse to the sensor to 6. initiate a reading, then listens for a pulse to return. The length of the 7. returning pulse is proportional to the distance of the object from the sensor. 8. The circuit: 9. - +V connection of the PING))) attached to +5V 10. - GND connection of the PING))) attached to ground 11. - SIG connection of the PING))) attached to digital pin 7 12. 13. created 3 Nov 2008 14. by David A. Mellis 15. modified 30 Aug 2011 16. by Tom Igoe 17. 18. This example code is in the public domain. 19. 20. http://www.arduino.cc/en/Tutorial/Ping 21. */ 22. 23. // this constant won't change. It's the pin number of the sensor's output: 24. const int pingPin = 7; 25. 26. void setup() { 27. // initialize serial communication: 28. Serial.begin(9600); 29. } 30. 31. void loop() { 32. // establish variables for duration of the ping, and the distance result 33. // in inches and centimeters: 34. long duration, inches, cm; 35. 36. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. 37. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: 38. pinMode(pingPin, OUTPUT); 39. digitalWrite(pingPin, LOW); 40. delayMicroseconds(2); 41. digitalWrite(pingPin, HIGH); 42. delayMicroseconds(5); 43. digitalWrite(pingPin, LOW); 44. 45. // The same pin is used to read the signal from the PING))): a HIGH pulse 46. // whose duration is the time (in microseconds) from the sending of the ping 47. // to the reception of its echo off of an object. 48. pinMode(pingPin, INPUT); 49. duration = pulseIn(pingPin, HIGH); 50. 51. // convert the time into a distance 52. inches = microsecondsToInches(duration); 53. cm = microsecondsToCentimeters(duration); 54. 55. Serial.print(inches); 56. Serial.print("in, "); 57. Serial.print(cm); 58. Serial.print("cm"); 59. Serial.println(); 60. 61. delay(100); 62. } 63. 64. long microsecondsToInches(long microseconds) { 65. // According to Parallax's datasheet for the PING))), there are 73.746 66. // microseconds per inch (i.e. sound travels at 1130 feet per second). 67. // This gives the distance travelled by the ping, outbound and return, 68. // so we divide by 2 to get the distance of the obstacle. 69. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf 70. return microseconds / 74 / 2; 71. } 72. 73. long microsecondsToCentimeters(long microseconds) { 74. // The speed of sound is 340 m/s or 29 microseconds per centimeter. 75. // The ping travels out and back, so to find the distance of the object we 76. // take half of the distance travelled. 77. return microseconds / 29 / 2; 78. }
