Pārlūkot izejas kodu

LDR management with averaging and hysteresis

using ResponsiveAnalogRead to smooth the values and SetPoint to make
hysteresis booleanvalue out of LDR
Etienne Landon 8 gadi atpakaļ
vecāks
revīzija
f3134f9fdb
1 mainītis faili ar 79 papildinājumiem un 41 dzēšanām
  1. 79 41
      I_Mihalache_AudioPlayer.ino

+ 79 - 41
I_Mihalache_AudioPlayer.ino

@@ -5,6 +5,11 @@
 #include <SerialFlash.h>
 #include <Metro.h>
 
+//Sensor smoothing and hysteresys
+#include <SetPoint.h>
+#include <ResponsiveAnalogRead.h>
+
+
 /*    ESP commands
  * AT+MODE=1
  * AT+CWLAP lists wifi AP
@@ -51,8 +56,16 @@ Metro sendRMS = Metro(50);
 
 int WAIT_TIME=10000 ;
 
+const int LDR_PIN = A7;
+ResponsiveAnalogRead ldr(LDR_PIN, false);
+int threshold = 700;
+int hysteresis = 100;
+int sensorPin = A7;
+SetPoint setPoint;
+
 bool presence ;
 uint32_t LastPresence ;
+
  
 // Use these with the Teensy Audio Shield
 #define SDCARD_CS_PIN    10
@@ -103,11 +116,16 @@ void setup() {
       delay(500);
     }
   }
+  Serial.println("Playing LOOP file");
   LoopPlayer.play(LOOP_WAV);
   // A brief delay for the library read WAV info
   delay(10); 
 
   
+  setPoint.begin(threshold, hysteresis);
+  setPoint.attach(RISING_EDGE, Presence);
+  setPoint.attach(FALLING_EDGE, Absence);
+  
   //ESP8266
   Serial1.println("AT+GMR");
   //while (!Serial1.available()) {};
@@ -190,6 +208,13 @@ void sendUDP() {
 void loop() {
   
   // LOOP FILE
+
+  ldr.update();
+  int value = ldr.getValue();
+  setPoint.update(value);
+
+
+  // LOOP is alwaiys playing
   if (!LoopPlayer.isPlaying()) {
     Serial.println("Playing LOOP file");
     LoopPlayer.play(LOOP_WAV);
@@ -197,49 +222,17 @@ void loop() {
     delay(10); 
   }
 
-  // PRESENCE FILE
-  presence = checkPresence(16, 20);
-  //printLDR();
-  //Serial.println(presence);
-
-  //PRESENCE DETECTED
-  if (presence ) { 
-    LastPresence = millis() ; 
-    
-    if (!PresencePlayer.isPlaying()) {
-      //stop WAIT_WAV if playing
-      if (WaitPlayer.isPlaying()) {
-          //fade out WaitPlayer
-          Serial.println("Fade out Wait file");
-          for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
-            mixer1.gain(1, 1.0 - fadeRamp/100) ;
-          }
-          WaitPlayer.stop() ;
-          mixer1.gain (1, 1.0); // set volume for next play
-          delay(2000) ; 
-        
-      }
-      PresencePlayer.play(PRESENCE_WAV) ;
-      delay(15);
-      Serial.println("Playing presence file");
-    }   
+//  // PRESENCE FILE
+//  presence = checkPresence(16, 20);
+//  //printLDR();
+//  //Serial.println(presence);
+//
+//  //PRESENCE DETECTED
+//  if (presence ) { 
+//    
+//  else if (!presence ) {
     
-    // if presence and playing, don't do anything
-  }
-  else if (!presence ) {
     
-    if (PresencePlayer.isPlaying()) {
-      //what if presence gone while playing ?
-      while(rms1.read() > 0) {  }
-      PresencePlayer.stop();
-      
-    }
-    if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) {
-      WaitPlayer.play(WAIT_WAV);
-      Serial.println("Playing wait file");
-      delay(10);
-    }
-  }
 
 //  if (checkUDP.check() ==1) {
 //    UDPalive = checkUDPconnected();
@@ -255,3 +248,48 @@ void loop() {
 //  
 }
 
+//////////////////////    LDR event handlers    /////////////////////////////
+
+void Presence() {
+
+  Serial.println("Presence detected");
+  LastPresence = millis() ; 
+    
+  if (!PresencePlayer.isPlaying()) {
+    //stop WAIT_WAV if playing
+    if (WaitPlayer.isPlaying()) {
+        //fade out WaitPlayer
+        Serial.println("Fade out Wait file");
+        for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
+          mixer1.gain(1, 1.0 - fadeRamp/100) ;
+        }
+        WaitPlayer.stop() ;
+        mixer1.gain (1, 1.0); // set volume for next play
+        delay(2000) ; 
+      
+    }
+    PresencePlayer.play(PRESENCE_WAV) ;
+    delay(15);
+    Serial.println("Playing presence file");
+  }   
+    
+    // if presence and playing, don't do anything
+  }
+
+void Absence() {
+  Serial.println("No presence detected");
+
+  if (PresencePlayer.isPlaying()) {
+      //what if presence gone while playing ?
+      Serial.println("Stopping Presence file");
+      while(rms1.read() > 0) {  }
+      PresencePlayer.stop();
+      
+    }
+    if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) {
+      WaitPlayer.play(WAIT_WAV);
+      Serial.println("Playing wait file");
+      delay(10);
+    }
+  }
+