接下來是程式碼的部分
const int ledPin = 13;
unsigned long ledOpenTime = 0;
int tipPoint = 800;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0);
unsigned long currentTime = millis();
if(sensorValue > tipPoint){
digitalWrite(ledPin, HIGH);
ledOpenTime = currentTime;
Serial.println("There are people in here.");
}
else{
if((ledOpenTime != 0) && ((currentTime - ledOpenTime) > 1000)){
ledOpenTime = 0;
digitalWrite(ledPin, LOW);
}
}
}
這個程式很簡單,就是從A0讀入電壓,電壓小於tipPoint的話表示沒人,如果電壓大於tipPoint則表示偵測到人。tipPoint的值根據你所在的環境而不同(或是你要求的敏感度,tipPoint越低則越敏感)。
偵測到人的話,A13將會是高電位,也會送個訊號到serial。
沒有留言:
張貼留言