Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledPin = 6; // The pin of your LED
- const int trigPin = 5; // The pin to which the TRIG is connected
- const int echoPin = 4; // The pin to which the ECHO is connected
- const int ledOnTime = 1000; // The time that the LED stays on, after detecting the motion (in milliseconds, 1000ms = 1s)
- const int trigDistance = 20; // The distance (and lower than it) at which the sensor is triggered (in centimeters)
- int duration;
- int distance;
- void setup() {
- pinMode(ledPin, OUTPUT);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- }
- void loop() {
- digitalWrite(trigPin, LOW);
- digitalWrite(trigPin, HIGH);
- delay(1);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = duration * 0.034 / 2;
- if (distance <= trigDistance) {
- digitalWrite(ledPin, HIGH);
- delay(ledOnTime);
- digitalWrite(ledPin, LOW);
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement