Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. // defines pins numbers
  2.  
  3. #define trigPin 12
  4. #define echoPin 13
  5. #define ledPin 14
  6.  
  7. // defines variables
  8. long duration;
  9. int distance;
  10.  
  11. boolean triggered = false;
  12.  
  13. double activateDistance = 10;
  14.  
  15. void setup() {
  16. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  17. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  18. pinMode(ledPin, OUTPUT);
  19.  
  20. Serial.begin(9600); // Starts the serial communication
  21.  
  22. }
  23.  
  24. void loop()
  25. {
  26. // Clears the trigPin
  27. digitalWrite(trigPin, LOW);
  28. delayMicroseconds(2);
  29.  
  30. // Sets the trigPin on HIGH state for 10 micro seconds
  31. digitalWrite(trigPin, LOW);
  32. delayMicroseconds(5);
  33. digitalWrite(trigPin, HIGH);
  34. delayMicroseconds(10);
  35. digitalWrite(trigPin, LOW);
  36.  
  37.  
  38.  
  39. // Reads the echoPin, returns the sound wave travel time in microseconds
  40. duration = pulseIn(echoPin, HIGH);
  41.  
  42. // Calculating the distance
  43. distance = duration * 0.034 / 2;
  44.  
  45. // Prints the distance on the Serial Monitor
  46. if (distance <= activateDistance)
  47. {
  48. digitalWrite(ledPin, HIGH);
  49. Serial.print("GOT");
  50. delay(1000);
  51. digitalWrite(ledPin,LOW);
  52. }else{
  53. digitalWrite(ledPin,LOW);
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement