Keisha Brathwaite
/
KBrat-SSD541-Midterm-Q2a-1
KBrat-SSD541-Midterm-Q2a
Fork of KBrat-SSD541-Midterm-Q2a by
main.cpp@2:8b60d4eb7388, 2016-10-10 (annotated)
- Committer:
- tisbrat
- Date:
- Mon Oct 10 05:56:09 2016 +0000
- Revision:
- 2:8b60d4eb7388
- Parent:
- 1:44dcf262c7dd
KBrat-SSD541-Midterm-Q2a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:04499bc54bee | 1 | #include "mbed.h" |
tisbrat | 2:8b60d4eb7388 | 2 | #include <math.h> |
tisbrat | 2:8b60d4eb7388 | 3 | #include <cmath> |
scohennm | 0:04499bc54bee | 4 | #include "TSISensor.h" |
scohennm | 0:04499bc54bee | 5 | #include "SLCD.h" |
scohennm | 1:44dcf262c7dd | 6 | #define LEDON false |
scohennm | 1:44dcf262c7dd | 7 | #define LEDOFF true |
tisbrat | 2:8b60d4eb7388 | 8 | #define NUMBUTS 2 //two buttons |
tisbrat | 2:8b60d4eb7388 | 9 | #define LBUT PTC12 //left button // port addresses for buttons |
tisbrat | 2:8b60d4eb7388 | 10 | #define RBUT PTC3 //right button |
tisbrat | 2:8b60d4eb7388 | 11 | #define ARGUMENTSTATE 0 //Switch case 0 |
tisbrat | 2:8b60d4eb7388 | 12 | #define ANSWERSTATE 1 //Switch case 1 |
tisbrat | 2:8b60d4eb7388 | 13 | #define LCDTIME 1.0 //LCD Timer 1 sec |
tisbrat | 2:8b60d4eb7388 | 14 | #define TSILIMIT 0.01 |
scohennm | 1:44dcf262c7dd | 15 | #define PRINTDELTA 0.01 |
scohennm | 0:04499bc54bee | 16 | #define LCDCHARLEN 10 |
scohennm | 0:04499bc54bee | 17 | #define DATAINTERVAL 0.1 |
scohennm | 1:44dcf262c7dd | 18 | #define BUTTONTIME 0.1 |
tisbrat | 2:8b60d4eb7388 | 19 | #define PROGNAME "KBrat-SSD541-Midterm-Q2a \nNewton's Method of Square Root\n\r" |
scohennm | 0:04499bc54bee | 20 | |
tisbrat | 2:8b60d4eb7388 | 21 | SLCD slcd;//define LCD display globally define |
scohennm | 0:04499bc54bee | 22 | Serial pc(USBTX, USBRX); |
scohennm | 0:04499bc54bee | 23 | |
tisbrat | 2:8b60d4eb7388 | 24 | Timer LCDTimer; //for reading lcd input state for display |
tisbrat | 2:8b60d4eb7388 | 25 | Timer dataTimer; //for reading data input states(from the slider 1-100) |
tisbrat | 2:8b60d4eb7388 | 26 | Timer ButtonTimer; //for reading button states |
scohennm | 1:44dcf262c7dd | 27 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
scohennm | 0:04499bc54bee | 28 | float tsidata; |
tisbrat | 2:8b60d4eb7388 | 29 | int displayState = ARGUMENTSTATE;//Make initial state ARGUMENTSTATE |
scohennm | 1:44dcf262c7dd | 30 | |
scohennm | 1:44dcf262c7dd | 31 | void initialize_global_vars(){ |
scohennm | 1:44dcf262c7dd | 32 | pc.printf(PROGNAME); |
scohennm | 1:44dcf262c7dd | 33 | // set up DAQ timers |
scohennm | 1:44dcf262c7dd | 34 | ButtonTimer.start(); |
scohennm | 1:44dcf262c7dd | 35 | ButtonTimer.reset(); |
scohennm | 1:44dcf262c7dd | 36 | dataTimer.start(); |
tisbrat | 2:8b60d4eb7388 | 37 | dataTimer.reset(); |
tisbrat | 2:8b60d4eb7388 | 38 | LCDTimer.start(); |
tisbrat | 2:8b60d4eb7388 | 39 | LCDTimer.reset(); |
scohennm | 1:44dcf262c7dd | 40 | } |
scohennm | 0:04499bc54bee | 41 | |
scohennm | 0:04499bc54bee | 42 | void LCDMess(char *lMess){ |
scohennm | 0:04499bc54bee | 43 | slcd.Home(); |
scohennm | 0:04499bc54bee | 44 | slcd.clear(); |
scohennm | 0:04499bc54bee | 45 | slcd.printf(lMess); |
scohennm | 0:04499bc54bee | 46 | } |
scohennm | 0:04499bc54bee | 47 | |
scohennm | 0:04499bc54bee | 48 | int main(void) { |
scohennm | 1:44dcf262c7dd | 49 | int i; |
scohennm | 0:04499bc54bee | 50 | char lcdData[LCDCHARLEN]; |
tisbrat | 2:8b60d4eb7388 | 51 | float lastTouch = 0.0; //intial lastTouch starts at 0.0 |
scohennm | 1:44dcf262c7dd | 52 | TSISensor tsi; |
scohennm | 1:44dcf262c7dd | 53 | float tempTSI; |
scohennm | 0:04499bc54bee | 54 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 55 | PwmOut rled(LED_RED); |
scohennm | 1:44dcf262c7dd | 56 | |
scohennm | 1:44dcf262c7dd | 57 | initialize_global_vars(); |
scohennm | 0:04499bc54bee | 58 | |
scohennm | 0:04499bc54bee | 59 | while (true) { |
scohennm | 1:44dcf262c7dd | 60 | if (ButtonTimer > BUTTONTIME){ |
tisbrat | 2:8b60d4eb7388 | 61 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 //find buttons |
scohennm | 1:44dcf262c7dd | 62 | if(!buttons[i]) { |
tisbrat | 2:8b60d4eb7388 | 63 | displayState = i; |
scohennm | 1:44dcf262c7dd | 64 | } // if ! buttons |
scohennm | 1:44dcf262c7dd | 65 | }// for loop to look at buttons |
scohennm | 1:44dcf262c7dd | 66 | ButtonTimer.reset(); |
tisbrat | 2:8b60d4eb7388 | 67 | } |
tisbrat | 2:8b60d4eb7388 | 68 | |
tisbrat | 2:8b60d4eb7388 | 69 | |
tisbrat | 2:8b60d4eb7388 | 70 | if(LCDTimer.read() > LCDTIME){ |
tisbrat | 2:8b60d4eb7388 | 71 | LCDTimer.reset(); |
tisbrat | 2:8b60d4eb7388 | 72 | switch (displayState){//start switch case for displayState |
tisbrat | 2:8b60d4eb7388 | 73 | |
tisbrat | 2:8b60d4eb7388 | 74 | case ARGUMENTSTATE: {// case #0 |
tisbrat | 2:8b60d4eb7388 | 75 | rled = 0.0;//red light on |
tisbrat | 2:8b60d4eb7388 | 76 | gled = 1.0;//green light off |
tisbrat | 2:8b60d4eb7388 | 77 | |
tisbrat | 2:8b60d4eb7388 | 78 | if(dataTimer.read() > DATAINTERVAL){ |
tisbrat | 2:8b60d4eb7388 | 79 | dataTimer.reset(); |
tisbrat | 2:8b60d4eb7388 | 80 | tempTSI = tsi.readPercentage(); |
tisbrat | 2:8b60d4eb7388 | 81 | if (tempTSI > TSILIMIT){ |
tisbrat | 2:8b60d4eb7388 | 82 | tsidata = tempTSI; |
tisbrat | 2:8b60d4eb7388 | 83 | if (fabs(tsidata - lastTouch)> PRINTDELTA){ |
tisbrat | 2:8b60d4eb7388 | 84 | pc.printf("Position %2.0f\n\r", tsidata*100);//print to computer tsidata*100 to get a range from 1-100 |
tisbrat | 2:8b60d4eb7388 | 85 | } |
tisbrat | 2:8b60d4eb7388 | 86 | } |
tisbrat | 2:8b60d4eb7388 | 87 | lastTouch = tsidata; |
tisbrat | 2:8b60d4eb7388 | 88 | } |
tisbrat | 2:8b60d4eb7388 | 89 | |
tisbrat | 2:8b60d4eb7388 | 90 | sprintf (lcdData,"%2.1f",tsidata*100); //print to lcd screen tsidata*100 to get a range from 1-100 |
tisbrat | 2:8b60d4eb7388 | 91 | LCDMess(lcdData); |
tisbrat | 2:8b60d4eb7388 | 92 | |
tisbrat | 2:8b60d4eb7388 | 93 | break; |
tisbrat | 2:8b60d4eb7388 | 94 | } |
tisbrat | 2:8b60d4eb7388 | 95 | |
tisbrat | 2:8b60d4eb7388 | 96 | case ANSWERSTATE: { |
tisbrat | 2:8b60d4eb7388 | 97 | |
tisbrat | 2:8b60d4eb7388 | 98 | rled = 1.0;//red light off |
tisbrat | 2:8b60d4eb7388 | 99 | gled = 0.0;//green light on |
tisbrat | 2:8b60d4eb7388 | 100 | |
tisbrat | 2:8b60d4eb7388 | 101 | /*-------With built in sqrt function-----*/ |
tisbrat | 2:8b60d4eb7388 | 102 | /*double slid_input, sq_root; |
tisbrat | 2:8b60d4eb7388 | 103 | slid_input = tsidata*100; |
tisbrat | 2:8b60d4eb7388 | 104 | if (slid_input > 0){ |
tisbrat | 2:8b60d4eb7388 | 105 | sq_root = sqrt(slid_input); |
tisbrat | 2:8b60d4eb7388 | 106 | pc.printf ("Square root(%f) = %f\n", slid_input, sq_root); |
tisbrat | 2:8b60d4eb7388 | 107 | sprintf (lcdData,"%2.2f", sq_root); |
tisbrat | 2:8b60d4eb7388 | 108 | LCDMess(lcdData); |
tisbrat | 2:8b60d4eb7388 | 109 | //return 0; |
tisbrat | 2:8b60d4eb7388 | 110 | }*/ |
tisbrat | 2:8b60d4eb7388 | 111 | |
tisbrat | 2:8b60d4eb7388 | 112 | /*-------Without built in sqrt function-----*/ |
tisbrat | 2:8b60d4eb7388 | 113 | double x1, slid_input, sq_root; |
tisbrat | 2:8b60d4eb7388 | 114 | slid_input = tsidata*100; |
tisbrat | 2:8b60d4eb7388 | 115 | if(slid_input > 0){ |
tisbrat | 2:8b60d4eb7388 | 116 | sq_root = log(slid_input); |
tisbrat | 2:8b60d4eb7388 | 117 | do { |
tisbrat | 2:8b60d4eb7388 | 118 | x1 = (sq_root - (((sq_root * sq_root) - slid_input)/(2* sq_root))); //Newton's Method |
tisbrat | 2:8b60d4eb7388 | 119 | sq_root = x1; |
tisbrat | 2:8b60d4eb7388 | 120 | |
tisbrat | 2:8b60d4eb7388 | 121 | } while ((x1 * x1) > slid_input); |
tisbrat | 2:8b60d4eb7388 | 122 | pc.printf ("Newton's Method: Square root(%2.2f) = %2.2f\n", slid_input, sq_root); |
tisbrat | 2:8b60d4eb7388 | 123 | sprintf (lcdData,"%2.2f", sq_root); |
tisbrat | 2:8b60d4eb7388 | 124 | LCDMess(lcdData); |
tisbrat | 2:8b60d4eb7388 | 125 | } |
tisbrat | 2:8b60d4eb7388 | 126 | break; |
tisbrat | 2:8b60d4eb7388 | 127 | }// end switch case displayState |
tisbrat | 2:8b60d4eb7388 | 128 | } |
scohennm | 1:44dcf262c7dd | 129 | LCDMess(lcdData); |
tisbrat | 2:8b60d4eb7388 | 130 | } // end LCD timer.read |
scohennm | 1:44dcf262c7dd | 131 | |
tisbrat | 2:8b60d4eb7388 | 132 | }// end while(true) |
tisbrat | 2:8b60d4eb7388 | 133 | |
scohennm | 0:04499bc54bee | 134 | } |