Advertisement
lameski

Untitled

Apr 7th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. /*
  2. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3. *
  4. * Crated by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. // defines pins numbers
  9. const int trigPin = 9;
  10. const int echoPin = 10;
  11. // defines variables
  12. long duration;
  13. int distance;
  14. void setup() {
  15. pinMode(6,OUTPUT);
  16. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  17. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  18. Serial.begin(9600); // Starts the serial communication
  19. }
  20. void loop() {
  21. // Clears the trigPin
  22. digitalWrite(trigPin, LOW);
  23. delayMicroseconds(2);
  24. // Sets the trigPin on HIGH state for 10 micro seconds
  25. digitalWrite(trigPin, HIGH);
  26. delayMicroseconds(10);
  27. digitalWrite(trigPin, LOW);
  28. // Reads the echoPin, returns the sound wave travel time in microseconds
  29. duration = pulseIn(echoPin, HIGH);
  30. // Calculating the distance
  31. distance= duration*0.034/2;
  32. // Prints the distance on the Serial Monitor
  33. Serial.print("Distance: ");
  34. Serial.println(distance);
  35.  
  36. if(distance<20){
  37. digitalWrite(6,HIGH);
  38. }
  39. else
  40. {
  41. digitalWrite(6,LOW);
  42. }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement