Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
3,387
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int ledPin = 6; // The pin of your LED
  2. const int trigPin = 5; // The pin to which the TRIG is connected
  3. const int echoPin = 4; // The pin to which the ECHO is connected
  4. const int ledOnTime = 1000; // The time that the LED stays on, after detecting the motion (in milliseconds, 1000ms = 1s)
  5. const int trigDistance = 20; // The distance (and lower than it) at which the sensor is triggered (in centimeters)
  6.  
  7. int duration;
  8. int distance;
  9.  
  10. void setup() {
  11.   pinMode(ledPin, OUTPUT);
  12.   pinMode(trigPin, OUTPUT);
  13.   pinMode(echoPin, INPUT);
  14. }
  15.  
  16. void loop() {
  17.   digitalWrite(trigPin, LOW);
  18.   digitalWrite(trigPin, HIGH);
  19.   delay(1);
  20.   digitalWrite(trigPin, LOW);
  21.   duration = pulseIn(echoPin, HIGH);
  22.   distance = duration * 0.034 / 2;
  23.  
  24.   if (distance <= trigDistance) {
  25.     digitalWrite(ledPin, HIGH);
  26.     delay(ledOnTime);
  27.     digitalWrite(ledPin, LOW);
  28.   }
  29.   delay(100);
  30. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement