Advertisement
lllClusterlll

Servo SRF05 Example

Feb 22nd, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #define ECHOPIN 9                            // Pin to receive echo pulse
  2. #define TRIGPIN 8                            // Pin to send trigger pulse
  3.  
  4. void setup()
  5. {
  6.  
  7.   Serial.begin(115200);
  8.   pinMode(ECHOPIN, INPUT);
  9.   pinMode(TRIGPIN, OUTPUT);
  10.  
  11. }
  12.  
  13. void loop()
  14. {
  15.  
  16.   digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  17.   delayMicroseconds(10);
  18.   digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  19.   long distance = pulseInLong(ECHOPIN, HIGH, 30000);        // Read in times pulse
  20.   Serial.print("pulseIn=");
  21.   Serial.print(distance);
  22.   Serial.print("\t");
  23.   distance = distance / 58;                     // Calculate distance from time of pulse
  24.   Serial.print("Distance=");
  25.   Serial.println(distance);
  26.   delay(50);                                    // Wait 50mS before next ranging
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement