Keisha Brathwaite
/
KBrat-SSD541-Midterm-Q2a-1
KBrat-SSD541-Midterm-Q2a
Fork of KBrat-SSD541-Midterm-Q2a by
main.cpp
- Committer:
- tisbrat
- Date:
- 2016-10-10
- Revision:
- 2:8b60d4eb7388
- Parent:
- 1:44dcf262c7dd
File content as of revision 2:8b60d4eb7388:
#include "mbed.h" #include <math.h> #include <cmath> #include "TSISensor.h" #include "SLCD.h" #define LEDON false #define LEDOFF true #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 "KBrat-SSD541-Midterm-Q2a \nNewton's Method of Square Root\n\r" SLCD slcd;//define LCD display globally define Serial pc(USBTX, USBRX); 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 = ARGUMENTSTATE;//Make initial state ARGUMENTSTATE void initialize_global_vars(){ pc.printf(PROGNAME); // set up DAQ timers ButtonTimer.start(); ButtonTimer.reset(); dataTimer.start(); dataTimer.reset(); LCDTimer.start(); LCDTimer.reset(); } void LCDMess(char *lMess){ slcd.Home(); slcd.clear(); slcd.printf(lMess); } int main(void) { int i; char lcdData[LCDCHARLEN]; float lastTouch = 0.0; //intial lastTouch starts at 0.0 TSISensor tsi; float tempTSI; PwmOut gled(LED_GREEN); PwmOut rled(LED_RED); initialize_global_vars(); while (true) { if (ButtonTimer > BUTTONTIME){ for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 //find buttons if(!buttons[i]) { displayState = i; } // if ! buttons }// for loop to look at buttons ButtonTimer.reset(); } 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); } // end LCD timer.read }// end while(true) }