Advertisement
Guest User

Autodoor

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