From 3717fc19e6aa7dc61791ac7be54b9bc34592009b Mon Sep 17 00:00:00 2001
From: Ngoc Minh Quan Pham <quandc176@gmail.com>
Date: Tue, 11 Dec 2018 16:44:37 +0100
Subject: [PATCH] Update thingz.ino with mouvement detection

---
 core/thingz.ino.txt |  22 +++++++-
 thingz.ino.txt      | 126 --------------------------------------------
 2 files changed, 20 insertions(+), 128 deletions(-)
 delete mode 100644 thingz.ino.txt

diff --git a/core/thingz.ino.txt b/core/thingz.ino.txt
index 50b98d5..3677513 100644
--- a/core/thingz.ino.txt
+++ b/core/thingz.ino.txt
@@ -6,9 +6,11 @@
 #include "Bluetooth.h"
 #include "Son.h"
 #include "Potentiometer.h"
+#include "MotionSensor.h"
 #define SPAN 3000
 #define HEADER_LIGHT_ON "LIGHT"
 #define HEADER_SOUND_ON "SOUND"
+#define HEADER_PLANT_OK "STOP"
 #define REMAINING_TIME 300000 // 5MIN
 #define DATASPAN 10000 // 5MIN
 #define ALARMSPAN 10000 // 10SEC
@@ -40,6 +42,7 @@ Luminosite luminosite;
 Potentiometer pot;
 Son buzzer;
 Bluetooth bluetooth;
+MotionSensor  motion;
 
 int humi;
 int lumi;
@@ -49,6 +52,7 @@ int dataTimeStamp;
 unsigned long timeStamp;
 boolean ledHasBeenTurnedOn;
 boolean alarmHasBeenTurnedOn;
+boolean plantInNeed;
 unsigned long timePushed;
 unsigned long alarmTimeStamp;
 unsigned long lightTimeStamp;
@@ -63,7 +67,11 @@ void updateSensors(){
 void display(){
     monEcran1.setContrast(70, false);
     updateSensors();
-    monEcran1.printMsg("CRYING PLANT !",0);
+    if(plantInNeed){
+		monEcran1.printMsg("CRYING PLANT !",0);
+	} else {
+		monEcran1.printMsg("PLANT IS OK !",0);
+	}
     monEcran1.printMsg(((String)"TEMP : ") + temp,2);
     monEcran1.printMsg(((String)"HUMI : ") + humi,3);
     monEcran1.printMsg(((String)"LUMI : ") + lumi,4);
@@ -96,9 +104,13 @@ void receiveFromBluetooth() {
     monEcran1.printMsg(buffer,0);
 	if (buffer.startsWith(HEADER_LIGHT_ON)){
 	    dealWithLight();
+		plantInNeed = true;
 	}
 	else if (buffer.startsWith(HEADER_SOUND_ON)){
 	    dealWithSound();
+		plantInNeed = true;
+	} else if(buffer.startsWith(HEADER_PLANT_OK)) {
+		plantInNeed = false;
 	}
 }
 
@@ -132,6 +144,7 @@ void setup()
   timeStamp = 0;
   ledHasBeenTurnedOn = false;
   alarmHasBeenTurnedOn = false;
+  plantInNeed = false;
   timePushed = 0;
   alarmTimeStamp = 0;
   dataTimeStamp = 0;
@@ -180,7 +193,12 @@ if(millis() - timePushed < REMAINING_TIME){
           ledHasBeenTurnedOn = false;
           maLed1.eteint();
       }
-  }
+	}
+	
+	if(plantInNeed && motion.detectsStartOfMotion()){
+		dealWithSound();
+		dealWithLight();
+	}
 }
 
 
diff --git a/thingz.ino.txt b/thingz.ino.txt
deleted file mode 100644
index 8331a8d..0000000
--- a/thingz.ino.txt
+++ /dev/null
@@ -1,126 +0,0 @@
-#include "Bouton.h"
-#include "Led.h"
-#include "Meteo.h"
-#include "Screen.h"
-#include "Luminosite.h"
-#include "Bluetooth.h"
-#include "Son.h"
-#define SPAN 3000
-#define HEADER_LIGHT_ON "LIGHT"
-#define HEADER_SOUND_ON "SOUND"
-#define REMAINING_TIME 10000
-#define FAb4 740
-#define LAb4 932
-/*
-Thingz prend ses mesures et envoie les données à l'appli java (toutes les 5mins par ex)
-l'appli java est une IHM configurable qui :
-    affiche les données en temps réel sur des graphiques
-    sait s'il faut arroser la plante ou non
-    envoie un mail au prof si besoin pour arroser
-    envoie via bluetooth si arrosage possible ou urgent et si en semaine ou weekend
-    son plus ou moins long
-
-Messages bluetooth :  
-LIGHT;
-SOUND;WEEKEND
-SOUND;SEMAINE
-*/
-
-Bouton monBouton1;
-Led maLed1;
-Meteo meteo;
-Screen monEcran1;
-Bluetooth bluetooth;
-Luminosite luminosite;
-Son buzzer;
-int humi;
-int lumi;
-int temp;
-unsigned long timeStamp;
-boolean ledHasToBeTurnedOn;
-unsigned long timePushed;
-unsigned long alarmTimeStamp;
-
-void setup()
-{
-  temp = 0;
-  lumi = 0;
-  humi = 0;
-  timeStamp = 0;
-  ledHasToBeTurnedOn = false;
-  timePushed = 0;
-  alarmTimeStamp = 0;
-}
-
-void updateSensors(){
-    temp = meteo.temperature();
-    lumi = luminosite.etat();
-    humi = meteo.getHumidity();
-}
-void display(){
-    monEcran1.setContrast(70, false);
-    updateSensors();
-    monEcran1.printMsg("CRYING PLANT !",0);
-    monEcran1.printMsg(((String)"TEMP : ") + temp,2);
-    monEcran1.printMsg(((String)"HUMI : ") + humi,3);
-    monEcran1.printMsg(((String)"LUMI : ") + lumi,4);
-}
-
-void sendBluetoothDataToServer(){
-    
-}
-
-void dealWithSound(String data){
-    if(0){ // on weekend then water
-        
-    } else { // on weekday then trigger the alarm for 5min
-        alarmTimeStamp = millis();
-    }
-}
-
-void receiveFromBluetooth() {
-	String buffer;
-
-	if (bluetooth.dataAvailable()){
-		buffer = bluetooth.receive();
-	}
-
-	if (buffer.startsWith(HEADER_LIGHT_ON)){
-	    // DO NOTHING YET
-	}
-	else if (buffer.startsWith(HEADER_SOUND_ON)){
-	    dealWithSound(buffer); // change
-	}
-}
-
-
-void fireAlarm(){
-  buzzer.tone(FAb4,600);
-  buzzer.tone(LAb4,200);
-}
-
-void loop()
-{
-
-if(millis() - timePushed < REMAINING_TIME){
-  if(millis() - timeStamp > SPAN){
-      timeStamp = millis();
-        display();
-  }
-} else {
-      monEcran1.clear();
-      monEcran1.setContrast(0, false);
-  }
-  
-  if (monBouton1.estTenuAppuye()) {
-     timePushed = millis();
-     maLed1.inverse();
-  }
-  
-  if(millis() - alarmTimeStamp < REMAINING_TIME && millis() > REMAINING_TIME){
-      fireAlarm();
-  }
-
-}
-
-
-- 
GitLab