obarlas

Arduino US

Jan 14th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <NewPing.h>
  2.  
  3. /*
  4.  * bunun çalışması için NewPing diye bir kütüphane var
  5.  * bunu Belgelerim/Arduino/libraries altına koyup
  6.  * Arduino'yu kapatıp açman lazım.
  7.  */
  8.  
  9. #define TRIGGER_PIN  9  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  10. #define ECHO_PIN     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
  11. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  12.  
  13. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  14.  
  15. void setup() {
  16.   Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  17. }
  18.  
  19. void loop() {
  20.   /*
  21.    * Wait 50ms between pings (about 20 pings/sec).
  22.    * 29ms should be the shortest delay between pings.
  23.    */
  24.   delay(50);
  25.   /*
  26.    * Send ping, get ping time in microseconds (uS).
  27.    */
  28.   unsigned int uS = sonar.ping();
  29.   /*
  30.    * Convert ping time to distance and print result
  31.    * (0 = outside set distance range, no ping echo)
  32.    */
  33.   Serial.println(uS / US_ROUNDTRIP_CM);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment