Recursion 12.2

Dependencies:   SLCD TSI mbed

Fork of kl46z_slider_mid_v1_amart by AnnaLouise Martinez

Revision:
3:2bb5ccf119fc
Parent:
2:bb868c525c5c
--- a/main.cpp	Mon Oct 10 04:04:39 2016 +0000
+++ b/main.cpp	Fri Nov 04 21:33:36 2016 +0000
@@ -15,7 +15,7 @@
 #define LCDCHARLEN 10
 #define DATAINTERVAL 0.1
 #define BUTTONTIME 0.1
-#define PROGNAME "SquareRootMidterm_AnnaLouise Martinez\n\r"
+#define PROGNAME "Recursive_C++_AnnaLouise Martinez\n\r"
 
 SLCD slcd; //define LCD display
 Serial pc(USBTX, USBRX);
@@ -41,33 +41,22 @@
         slcd.printf(lMess);
 }
 
-float sqroot(float tsiData)
+int countdown(int number, int increm, int minNum)
 {
- // Newton's method for square root
-    float xnew = 0.0;
-    int intmax = 20;
-    float epsilon = 1e-7;
-    float xold = float(tsiData/2.5);
-    
-    for(int i =0; i< intmax; i++)
+  int increment = increm;
+  int min_number = minNum;
+  int intmax = number;
+  
+   if(intmax <= min_number)
     {
-        xnew = 0.5*(xold + ((tsiData)/xold)); // Calculation
-       float delta = abs(xnew-xold); // Compare old and new values
-        
-        if (delta < epsilon) //Check for convergence
-           { 
-                break;
-           }
-        else
-           {
-                xold = xnew;  //replace new calculated value to redo the calculation
-           }
+        pc.printf("%d\n\r", intmax);
+        return intmax;
     }
-    //float newtsiData = tsiData * 100;
-    pc.printf("The square root of ");
-    pc.printf("%0.4f", tsiData);
-    pc.printf("  is %0.4f\n\r", xnew);
-    return xnew;    
+    else
+    {
+      pc.printf("%d\n\r", intmax);
+      return countdown(intmax - increment, increment, min_number); 
+    }  
 }
 
 int main(void) {
@@ -78,6 +67,9 @@
     float tempTSI;
     PwmOut gled(LED_GREEN);
     PwmOut rled(LED_RED);
+    int num = 50;
+    int incr = 2;
+    int minNum = 2;
     
     initialize_global_vars();
 
@@ -86,8 +78,8 @@
             for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
                 if(!buttons[i]) { 
                     displayState = i;
+                   int numReturn = countdown(num, incr, minNum);
                     // do something here.
-                    
                 } // if ! buttons
             }// for loop to look at buttons
             ButtonTimer.reset();
@@ -103,9 +95,8 @@
             if (tempTSI > TSILIMIT){
                 tsidata = tempTSI;
                 if (fabs(tsidata - lastTouch)> PRINTDELTA){
-                    pc.printf("Position %0.4f\n\r", tsidata);
-                    float sqrt = sqroot(tsidata * 100); 
-                }           
+
+               }           
             }
             lastTouch=tsidata;
         }