///////// DETECTEUR DE MOUVEMENT SIGNAL //////////////////// //INITIALISATION POUR LE CALIBRAGE // calibrage du capteur (10-60 secs according to the datasheet) int calibrationTime = 20; //the time when the sensor outputs a low impulse long unsigned int lowIn; //temps (millisecondes) durant lequel le signal est faible, soit avant qu'on suppose que tout mouvement s'est arrêté. //on considère cela comme une pause d'insécurité sur la question: mouvement présent oui ou non? long unsigned int pause = 5000; boolean lockLow = true; boolean takeLowTime; int pirPin = 3; //la PIN du capteur de mouvement en 3 //SETUP/INITIALISATION void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); //pinMode(ledPin, OUTPUT); digitalWrite(pirPin, LOW); //MOMENT POUR LE CALIBRAGE Serial.print("calibrating sensor "); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); } //////////////////////////// //LOOP void loop(){ int i=0; while (i<5){ //Part 1 for each detection if(digitalRead(pirPin) == HIGH){ //Serial.println("ok "); i++; //makes sure we wait for a transition to LOW before any further output is made : if (lockLow){ lockLow=false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); delay(1000); } takeLowTime=true; } //Part 2 when there is no detection if(digitalRead(pirPin)==LOW){ if (takeLowTime){ lowIn=millis(); takeLowTime=false; } if(!lockLow && millis()-lowIn > pause){ lockLow=true; Serial.print("motion ended at "); Serial.print((millis()-pause)/1000); Serial.println(" sec"); delay(1000); } } } if(i==5){ Serial.println("Sommeil agité !!!"); delay(1000); i=0; } }