Gets input from slider and returns the square root of that number

Dependencies:   SLCD TSI mbed

Files at this revision

API Documentation at this revision

Comitter:
Raiden817
Date:
Fri Oct 07 02:16:15 2016 +0000
Parent:
0:be53fac41c7a
Commit message:
SquareRoot with slider

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r be53fac41c7a -r 81905675b73e main.cpp
--- a/main.cpp	Thu Oct 06 17:50:58 2016 +0000
+++ b/main.cpp	Fri Oct 07 02:16:15 2016 +0000
@@ -42,14 +42,30 @@
         slcd.printf(lMess);
 }
 
-int squareRoot(int val) {
-    double sqr = 0;
+float squareRoot(int val) {
+    float xNew = 0.0;
+    float xOld = val/2.0;
+    int max = 40;
+    float epsilon = 0.0000007;
+    float delta;
     
-    return sqr;
+    for (int i = 0; i < max; i++) {
+        xNew = 0.5 * (xOld + (val/xOld) );
+        delta = abs(xNew - xOld);
+        //Check Convergence
+        if (delta < epsilon) {
+            break;
+        } else {
+            xOld = xNew;
+        }
+    }
+    
+    return xNew;
 }
 
 int main(void) {
     int i;  
+    float holdVal;
     float lastTouch = 0.0;
     char lcdData[LCDCHARLEN];
     PwmOut gled(LED_GREEN);
@@ -65,36 +81,45 @@
             for (i = 0; i < NUMBUTS; i++) {
                 if(!buttons[i]) {
                     displayState = i;
+                    
                 }
             }
             //Get slider value
             if (displayState == 0) {
-                rled = 1.0;
-                gled = 0.0;
+                rled = 0.0;
+                gled = 1.0;
                 if (dataTimer.read() > DATAINTERVAL) {
                     dataTimer.reset();
-                    tempTSI = tsi.readPercentage();
-                    tempTSI = tempTSI * 100;
+                    tempTSI = tsi.readPercentage() *100;
                     if (tempTSI > TSILIMIT) {
                         tsiData = tempTSI;
                         if (tempTSI > TSILIMIT) {
                             tsiData = tempTSI;
                             if (fabs(tsiData - lastTouch) > PRINTDELTA) {
-                                pc.printf("Position %0.4f\n\r", tsiData);
+                                pc.printf("Position %0.0f\n\r", tsiData);
                             }
                         }
                         lastTouch = tsiData;
                     }
-                }                              
+                    
+                }        
+                sprintf(lcdData,"%0.0f",tsiData);
+                LCDMess(lcdData);                      
             } 
             //Perform SquareRoot
-            else {
-                rled = 0.0;
-                gled = 1.0;               
+            else {               
+                rled = 1.0;
+                gled = 0.0; 
+                
+                holdVal = squareRoot(tsiData);  
+                sprintf(lcdData,"%2.2f",holdVal); 
+                if (holdVal < 0.0) {
+                     sprintf(lcdData, "z%2.2f", fabs(holdVal));
+                }
+                
+                LCDMess(lcdData);                      
             }
             buttonTimer.reset();
-            sprintf(lcdData,"%0.4f",tsiData);
-            LCDMess(lcdData);
         }   
     }
 }
\ No newline at end of file