Advertisement
RifatBinIslam

DE Project 191-15-12611

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