Blame view

progArduino/principal/principal.ino 8.41 KB
38e849ce   csaad   ajout dossier pro...
1
2
3
4
5
6
7
8
9
10
11
12
13
  ////////////////////////////////////////   PROJET SC: VEILLEUSE CONNECTEE   (IMA3)
  //Claire Vandamme
  //Justine Senellart
  //Camille Saâd
  
  
  
  //--------------------------------------------------------------------------------------------------------------- INITIALISATION CONSTANTES et paramètres
  
  
  //INIT CAPTEUR PRESENCE
  
  //temps donné pour calibrer le capteur de présence.(10-60 secs according to the datasheet)
dd13ea41   csaad   création automati...
14
  int calibrationTime = 10;        
38e849ce   csaad   ajout dossier pro...
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
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
126
127
128
129
130
131
132
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
159
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
  //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; 
  int seuil = 200; 
  
  //INIT interrupteur
  const int pinInter = 9 ;
  int etatBouton;
  
  
  //--------------------------------------------------------------------------------------------------------------------------------------SETUP ()
  
  
  void setup() {
    //SETUP LIAISON SERIE 
    Serial.begin(9600);
  
    //SETUP INTER
    pinMode(pinInter, INPUT_PULLUP); //le bouton est une entrée
    etatBouton = HIGH; //on initialise l'état du bouton comme "relaché"
  
    //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 choix_couleur()
  {
    int R,G,B;
    
    Serial.println(" Rouge =");
    while (Serial.available()<0){ delay(10);}
    R=Serial.read();
    Serial.print(R);
    Serial.print("  Vert =");
    while (Serial.available()<0){ delay(10);}
    G=Serial.read();
    Serial.print(G);
    Serial.print("   Bleu =");
    while (Serial.available()<0){ delay(10);}
    B=Serial.read();
    Serial.print(B);
    
    delay(3000);
    couleur(R,G,B);
  }
  */
  
  void eteindre()
  {
    strip.setPixelColor(0,0,0,0); 
    strip.show();
    digitalWrite(pirPin, LOW);
    digitalWrite(ledPin, LOW);
    digitalWrite(PININ, LOW);
  }
  
  void choix_intensite()
  {
    int k;
    k=Serial.read();
    strip.setBrightness(k);  //permet de régler la luminosité
  }
  
  //fonction qui allume les led de manière automatique selon la luminosité ambiante
  void lumiere_auto()
  {
    //On récupère la valeur du seuil
    luminosite = analogRead(CAPTEUR);
    
    //Monitoring
    Serial.print("Luminosite = ");
    Serial.print(luminosite);
    Serial.print(" / Seuil = ");
    Serial.print(seuil);
   
    //Allumage de la led si la luminosité est inférieur au seuil (on l'allume dans la couleur blanche  la base)
    if(luminosite < seuil) {
      couleur(255,255,255);
      //Monitoring
      Serial.println(" / LED ON");
    } 
    //Dans le cas contraire, on l'éteint
    else {
      digitalWrite(PININ, LOW);
      eteindre();
      Serial.println(" / LED OFF");
    }
    delay(50);
  }
  
  /*
  void choix_type_allumage()
  {
    int choix;
    Serial.println("Vous pouvez choisir entre l'allumage automatique (choix=1) de la veilleuse et l'allumage manuel (choix=2).");
    choix= Serial.read();
    if(choix==1)
    {
      lumiere_auto();
    }
    else if (choix==2)
    {
      int on_off;
      Serial.println("Voulez vous allumer la veilleuse? (non = 0; oui =1)");
      on_off=Serial.read();
      //permet d'utiliser un allumage application. 
    }
    else
    {
      Serial.println("ERROR CHOIX ALLUMAGE.");
    }
  }
  */
  
  void mouvement()
  {
    int i=0;
       while(i<5)
       {
        
        //MOVEMENT DETECTION
         if(digitalRead(pirPin) == HIGH)
         {
          digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
          if(lockLow)
          { 
            //makes sure we wait for a transition to LOW before any further output is made:
            lockLow = false;            
dd13ea41   csaad   création automati...
207
208
209
210
            //Serial.println("---");
            //Serial.print("motion detected at ");
            //Serial.print(millis()/1000);
            //Serial.println(" sec"); 
38e849ce   csaad   ajout dossier pro...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
            delay(50);
           }         
          takeLowTime = true;
         }
        
         //DETECTION'S END
         if(digitalRead(pirPin) == LOW)
         {       
           digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
  
           if(takeLowTime)
           {
            lowIn = 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() - lowIn > pause){  
             //makes sure this block of code is only executed again after 
             //a new motion sequence has been detected
             lockLow = true;                        
dd13ea41   csaad   création automati...
232
233
234
             //Serial.print("motion ended at ");      //output
             //Serial.print((millis() - pause)/1000);
             //Serial.println(" sec");
38e849ce   csaad   ajout dossier pro...
235
             i++;
dd13ea41   csaad   création automati...
236
             Serial.println(i);
38e849ce   csaad   ajout dossier pro...
237
             //CHILD IS AWAKE
dd13ea41   csaad   création automati...
238
239
            // if(i==5 || ((millis() - pause)/1000)-(millis()/1000)> 10 
             if(i==5){
38e849ce   csaad   ajout dossier pro...
240
241
242
243
244
245
246
247
248
249
250
              Serial.println("Sommeil agite !!!");
              MESSAGE = 1; //servira pour la progra html (peut-être)
              Serial.println(MESSAGE); 
              i=0;
              }
             delay(50);
             }
         }
       }
  }
  
dd13ea41   csaad   création automati...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  void automatique()
  {
    //On récupère la valeur du seuil
    luminosite = analogRead(CAPTEUR);
    int mouv =1;
    
    //Monitoring
    Serial.print("Luminosite = ");
    Serial.print(luminosite);
    Serial.print(" / Seuil = ");
    Serial.print(seuil);
   
    //Allumage de la led si la luminosité est inférieur au seuil (on l'allume dans la couleur blanche  la base)
    if(luminosite < seuil) {
      couleur(255,255,255);
      //Monitoring
      Serial.println(" / LED ON");
    } 
    //Dans le cas contraire, on l'éteint
    else {
      digitalWrite(PININ, LOW);
      mouv=0;
      eteindre();
      Serial.println(" / LED OFF");
    }
    delay(50);
  
    if(mouv == 1)
    {
      //Serial.println("mouv");
      mouvement();
    }
  }
  
38e849ce   csaad   ajout dossier pro...
286
287
288
289
290
291
292
  //---------------------------------------------------------------------------------------------------------------------------FONCTIONS PRINCIPALES
  
  
  //fonction du programme principal (à copier plus tard dans la LOOP()
  void mainVeilleuse()
  {
    etatBouton = digitalRead(pinInter); //Rappel :pinInter = 9
dd13ea41   csaad   création automati...
293
    
38e849ce   csaad   ajout dossier pro...
294
295
      //si la veilleuse est allumé et interrupteur = LOW on utilise le mode automatique. 
     while (etatBouton == LOW) //test si le bouton a un niveau logique HAUT
dd13ea41   csaad   création automati...
296
      {    
38e849ce   csaad   ajout dossier pro...
297
          delay(1000);
dd13ea41   csaad   création automati...
298
299
300
          
          automatique();
          
38e849ce   csaad   ajout dossier pro...
301
302
          etatBouton = digitalRead(pinInter); //Rappel :pinInter = 9
      }
dd13ea41   csaad   création automati...
303
    
38e849ce   csaad   ajout dossier pro...
304
305
      //on est eteint avec l'interrupteur manuel on peut tjrs utiliser l'application pour gerer la veilleuse
      eteindre(); //la LED reste éteinte
dd13ea41   csaad   création automati...
306
      Serial.println("UTILISATION APPLICATION");
38e849ce   csaad   ajout dossier pro...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
      
  
  }
  
  
  
  //fonction permettant de faire les tests des foncions secondaires
  void tests()
  {
   //couleur(0,255,0);
   //choix_couleur();
   //eteindre();
   //choix_intensite();
   //choix_type_allumage();
dd13ea41   csaad   création automati...
321
   //mouvement();
38e849ce   csaad   ajout dossier pro...
322
   //lumiere_auto();
dd13ea41   csaad   création automati...
323
   automatique();
38e849ce   csaad   ajout dossier pro...
324
325
326
327
328
329
330
331
332
333
   delay(200);
  }
  
  
  
  
  //----------------------------------------------------------------------------------------------------------------------------LOOP == MAIN PROG
  void loop() 
  
  {
dd13ea41   csaad   création automati...
334
   //tests();
38e849ce   csaad   ajout dossier pro...
335
  
dd13ea41   csaad   création automati...
336
   mainVeilleuse();
38e849ce   csaad   ajout dossier pro...
337
338
    delay(200);
  }