Blame view

progArduino/poubelle_tests/test1/test1.ino 2.03 KB
38e849ce   csaad   ajout dossier pro...
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  ///////// 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;
        }
  }