Shane Barton
/
sbart_recursion_count_v1
HW 12.2
Fork of sbart_midterm_Q2 by
Revision 3:771f97716830, committed 2016-11-06
- Comitter:
- sbart
- Date:
- Sun Nov 06 22:29:57 2016 +0000
- Parent:
- 2:6610fa50aa81
- Commit message:
- HW 12.2
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 6610fa50aa81 -r 771f97716830 main.cpp --- a/main.cpp Mon Oct 10 03:03:43 2016 +0000 +++ b/main.cpp Sun Nov 06 22:29:57 2016 +0000 @@ -18,6 +18,13 @@ #define LCDTIME 0.25 #define PROGNAME "kl46z_slider_mid_v1\n\r" +#define MIN_NUM -5 +#define INCREMENT 2 + +#include <string> + +//using namespace std; + SLCD slcd; //define LCD display Serial pc(USBTX, USBRX); @@ -46,27 +53,38 @@ slcd.printf(lMess); } -float newtonSqrRt(float a) { - - float xnew = 0.0; - int intmax = 20; - //int n_int = 0; - float epsilon = 1e-7; - float xold = (a/2.5); //Make a guess - - for (int i = 0; i < intmax; i++) - { - xnew = 0.5 * (xold + (a/xold)); //Calculation - float delta = fabs(xnew - xold); //Compare old and new values +//float newtonSqrRt(float a) { +// +// float xnew = 0.0; +// int intmax = 20; +// //int n_int = 0; +// float epsilon = 1e-7; +// float xold = (a/2.5); //Make a guess +// +// for (int i = 0; i < intmax; i++) +// { +// xnew = 0.5 * (xold + (a/xold)); //Calculation +// float delta = fabs(xnew - xold); //Compare old and new values +// +// if (delta < epsilon) +// break; +// else +// xold = xnew; +// } +// +// return xnew; +//} + +void backwardCount(float n, int delta, int minNum){ - if (delta < epsilon) - break; - else - xold = xnew; + pc.printf("current: %0f\n\r", n); + + if(n <= minNum){ + return; + } else { + backwardCount(n-delta, delta, minNum); + } } - - return xnew; -} int main(void) { int i; @@ -76,24 +94,36 @@ float tempTSI; PwmOut gled(LED_GREEN); PwmOut rled(LED_RED); + pc.printf(PROGNAME); initialize_global_vars(); while (true) { if (ButtonTimer > BUTTONTIME){ + float count = 0; for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 if(!buttons[i]) { displayState = i; // do something here. switch (displayState) { - case 0: //R BUTTON + case ARGUMENTSTATE: //R BUTTON + + count = tsidata * 100; + pc.printf("Position %0f\n\r", count); rled = 1.0; gled = 0.0; + count = lastTouch; + break; - case 1: //L BUTTON + case ANSWERSTATE: //L BUTTON + count = tsidata * 100; + pc.printf("Count before: %0f\n\r", count); + backwardCount(count, INCREMENT, MIN_NUM); rled = 0.0; gled = 1.0; + + break; } @@ -107,25 +137,30 @@ dataTimer.reset(); tempTSI = tsi.readPercentage(); if (tempTSI > TSILIMIT){ - tsidata = tempTSI; + tsidata = tempTSI/2; if (fabs(tsidata - lastTouch)> PRINTDELTA){ - pc.printf("Position %0.4f\n\r", tsidata); + //pc.printf("Position %2f\n\r", tsidata); } } lastTouch=tsidata; } if (LCDTimer.read() > LCDTIME) { + + //float count = 0; LCDTimer.reset(); switch(displayState) { - case 0: //R BUtton - float sqrRoot = newtonSqrRt(lastTouch); - sprintf (lcdData,"%0.2f", sqrRoot * 10); - LCDMess(lcdData); + case 0: //R BUtton + //pc.printf("R Button"); + //count = lastTouch; + //sprintf (lcdData,"%0f", count * 10); + //LCDMess(lcdData); break; case 1: //L Button - sprintf (lcdData,"%0.2f", (tsidata * 100)); - LCDMess(lcdData); + //pc.printf("L Button"); + //count = lastTouch * 10; + //backwardCount(count, INCREMENT, MIN_NUM); + //LCDMess(lcdData); break; default: break;