patle

Dependencies:   SLCD TSI mbed

Fork of slider_tone_v1 by Stanley Cohen

Revision:
3:ee81131ce456
Parent:
2:7f347d6a6422
--- a/slider_tone_v1.cpp	Sat Feb 14 23:03:06 2015 +0000
+++ b/slider_tone_v1.cpp	Mon Apr 27 23:45:01 2015 +0000
@@ -24,9 +24,12 @@
 
 TSISensor tsiScaling; // Capacitive sensor/slider
 
+AnalogIn analogRand(PTB3);
 PwmOut led(LED_RED);
 DigitalOut outPin(PTC9); //J1-16
 PwmOut soundOut(PTA13);
+Serial pc(USBTX, USBRX);
+
 // Global scalars
 char lcdData[LCDLEN];
 
@@ -54,7 +57,6 @@
 }
 void lightAdjust( float scaling) { // Control brightness of LED
     float tempDutyFactor;
-    
     tempDutyFactor = 1.0 - scaling; //LED is a sinking connection 
                                     // anode is held at 5V
     led.write(tempDutyFactor); //sdjusting duty factor
@@ -63,11 +65,15 @@
 int main(){
     int tempInt;
     float tempValue;
+    float analogValue;
+    float mx = 0;
+    float mn = 1;
+    float previousDelta = 0;
+    float delta = 0;
+    
     tonePeriod = 1.0/toneFreq; 
     soundOut.period(tonePeriod);
     
-    
-    
     led.write(CHANNELON);
     outPin.write(CHANNELOFF);
     tempInt = (int)toneFreq;
@@ -77,10 +83,27 @@
     while (true) {
     
         tempValue = tsiScaling.readPercentage();
-        if(tempValue > 0) {
+        for (int i = 0; i < 1000; ++i) {
+            analogValue = analogRand.read();
+            //mn =  mn > analogValue ? analogValue : mn; 
+//            mx = mx < analogValue ? analogValue : mx;
+            if (analogValue > mx){
+                mx = analogValue;
+            } 
+            if (analogValue < mn){
+                mn = analogValue;
+            }
+                
+        }
+        delta = mx - mn;
+        
+        pc.printf("%f    %f    %f    %f\n", analogValue, mx, mn, delta);
+        wait_ms(DATATIME);
+        if(previousDelta !=  delta) {
             soundOut.write(TONEON); // set duty factor to 505
-            toneAdjust( tempValue);
-            lightAdjust(tempValue);
+            toneAdjust( analogValue);
+            lightAdjust(analogValue);
+            previousDelta = delta;
         } else { 
             soundOut.write(TONEOFF); // set dutyfactor to 0%
             LCDMessNoDwell("SOFF");