KRAI 2016 / Mbed 2 deprecated Ultimate_Hybrid_LF_v4

Dependencies:   ESC Motordriver PS_PAD Ping hadah mbed

Fork of Ultimate_Hybrid_LF by KRAI 2016

Revision:
7:59a513681663
Parent:
6:7fce519e4976
--- a/sensor_goertzel.txt	Thu Apr 28 14:59:21 2016 +0000
+++ b/sensor_goertzel.txt	Sat Feb 10 04:24:23 2018 +0000
@@ -22,11 +22,13 @@
 int sensor[6] = {A0,A5,A4,A1,A3,A2};
 
 // output pins
-//int output[6] = {0,1,2,3,4,5,13,6,12,7,11,8,10};
+int output[6] = {8,9,10,11,12,13};
 int calibrateButton = 2;
 
 //sensor data
 float THRESHOLD[6];
+float lref[6];
+float href[6];
 float magnitude[6];
 char data;
 
@@ -55,7 +57,7 @@
     tone(pwm, TARGET_FREQUENCY); 
     data = 0;
     
-    //Sensor logic
+    //Sensor logic cd
     for(int i=0; i<6; i++)
     {
       goertzel.sample(sensor[i]); //Will take n samples
@@ -65,40 +67,60 @@
       {
         data |= 1 << i;
         //Serial.print("1");
-        //digitalWrite(output[i],HIGH);
+        digitalWrite(output[i],HIGH);
       }
       else
       {
         data |= 0 << i;
-         //Serial.print("0");
-         //digitalWrite(output[i],LOW);
+        //Serial.print("0");
+        digitalWrite(output[i],LOW);
       }
   
       //Serial.print(THRESHOLD[i]);
       //Serial.print(magnitude[i]);
       //Serial.print("\t");
 
-      
     }
-
+ 
     //Serial.println();
     /* ============= KALIBRASI ============== */ 
 
     if(digitalRead(calibrateButton)== LOW)
     {
-       //Serial.write(0xFF);
-       for(int i=0; i<6; i++)
-       {
-          stat.clear();
-          for (int n=0; n<50; n++)
-          {
-            stat.add(magnitude[i]);
-          }
-          THRESHOLD[i] = stat.minimum() - 20;
+      for(int i=0; i<6; i++){
+        href[i] = THRESHOLD[i];
+        digitalWrite(output[i],HIGH);
+      }
+
+      do{
+        read_magnitude();
+        for(int i=0; i<6; i++){
+          if(magnitude[i] < href[i])        lref[i] = magnitude[i];
+          if(magnitude[i] > lref[i])        href[i] = magnitude[i];
+        }
+
+        //Serial.println("cek data");
+        delay(200);
+      }while( digitalRead(calibrateButton)!= LOW);
+
+      //Serial.println("simpan"); 
+      for(int i=0; i<6; i++){
+          THRESHOLD[i] = (href[i] + lref[i])/2;
        }
+      
        EEPROM.updateBlock(addr,THRESHOLD,6);
+
+       delay(200);
     }
     else{
-      Serial.write(data);
-    }
-}  
\ No newline at end of file
+       Serial.write(data);
+    }  
+}  
+
+void read_magnitude(){
+     for(int i=0; i<6; i++)
+     {
+        goertzel.sample(sensor[i]); //Will take n samples
+        magnitude[i] = goertzel.detect();  //check them for target_freq
+     }
+}