-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIR测距报警器
77 lines (65 loc) · 1.56 KB
/
PIR测距报警器
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
int Sensor = 8; //接收传感器的信号
int LED =5;
int buzzer =12;
const int TrigPin = 2; //发送超声波
const int EchoPin = 3; //接收超声波
float cm;
void setup()
{
Serial.begin(115200);
Serial.println("Initialisation complete.");
pinMode(LED,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(Sensor, INPUT); // 设置为输入
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
unsigned char i,j;
int val = digitalRead(Sensor); // 读取引脚电平
if (val==1) // 若检测到有人,则val值为1
{
int sum=0;
digitalWrite(LED,HIGH);
delay(200);
digitalWrite(LED,LOW);
delay(200);
digitalWrite(buzzer,HIGH);
Serial.println("who's there?");
for(int i=0;i<3;i++)
{
// 产生一个10us的高电平脉冲去触发TrigPin
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
// 检测脉冲宽度,并计算出距离
cm = pulseIn(EchoPin, HIGH) / 58.0;
//通过串口监视器输出
sum=sum+cm;
delay(100);
}
Serial.print("Distance:");
Serial.println(sum/3.0);
for(i=0;i<150;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);
digitalWrite(buzzer,LOW);
delay(1);
}
for(j=0;j<200;j++)
{
digitalWrite(buzzer,HIGH);
delay(2);
digitalWrite(buzzer,LOW);
delay(2);}
}
else
{ digitalWrite(LED,LOW);
digitalWrite(buzzer,LOW);
Serial.println("emmmm,sleep");}
delay(100);
}