Advertisement
Guest User

Garagae Door

a guest
Jun 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. int servoPin = 3;
  4.  
  5.  
  6. #define trigPin 13
  7. #define echoPin 12
  8. #define led 11
  9. #define led2 10
  10.  
  11. Servo Serv;
  12. void setup() {
  13.   Serial.begin (9600);
  14.   pinMode(trigPin, OUTPUT);
  15.   pinMode(echoPin, INPUT);
  16.   pinMode(led, OUTPUT);
  17.   pinMode(led2, OUTPUT);
  18.   Serv.attach(servoPin);
  19.   pinMode(2, OUTPUT);
  20. }
  21.  
  22. void loop() {
  23.   long duration, distance;
  24.   digitalWrite(trigPin, LOW);  // Added this line
  25.   delayMicroseconds(2); // Added this line
  26.   digitalWrite(trigPin, HIGH);
  27. //  delayMicroseconds(1000); - Removed this line
  28.   delayMicroseconds(10); // Added this line
  29.   digitalWrite(trigPin, LOW);
  30.   duration = pulseIn(echoPin, HIGH);
  31.   distance = (duration/2) / 29.1;
  32.   Serial.println(distance);
  33.  
  34.   if(distance < 10){
  35.       digitalWrite(2, HIGH);
  36.       // Make servo go to 0 degrees
  37.   Serv.write(0);
  38.   Serv.write(180);
  39.   delay(1000);  
  40.   }
  41.   else{
  42.    Serv.write(0);
  43.    digitalWrite(2, LOW);
  44.   }
  45.  
  46.  
  47. //  if (distance < 4) {  // This is where the LED On/Off happens
  48. //    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  49. //  digitalWrite(led2,LOW);
  50. //}
  51. //  else {
  52. //    digitalWrite(led,LOW);
  53. //    digitalWrite(led2,HIGH);
  54. //  }
  55. //  if (distance >= 200 || distance <= 0){
  56. //    Serial.println("Out of range");
  57. //  }
  58. //  else {
  59. //    Serial.print(distance);
  60. //    Serial.println(" cm");
  61. //  }
  62. //  delay(500);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement