Arduino教程(4)模拟输入和监控移动物体警报系统


若看不清可至http://www.guanlantech.com/2014/05/arduino-tutorial4/查看

//Program by Jeremy Blum
//www.jeremyblum.com 当室内昏暗并有人走动时点亮LED灯
//Turn on an LED if a room is dim, and motion is detected

//Define Pins
int motionPin = 0; 距...详情
若看不清可至http://www.guanlantech.com/2014/05/arduino-tutorial4/查看

//Program by Jeremy Blum
//www.jeremyblum.com 当室内昏暗并有人走动时点亮LED灯
//Turn on an LED if a room is dim, and motion is detected

//Define Pins
int motionPin = 0; 距离感应器接在引脚0
int lightPin = 1; 光敏电阻接在引脚1
int ledPin = 9;

//Distance Variables
int lastDist = 0; 上次距离
int currentDist = 0; 当前距离

//Threshold for Movement
int thresh = 200; Threshold含义是门槛,阈值

void setup()
{
//The LED pin needs to be set as an output
pinMode(ledPin, OUTPUT);
}

void loop()
{
// read the sensor
int lightVal = analogRead(lightPin); lightVal第一次使用,所以要定义类型,此处为int
currentDist = analogRead(motionPin);

//Does the current distance deviate from the last distance by more than the threshold?
if ((currentDist > lastDist + thresh || currentDist < lastDist - thresh) && lightVal < 800)
{ 如果室内光线值小于800,并且当前距离和上次距离差距大于阈值
digitalWrite(ledPin, HIGH); ||为或者意思,表明2个条件

delay(1000);                 只要满足其一

}
else
{
digitalWrite(ledPin, LOW);
}
lastDist = currentDist; 将当前距离值赋予上次距离
}隐藏

标签: arduino视频