-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion-sensor.ino
46 lines (43 loc) · 955 Bytes
/
motion-sensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int LED = 13;
int ECHOPIN = 2;
int TRIGPIN = 3;
int OUT = 4;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(OUT, OUTPUT);
Serial.begin(9600);
}
void loop()
{
checkSonic();
}
void checkSonic()
{
// Set the trigger pin to low for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
// Send a 10uS high to trigger ranging
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
// Send pin low again
digitalWrite(TRIGPIN, LOW);
// Read in times pulse
int distance = pulseIn(ECHOPIN, HIGH);
// Calculate distance from time of pulse
distance = distance / 58;
Serial.println(distance);
if (distance <= 10 && distance > 0)
{
Serial.println("motion detected");
digitalWrite(OUT, 1);
digitalWrite(LED, 1);
} else {
Serial.println("no motion detected");
digitalWrite(OUT, 0);
digitalWrite(LED, 0);
}
delay(200);
}