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
| //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
int ledPin = 2; // choose the pin for the LED
int pir1 = 3; // choose the input pin (for PIR sensor)
int pir2 = 4;
int pirState = LOW; // we start, assuming no motion detected
int val1 = 0; // variable for reading the pin status
int val2 = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pir1, INPUT); // declare sensor as input
pinMode(pir2, INPUT); // declare sensor as input
Serial.begin(9600);
Serial.print("calibrating sensor 1 ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
}
void loop(){
val1 = digitalRead(pir1); // read input value
val2 = digitalRead(pir2); // read input value
Serial.println(val1);
Serial.println(val2);
if (val1 == HIGH || val2 == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
delay(150);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
delay(300);
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
} |
Partager