Advertisement
NugrohoBudiP

Automatic Sprayer

Apr 6th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. //created by Nugroho Budi P
  2. #define trigPin 2
  3. #define echoPin 3
  4. int Switch = 4;  
  5.  
  6.  
  7.  
  8. void setup() {
  9.   Serial.begin (9600);
  10.   pinMode(trigPin, OUTPUT);
  11.   pinMode(echoPin, INPUT);
  12.   pinMode(Switch, OUTPUT);
  13.  
  14.  
  15. }
  16.  
  17. void loop() {
  18.  
  19.  
  20.  
  21.   long duration, distance;
  22.   digitalWrite(trigPin, LOW);
  23.   delayMicroseconds(2);
  24.   digitalWrite(trigPin, HIGH);
  25.   delayMicroseconds(10);
  26.   digitalWrite(trigPin, LOW);
  27.   duration = pulseIn(echoPin, HIGH);
  28.   distance = (duration/2)/70;
  29.  
  30.  
  31.   if (distance <= 20) {
  32.     digitalWrite(Switch, LOW);
  33.     delay(7000);
  34.     digitalWrite(Switch, HIGH);
  35.     delay(5000);
  36. }
  37.   else{
  38.     digitalWrite(Switch,HIGH);
  39.  
  40.   }
  41.   delay(200);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement