2017-04-06 54 views
0

我目前有一个带有wifi屏蔽的arduino,连接到一个PIR探测器和一个网站。这应该控制一个锁。我已经完成了所有这些工作,但有一部分我想改进。我可以改变PIR探测器吗?如果过去有移动,例如5秒钟,它将允许它打开(如果字母也是正确的)?它现在的做法是,它必须不断移动,否则它会在几秒钟内锁定。请注意,我是新来的Arduino编码,这样下去容易对我试图让我的一块arduino代码基于时间

while (client.available()){ 
int sensorValue2 = analogRead(A1); //Input from PIR-detector 
Serial.println(sensorValue2); 
char c = client.read(); 
Serial.write(c); 
if ((sensorValue2 > 0.0) && (c == 'L')) { //If the input from the detector is over 0.0, and the text which is received from the website is 'L', the lock will open 
    digitalWrite(5, LOW); 
} 
else{ 
    digitalWrite(5, HIGH); 
} 

}

+0

看看“没有延迟的瞬间”的例子,你需要的东西就像它 – dandavis

回答

1

可以使用millis()功能withou暂停你的Arduino使用计时器。 This Tutorial应该帮助你做到这一点。

0

这是你如何使用millis()

假设previousMilis是在该运动是第一触发或任何你想要的时间。

if (triggered(0) { 
    previousMillis() = millis(); 
} 

while (millis() - previousMillis < 5000) { 
    // do something while the 5 seconds is being counted down 
    // this will update each milliseconds 
}