AnnaLouise Martinez
/
kl46z_slider_recursive_hw12_2
Recursion 12.2
Fork of kl46z_slider_mid_v1_amart by
main.cpp@2:bb868c525c5c, 2016-10-10 (annotated)
- Committer:
- annalou
- Date:
- Mon Oct 10 04:04:39 2016 +0000
- Revision:
- 2:bb868c525c5c
- Parent:
- 1:44dcf262c7dd
- Child:
- 3:2bb5ccf119fc
AMart_SSD341_Mid
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:04499bc54bee | 1 | #include "mbed.h" |
scohennm | 1:44dcf262c7dd | 2 | #include <math.h> |
scohennm | 0:04499bc54bee | 3 | #include "TSISensor.h" |
scohennm | 0:04499bc54bee | 4 | #include "SLCD.h" |
scohennm | 1:44dcf262c7dd | 5 | |
scohennm | 1:44dcf262c7dd | 6 | #define LEDON false |
scohennm | 1:44dcf262c7dd | 7 | #define LEDOFF true |
scohennm | 1:44dcf262c7dd | 8 | #define NUMBUTS 2 |
scohennm | 1:44dcf262c7dd | 9 | #define LBUT PTC12 // port addresses for buttons |
scohennm | 1:44dcf262c7dd | 10 | #define RBUT PTC3 |
scohennm | 1:44dcf262c7dd | 11 | #define ARGUMENTSTATE 0 |
scohennm | 1:44dcf262c7dd | 12 | #define ANSWERSTATE 1 |
scohennm | 1:44dcf262c7dd | 13 | #define TSILIMIT 0.01 |
scohennm | 1:44dcf262c7dd | 14 | #define PRINTDELTA 0.01 |
scohennm | 0:04499bc54bee | 15 | #define LCDCHARLEN 10 |
scohennm | 0:04499bc54bee | 16 | #define DATAINTERVAL 0.1 |
scohennm | 1:44dcf262c7dd | 17 | #define BUTTONTIME 0.1 |
annalou | 2:bb868c525c5c | 18 | #define PROGNAME "SquareRootMidterm_AnnaLouise Martinez\n\r" |
scohennm | 0:04499bc54bee | 19 | |
scohennm | 0:04499bc54bee | 20 | SLCD slcd; //define LCD display |
scohennm | 0:04499bc54bee | 21 | Serial pc(USBTX, USBRX); |
scohennm | 0:04499bc54bee | 22 | |
scohennm | 1:44dcf262c7dd | 23 | Timer dataTimer; |
scohennm | 1:44dcf262c7dd | 24 | Timer ButtonTimer; // for reading button states |
scohennm | 1:44dcf262c7dd | 25 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
scohennm | 0:04499bc54bee | 26 | float tsidata; |
scohennm | 1:44dcf262c7dd | 27 | int displayState; |
scohennm | 1:44dcf262c7dd | 28 | |
scohennm | 1:44dcf262c7dd | 29 | void initialize_global_vars(){ |
scohennm | 1:44dcf262c7dd | 30 | pc.printf(PROGNAME); |
scohennm | 1:44dcf262c7dd | 31 | // set up DAQ timers |
scohennm | 1:44dcf262c7dd | 32 | ButtonTimer.start(); |
scohennm | 1:44dcf262c7dd | 33 | ButtonTimer.reset(); |
scohennm | 1:44dcf262c7dd | 34 | dataTimer.start(); |
scohennm | 1:44dcf262c7dd | 35 | dataTimer.reset(); |
scohennm | 1:44dcf262c7dd | 36 | } |
scohennm | 0:04499bc54bee | 37 | |
scohennm | 0:04499bc54bee | 38 | void LCDMess(char *lMess){ |
scohennm | 0:04499bc54bee | 39 | slcd.Home(); |
scohennm | 0:04499bc54bee | 40 | slcd.clear(); |
scohennm | 0:04499bc54bee | 41 | slcd.printf(lMess); |
scohennm | 0:04499bc54bee | 42 | } |
scohennm | 0:04499bc54bee | 43 | |
annalou | 2:bb868c525c5c | 44 | float sqroot(float tsiData) |
annalou | 2:bb868c525c5c | 45 | { |
annalou | 2:bb868c525c5c | 46 | // Newton's method for square root |
annalou | 2:bb868c525c5c | 47 | float xnew = 0.0; |
annalou | 2:bb868c525c5c | 48 | int intmax = 20; |
annalou | 2:bb868c525c5c | 49 | float epsilon = 1e-7; |
annalou | 2:bb868c525c5c | 50 | float xold = float(tsiData/2.5); |
annalou | 2:bb868c525c5c | 51 | |
annalou | 2:bb868c525c5c | 52 | for(int i =0; i< intmax; i++) |
annalou | 2:bb868c525c5c | 53 | { |
annalou | 2:bb868c525c5c | 54 | xnew = 0.5*(xold + ((tsiData)/xold)); // Calculation |
annalou | 2:bb868c525c5c | 55 | float delta = abs(xnew-xold); // Compare old and new values |
annalou | 2:bb868c525c5c | 56 | |
annalou | 2:bb868c525c5c | 57 | if (delta < epsilon) //Check for convergence |
annalou | 2:bb868c525c5c | 58 | { |
annalou | 2:bb868c525c5c | 59 | break; |
annalou | 2:bb868c525c5c | 60 | } |
annalou | 2:bb868c525c5c | 61 | else |
annalou | 2:bb868c525c5c | 62 | { |
annalou | 2:bb868c525c5c | 63 | xold = xnew; //replace new calculated value to redo the calculation |
annalou | 2:bb868c525c5c | 64 | } |
annalou | 2:bb868c525c5c | 65 | } |
annalou | 2:bb868c525c5c | 66 | //float newtsiData = tsiData * 100; |
annalou | 2:bb868c525c5c | 67 | pc.printf("The square root of "); |
annalou | 2:bb868c525c5c | 68 | pc.printf("%0.4f", tsiData); |
annalou | 2:bb868c525c5c | 69 | pc.printf(" is %0.4f\n\r", xnew); |
annalou | 2:bb868c525c5c | 70 | return xnew; |
annalou | 2:bb868c525c5c | 71 | } |
annalou | 2:bb868c525c5c | 72 | |
scohennm | 0:04499bc54bee | 73 | int main(void) { |
scohennm | 1:44dcf262c7dd | 74 | int i; |
scohennm | 0:04499bc54bee | 75 | char lcdData[LCDCHARLEN]; |
scohennm | 1:44dcf262c7dd | 76 | float lastTouch = 0.0; |
scohennm | 1:44dcf262c7dd | 77 | TSISensor tsi; |
scohennm | 1:44dcf262c7dd | 78 | float tempTSI; |
scohennm | 0:04499bc54bee | 79 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 80 | PwmOut rled(LED_RED); |
scohennm | 1:44dcf262c7dd | 81 | |
scohennm | 1:44dcf262c7dd | 82 | initialize_global_vars(); |
scohennm | 0:04499bc54bee | 83 | |
scohennm | 0:04499bc54bee | 84 | while (true) { |
scohennm | 1:44dcf262c7dd | 85 | if (ButtonTimer > BUTTONTIME){ |
scohennm | 1:44dcf262c7dd | 86 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
scohennm | 1:44dcf262c7dd | 87 | if(!buttons[i]) { |
scohennm | 1:44dcf262c7dd | 88 | displayState = i; |
scohennm | 1:44dcf262c7dd | 89 | // do something here. |
scohennm | 1:44dcf262c7dd | 90 | |
scohennm | 1:44dcf262c7dd | 91 | } // if ! buttons |
scohennm | 1:44dcf262c7dd | 92 | }// for loop to look at buttons |
scohennm | 1:44dcf262c7dd | 93 | ButtonTimer.reset(); |
scohennm | 1:44dcf262c7dd | 94 | sprintf (lcdData,"%0.4f",tsidata); |
scohennm | 1:44dcf262c7dd | 95 | LCDMess(lcdData); |
scohennm | 0:04499bc54bee | 96 | rled = 0.0; |
scohennm | 1:44dcf262c7dd | 97 | gled = 1.0; |
scohennm | 0:04499bc54bee | 98 | } |
annalou | 2:bb868c525c5c | 99 | |
scohennm | 1:44dcf262c7dd | 100 | if(dataTimer.read() > DATAINTERVAL){ |
scohennm | 1:44dcf262c7dd | 101 | dataTimer.reset(); |
annalou | 2:bb868c525c5c | 102 | tempTSI = tsi.readPercentage(); |
scohennm | 1:44dcf262c7dd | 103 | if (tempTSI > TSILIMIT){ |
scohennm | 1:44dcf262c7dd | 104 | tsidata = tempTSI; |
scohennm | 1:44dcf262c7dd | 105 | if (fabs(tsidata - lastTouch)> PRINTDELTA){ |
scohennm | 1:44dcf262c7dd | 106 | pc.printf("Position %0.4f\n\r", tsidata); |
annalou | 2:bb868c525c5c | 107 | float sqrt = sqroot(tsidata * 100); |
scohennm | 1:44dcf262c7dd | 108 | } |
scohennm | 1:44dcf262c7dd | 109 | } |
scohennm | 1:44dcf262c7dd | 110 | lastTouch=tsidata; |
scohennm | 1:44dcf262c7dd | 111 | } |
scohennm | 0:04499bc54bee | 112 | } |
scohennm | 0:04499bc54bee | 113 | } |