Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor
  2. const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
  3. const int ledPin = ; // LED Pin
  4. const int buzzPin = ; // Buzzer Pin
  5.  
  6.  
  7. void setup() {
  8. Serial.begin(9600); // Starting Serial Terminal
  9. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  10. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  11. pinMode(ledPin, OUTPUT); //Sets the LED pin as O/P
  12. pinMode(buzzPin, OUTPUT); //Sets the buzzer pin as O/P
  13. }
  14.  
  15. void loop() {
  16. long duration,cm;
  17. digitalWrite(trigPin, LOW);
  18. delayMicroseconds(2);
  19.  
  20. digitalWrite(trigPin, HIGH);
  21. delayMicroseconds(10);
  22. digitalWrite(pingPin, LOW);
  23.  
  24. duration = pulseIn(echoPin, HIGH);
  25. cm = microsecondsToCentimeters(duration);
  26.  
  27. Serial.print(cm);
  28. Serial.print("cm");
  29. Serial.println();
  30. if ( cm < 20.0 )
  31. {
  32. // TURN ON LED
  33. digitalWrite(ledPin, HIGH);
  34. if ( cm < 10.0 )
  35. {
  36. // TURN ON Buzzer
  37. tone(buzzPin, 1000);
  38. delay(250)
  39. }
  40. else
  41. {
  42. // TURN OFF Buzzer
  43. noTone(buzzPin);
  44. delay(250);
  45. }
  46. }
  47. else
  48. {
  49. // TURN OFF
  50. digitalWrite(ledPin, LOW);
  51.  
  52. }
  53. delay(100);
  54. }
  55.  
  56. long microsecondsToCentimeters(long microseconds) {
  57. return microseconds / 29 / 2;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement