Guest User

my_code

a guest
Apr 11th, 2021
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #define echoPin 49 // attach pin D2 Arduino to pin Echo of HC-SR04
  2. #define trigPin 51 //attach pin D3 Arduino to pin Trig of HC-SR04
  3.  
  4. // defines variables
  5. long duration; // variable for the duration of sound wave travel
  6. float distance; // variable for the distance measurement
  7.  
  8.  
  9. void setup() {
  10. pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  11. pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  12. pinMode(53, OUTPUT);
  13. pinMode(47, OUTPUT);
  14. Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  15.  
  16. digitalWrite(53, HIGH); //these pins are used to provide power to the sensor.
  17. digitalWrite(47, LOW);
  18.  
  19. }
  20. void loop() {
  21. Serial.print("Distance: ");
  22. Serial.print(GetDistance());
  23. Serial.println(" cm");
  24. }
  25.  
  26. float GetDistance(){
  27. digitalWrite(trigPin, LOW);
  28. delayMicroseconds(2);
  29. digitalWrite(trigPin, HIGH);
  30. delayMicroseconds(10);
  31. digitalWrite(trigPin, LOW);
  32. duration = pulseIn(echoPin, HIGH);
  33. distance = duration * 0.034 / 2;
  34. return distance;
  35. }
  36.  
Add Comment
Please, Sign In to add comment