Blame view

progArduino/Autofinal/Autofinal.ino 7.48 KB
484d8cc5   csaad   essai commit (inu...
1
  
bb12c4eb   csaad   creation fichier/...
2
3
4
5
6
7
  ////////////////////////////////////////   PROJET SC: VEILLEUSE CONNECTEE   (IMA3)
  //Claire Vandamme
  //Justine Senellart
  //Camille Saâd
  
  
bb12c4eb   csaad   creation fichier/...
8
9
  //--------------------------------------------------------------------------------------------------------------- INITIALISATION CONSTANTES et paramètres
  
bb12c4eb   csaad   creation fichier/...
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
  //INIT CAPTEUR PRESENCE
  
  //temps donné pour calibrer le capteur de présence.(10-60 secs according to the datasheet)
  int calibrationTime = 10;        
  //the time when the sensor outputs a low impulse
  long unsigned int lowIn;         
  //the amount of milliseconds the sensor has to be low 
  //before we assume all motion has stopped
  long unsigned int pause = 500;  
  
  boolean lockLow = true;
  boolean takeLowTime;  
  int pirPin = 3;    //the digital pin connected to the PIR sensor's output
  int ledPin = 13;
  //var meaning the parent receives a message
  int MESSAGE=0;
  
  
  //INIT NEOPIXEL LED RGB
  
  //add the library
  #include <Adafruit_NeoPixel.h>
  //control pin
  # define PININ 12
  # define PINOUT 8
   
  // Parameter 1 = number of pixels in strip
  // Parameter 2 = pin number (most are valid)
  // Parameter 3 = pixel type flags, add together as needed:
  //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PININ, NEO_RGB + NEO_KHZ800);
  
  
  //INIT PHOTRESISTANCE (LUMINOSITE AMBIANTE)
  #define CAPTEUR A0
  int luminosite = 0; 
543e5426   csaad   modifications non...
49
  int seuil = 200;  //70 change selon le lieu
bb12c4eb   csaad   creation fichier/...
50
51
  
  //INIT interrupteur
3dc1fda1   csaad   modification pin ...
52
  const int pinInter = 2 ; //9 ne marche pas chelou
bb12c4eb   csaad   creation fichier/...
53
54
55
56
57
58
59
60
61
62
63
64
  int etatBouton;
  
  
  //--------------------------------------------------------------------------------------------------------------------------------------SETUP ()
  
  
  void setup() {
    //SETUP LIAISON SERIE 
    Serial.begin(9600);
  
    //SETUP INTER
    pinMode(pinInter, INPUT_PULLUP); //le bouton est une entrée
3dc1fda1   csaad   modification pin ...
65
    //etatBouton = HIGH; //on initialise l'état du bouton comme "relaché"
bb12c4eb   csaad   creation fichier/...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  
    //SETUP LEDS
    //pour les RGB
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
  
    //Led en sortie
    pinMode(PININ, OUTPUT);
  
    
    //SETUP CAPTEUR PRESENCE ET LED ROUGE
    pinMode(pirPin, INPUT);
    pinMode(ledPin, OUTPUT);
    digitalWrite(pirPin, LOW);
    //give the sensor some time to calibrate
    Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
        Serial.print(".");
        delay(1000);
        }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
      
      
    delay(50);
  }
  
  
  //------------------------------------------------------------------------------------------------------------------------FONCTIONS SECONDAIRES
  
  void couleur(int R, int G, int B)
  {
    strip.setPixelColor(0,R,G,B); 
    strip.show();
  }
  
  
  void eteindre()
  {
    strip.setPixelColor(0,0,0,0); 
    strip.show();
    digitalWrite(pirPin, LOW);
    digitalWrite(ledPin, LOW);
    digitalWrite(PININ, LOW);
  }
  
  
  //-------------------------------------------------------------------------------------------------------------------------------------------FONCTIONS PRINCIPALES
  
  //---------------------------------------------------------------------------------------------------------------------------------mainveilleuse()
  
  //fonction du programme principal (à copier plus tard dans la LOOP()
  //elle permet l'utilisation de la veilleuse de manière automatique selon la position de l'interrupteur, sinon c'est l'application qui gere le contrôle
  //Elle est constituée de la fusion des fonctions secondaires: lumiere_auto(), mouvement() et automatique()
  void mainVeilleuse_auto()
  {
    etatBouton = digitalRead(pinInter); //Rappel :pinInter = 9
  
    //MODE AUTOMATIQUE
      //si la veilleuse est allumé et interrupteur = LOW on utilise le mode automatique. 
3dc1fda1   csaad   modification pin ...
126
     while (etatBouton == HIGH) //test si le bouton a un niveau logique haut
bb12c4eb   csaad   creation fichier/...
127
128
129
130
131
      {    
          delay(1000);
          
          etatBouton = digitalRead(pinInter);
  
3dc1fda1   csaad   modification pin ...
132
          if(etatBouton==HIGH)
bb12c4eb   csaad   creation fichier/...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
          {
             //On récupère la valeur du seuil
            luminosite = analogRead(CAPTEUR);
            int i=0;
    
            //Monitoring
            Serial.print("Luminosite = ");
            Serial.print(luminosite);
            Serial.print(" / Seuil = ");
            Serial.print(seuil);
  
            // on l'éteint
            if (luminosite > seuil)
            {
              eteindre();
              Serial.println(" / LED OFF");
            }
  
            //Allumage de la led si la luminosité est inférieur au seuil (on l'allume dans la couleur blanche  la base)
            //enfant couché il doit dormir
            else
            {
              //Monitoring
              couleur(255,255,255);  
        
      
3dc1fda1   csaad   modification pin ...
159
              while(i<=5 && etatBouton==HIGH)
bb12c4eb   csaad   creation fichier/...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
              {
                luminosite = analogRead(CAPTEUR);
  
                if(luminosite < seuil)
                {
                  couleur(255,255,255); 
          
                  //DETECTION D'UN MOUVEMENT
                  if(digitalRead(pirPin)==HIGH)
                  {
                    digitalWrite(ledPin,HIGH);   // la led modélise la déection d'un mouvement selon l'etat du capteur
                    if (lockLow)
                    {
                      //être sûr d'attendre la transistion à l'état LOW du capteur pour continuer :
                      lockLow= false;
                      delay(50); 
                    }
                    takeLowTime = true;
                  }
  
                  //FIN DE LA DETECTION
                  if(digitalRead(pirPin)==LOW)
                  {
                    digitalWrite(ledPin, LOW);
                    if(takeLowTime)
                    {
                      lowIn= millis();      // enregistrement du temps qu'il faut pour passer de HIGH à LOW pour le capteur
                      takeLowTime = false;  // être sûr que cela se fait uniquement au debut du passage à LOW
                    }
                    // Ainsi si le capteur est LOW plus longtemps que la pause, on suppose qu'il n'y plus de mouvement
                    // if > être sûr que la suite s'execute seulement après un nouveau mouvement, donc nouvelle séquence de led allumée
                    if(!lockLow && millis()-lowIn > pause)
                    {
                      lockLow = true;
                      i++;
                      Serial.println(i);
  
                      //CHILD AWAKE
                      if(i==5)
                      {
                        Serial.println("Sommeil agite !!!");
                        MESSAGE = 1;
                        i=0;
                      }
                      delay(20);
                    }  
                  }
         
                  delay(500);
                }
              
                else
                {
                  eteindre();
                  i=0;
                }
  
                etatBouton = digitalRead(pinInter); //Rappel :pinInter = 9
        
              }
              delay(50);
            }
          }
          else
          {
            eteindre();
            Serial.println("UTILISATION APPLICATION");
          }
          
          etatBouton = digitalRead(pinInter); //Rappel :pinInter = 9
      }
  
      //MODE MANUEL (APPLICATION)   
      //on est eteint avec l'interrupteur manuel on peut tjrs utiliser l'application pour gerer la veilleuse
      eteindre(); //la LED reste éteinte
      Serial.println("UTILISATION APPLICATION");
  
      delay(1000);
      
  
  }
  
  //-------------------------------------------------------------------------------------------------------------------------------------------TESTS & LOOP == MAIN PROG
  
  
  void loop() 
  {
   
   mainVeilleuse_auto();
   delay(200);
  }