Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of kl46z_slider_mid_v1 by
Revision 2:bb868c525c5c, committed 2016-10-10
- Comitter:
- annalou
- Date:
- Mon Oct 10 04:04:39 2016 +0000
- Parent:
- 1:44dcf262c7dd
- Commit message:
- AMart_SSD341_Mid
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 44dcf262c7dd -r bb868c525c5c main.cpp --- a/main.cpp Sat Oct 01 22:10:10 2016 +0000 +++ b/main.cpp Mon Oct 10 04:04:39 2016 +0000 @@ -15,7 +15,7 @@ #define LCDCHARLEN 10 #define DATAINTERVAL 0.1 #define BUTTONTIME 0.1 -#define PROGNAME "kl46z_slider_mid_v1\n\r" +#define PROGNAME "SquareRootMidterm_AnnaLouise Martinez\n\r" SLCD slcd; //define LCD display Serial pc(USBTX, USBRX); @@ -41,6 +41,35 @@ slcd.printf(lMess); } +float sqroot(float tsiData) +{ + // 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++) + { + 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 + } + } + //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; +} + int main(void) { int i; char lcdData[LCDCHARLEN]; @@ -66,15 +95,16 @@ LCDMess(lcdData); rled = 0.0; gled = 1.0; - } + if(dataTimer.read() > DATAINTERVAL){ dataTimer.reset(); - tempTSI = tsi.readPercentage(); + tempTSI = tsi.readPercentage(); 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;