Advertisement
Guest User

Parallax Ping)) with arduino darylrobotproject.wordpress.com

a guest
Dec 11th, 2012
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. int PingPin = A3;
  2. int BaudRate = 9600;
  3.  
  4. void setup() {  
  5.  
  6.   Serial.begin(BaudRate);
  7.  
  8. }  
  9.  
  10. void loop() {  
  11.  
  12.   pinMode(PingPin, OUTPUT);  
  13.   digitalWrite(PingPin, LOW);  // init sensor to ensure clean HIGH pulse  
  14.   delayMicroseconds(2);    
  15.   digitalWrite(PingPin, HIGH);  // make the sensor send a pulse    
  16.   delayMicroseconds(5);    
  17.   digitalWrite(PingPin, LOW);  // Set LOW again  
  18.   pinMode(PingPin, INPUT);  // Get ready to capture the duration of the resulting pulse  
  19.   // Capture how long the pin stays in HIGH state.  
  20.   unsigned long Duration = pulseIn(PingPin, HIGH);
  21.  
  22.   if (Duration == 0) {
  23.     Serial.println("No Pulse received from the sensor");  
  24.   } else  {    
  25.     Serial.print("Distance : ");    
  26.     Serial.print(Convert_Time_Space(Duration));  // convert the duration into distance    
  27.     Serial.println(" cm");    
  28.   }  
  29.   //delay (1000) ;  
  30. }  
  31.  
  32. unsigned long Convert_Time_Space(const unsigned long fnDuration ) {  
  33.   // This function could be more precise by using floats  
  34.   // and taking into account temperature and humidity  
  35.   // I used 29 microseconds per cm.    
  36.   return fnDuration / 29 / 2 ;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement