1、回顾
与门:&
或门:|
非门:!
2.有人靠近
- 单词
.
|word | meaning
|--|--|
|sensor|传感器
|voltage|电压
|current|电流
图中那个长得跟碟片一样的玩意就是人体热释电传感器。
代码:检测到人就报警
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,LOW);
while(!digitalRead(3));
tone(12,233,10);
digitalWrite(13,HIGH);
}
在上面代码基础上,增加一个听到声音就报警的功能:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,LOW);
while(!digitalRead(3)&&!digitalRead(2));
tone(12,233,5000);
digitalWrite(13,HIGH);
delay(5000);
digitalWrite(13,LOW);
}
再将LED灯换成呼吸灯:
int red=10,green=9,blue=6;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
analogWrite(10,255);
analogWrite(9,255);
analogWrite(6,255);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,LOW);
while(!digitalRead(3)&&!digitalRead(2));
tone(12,233,1000);
//digitalWrite(13,HIGH);
for(int i=255;i>=0;i--) {analogWrite(red,i);delay(2);}
analogWrite(red,255);
for(int i=255;i>=0;i--) {analogWrite(green,i);delay(2);}
analogWrite(green,255);
for(int i=255;i>=0;i--) {analogWrite(blue,i);delay(2);}
analogWrite(blue,255);
//digitalWrite(13,LOW);
}
报警器代码:
把呼吸灯换回LED灯
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,LOW);
while(!digitalRead(3)&&!digitalRead(2));
for(int i=100;i<=300;i++) tone(12,i,10);
tone(12,300,2000);
for(int i=0;i<20;i++)
{
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
}
for(int i=300;i>=100;i++) tone(12,i,10);
tone(12,100,2000);
for(int i=0;i<20;i++)
{
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
}
}