Keisha Brathwaite
/
KBrat-SSD541-Midterm-Q2a
KBrat-SSD541-Midterm-Q2a
Fork of kl46z_slider_mid_v1 by
Revision 2:8b60d4eb7388, committed 2016-10-10
- Comitter:
- tisbrat
- Date:
- Mon Oct 10 05:56:09 2016 +0000
- Parent:
- 1:44dcf262c7dd
- Commit message:
- KBrat-SSD541-Midterm-Q2a
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Oct 01 22:10:10 2016 +0000 +++ b/main.cpp Mon Oct 10 05:56:09 2016 +0000 @@ -1,30 +1,32 @@ #include "mbed.h" -#include <math.h> +#include <math.h> +#include <cmath> #include "TSISensor.h" #include "SLCD.h" - #define LEDON false #define LEDOFF true -#define NUMBUTS 2 -#define LBUT PTC12 // port addresses for buttons -#define RBUT PTC3 -#define ARGUMENTSTATE 0 -#define ANSWERSTATE 1 -#define TSILIMIT 0.01 +#define NUMBUTS 2 //two buttons +#define LBUT PTC12 //left button // port addresses for buttons +#define RBUT PTC3 //right button +#define ARGUMENTSTATE 0 //Switch case 0 +#define ANSWERSTATE 1 //Switch case 1 +#define LCDTIME 1.0 //LCD Timer 1 sec +#define TSILIMIT 0.01 #define PRINTDELTA 0.01 #define LCDCHARLEN 10 #define DATAINTERVAL 0.1 #define BUTTONTIME 0.1 -#define PROGNAME "kl46z_slider_mid_v1\n\r" +#define PROGNAME "KBrat-SSD541-Midterm-Q2a \nNewton's Method of Square Root\n\r" -SLCD slcd; //define LCD display +SLCD slcd;//define LCD display globally define Serial pc(USBTX, USBRX); -Timer dataTimer; -Timer ButtonTimer; // for reading button states +Timer LCDTimer; //for reading lcd input state for display +Timer dataTimer; //for reading data input states(from the slider 1-100) +Timer ButtonTimer; //for reading button states DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; float tsidata; -int displayState; +int displayState = ARGUMENTSTATE;//Make initial state ARGUMENTSTATE void initialize_global_vars(){ pc.printf(PROGNAME); @@ -32,7 +34,9 @@ ButtonTimer.start(); ButtonTimer.reset(); dataTimer.start(); - dataTimer.reset(); + dataTimer.reset(); + LCDTimer.start(); + LCDTimer.reset(); } void LCDMess(char *lMess){ @@ -44,7 +48,7 @@ int main(void) { int i; char lcdData[LCDCHARLEN]; - float lastTouch = 0.0; + float lastTouch = 0.0; //intial lastTouch starts at 0.0 TSISensor tsi; float tempTSI; PwmOut gled(LED_GREEN); @@ -54,30 +58,77 @@ while (true) { if (ButtonTimer > BUTTONTIME){ - for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 + for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 //find buttons if(!buttons[i]) { - displayState = i; - // do something here. - + displayState = i; } // if ! buttons }// for loop to look at buttons ButtonTimer.reset(); - sprintf (lcdData,"%0.4f",tsidata); + } + + + if(LCDTimer.read() > LCDTIME){ + LCDTimer.reset(); + switch (displayState){//start switch case for displayState + + case ARGUMENTSTATE: {// case #0 + rled = 0.0;//red light on + gled = 1.0;//green light off + + if(dataTimer.read() > DATAINTERVAL){ + dataTimer.reset(); + tempTSI = tsi.readPercentage(); + if (tempTSI > TSILIMIT){ + tsidata = tempTSI; + if (fabs(tsidata - lastTouch)> PRINTDELTA){ + pc.printf("Position %2.0f\n\r", tsidata*100);//print to computer tsidata*100 to get a range from 1-100 + } + } + lastTouch = tsidata; + } + + sprintf (lcdData,"%2.1f",tsidata*100); //print to lcd screen tsidata*100 to get a range from 1-100 + LCDMess(lcdData); + + break; + } + + case ANSWERSTATE: { + + rled = 1.0;//red light off + gled = 0.0;//green light on + + /*-------With built in sqrt function-----*/ + /*double slid_input, sq_root; + slid_input = tsidata*100; + if (slid_input > 0){ + sq_root = sqrt(slid_input); + pc.printf ("Square root(%f) = %f\n", slid_input, sq_root); + sprintf (lcdData,"%2.2f", sq_root); + LCDMess(lcdData); + //return 0; + }*/ + + /*-------Without built in sqrt function-----*/ + double x1, slid_input, sq_root; + slid_input = tsidata*100; + if(slid_input > 0){ + sq_root = log(slid_input); + do { + x1 = (sq_root - (((sq_root * sq_root) - slid_input)/(2* sq_root))); //Newton's Method + sq_root = x1; + + } while ((x1 * x1) > slid_input); + pc.printf ("Newton's Method: Square root(%2.2f) = %2.2f\n", slid_input, sq_root); + sprintf (lcdData,"%2.2f", sq_root); + LCDMess(lcdData); + } + break; + }// end switch case displayState + } LCDMess(lcdData); - rled = 0.0; - gled = 1.0; + } // end LCD timer.read - } - if(dataTimer.read() > DATAINTERVAL){ - dataTimer.reset(); - tempTSI = tsi.readPercentage(); - if (tempTSI > TSILIMIT){ - tsidata = tempTSI; - if (fabs(tsidata - lastTouch)> PRINTDELTA){ - pc.printf("Position %0.4f\n\r", tsidata); - } - } - lastTouch=tsidata; - } - } + }// end while(true) + } \ No newline at end of file