///////// DETECTEUR DE MOUVEMENT SIGNAL //////////////////// //INITIALISATION POUR LE CALIBRAGE // calibrage du capteur (10-60 secs according to the datasheet) int calibrationTime = 20; // le temps quand le capteur envoie un signal faible en entrée long unsigned int timelowIn; //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 int ledPin = 13; //Une led modélise le signal "trop de mouv" dans un premier temps //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; if(digitalRead(pirPin) == HIGH){i++;} //On pose comme condition que si il y a 5 fois du mouvement if (i>5){ Serial.println ("Sommeil agité !!!"); i=0; if(lockLow){ //makes sure we wait for a transition to LOW before any further output is made: lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); } } if(digitalRead(pirPin) == LOW){ digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state if(takeLowTime){ timelowIn = millis(); //save the time of the transition from high to LOW takeLowTime = false; //make sure this is only done at the start of a LOW phase } //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if(!lockLow && millis() - timelowIn > pause){ //makes sure this block of code is only executed again after //a new motion sequence has been detected lockLow = true; Serial.print("motion ended at "); //output Serial.print((millis() - pause)/1000); Serial.println(" sec"); delay(50); } } }