midtermDorianFolie

Dependencies:   SLCD TSI mbed

Fork of kl46z_slider_mid_v1 by Stanley Cohen

Committer:
dorian505
Date:
Mon Oct 10 02:42:05 2016 +0000
Revision:
2:64f0521a1213
Parent:
1:44dcf262c7dd
midtermDorianFolie

Who changed what in which revision?

UserRevisionLine numberNew 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
scohennm 1:44dcf262c7dd 18 #define PROGNAME "kl46z_slider_mid_v1\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
dorian505 2:64f0521a1213 29 float newtonSqrt(float argument){
dorian505 2:64f0521a1213 30 #define MAXITER 20
dorian505 2:64f0521a1213 31 int i = 0;
dorian505 2:64f0521a1213 32 float xnew = 0.0;
dorian505 2:64f0521a1213 33 int itermax = MAXITER;
dorian505 2:64f0521a1213 34 float epsilon = EPSILONC;
dorian505 2:64f0521a1213 35 float xold = argument/2.0;
dorian505 2:64f0521a1213 36 float delta = 1;
dorian505 2:64f0521a1213 37 intmax = 20
dorian505 2:64f0521a1213 38 epsilon = 1e-7
dorian505 2:64f0521a1213 39 #xold = float(a/2.5)# make a guess5
dorian505 2:64f0521a1213 40
dorian505 2:64f0521a1213 41 for interations in range(0,intmax):
dorian505 2:64f0521a1213 42 xnew = 0.5*(xold + (a/xold)) # Calculation
dorian505 2:64f0521a1213 43 delta = abs(xnew-xold) # Compare old and new values
dorian505 2:64f0521a1213 44
dorian505 2:64f0521a1213 45 if delta < epsilon: # Check for convergence
dorian505 2:64f0521a1213 46 break
dorian505 2:64f0521a1213 47 else:
dorian505 2:64f0521a1213 48 xold = xnew # replace new calculated value to redo the calculation
dorian505 2:64f0521a1213 49
dorian505 2:64f0521a1213 50 return(xnew);
dorian505 2:64f0521a1213 51 }
dorian505 2:64f0521a1213 52
scohennm 1:44dcf262c7dd 53 void initialize_global_vars(){
scohennm 1:44dcf262c7dd 54 pc.printf(PROGNAME);
scohennm 1:44dcf262c7dd 55 // set up DAQ timers
scohennm 1:44dcf262c7dd 56 ButtonTimer.start();
scohennm 1:44dcf262c7dd 57 ButtonTimer.reset();
scohennm 1:44dcf262c7dd 58 dataTimer.start();
scohennm 1:44dcf262c7dd 59 dataTimer.reset();
scohennm 1:44dcf262c7dd 60 }
scohennm 0:04499bc54bee 61
scohennm 0:04499bc54bee 62 void LCDMess(char *lMess){
scohennm 0:04499bc54bee 63 slcd.Home();
scohennm 0:04499bc54bee 64 slcd.clear();
scohennm 0:04499bc54bee 65 slcd.printf(lMess);
scohennm 0:04499bc54bee 66 }
scohennm 0:04499bc54bee 67
scohennm 0:04499bc54bee 68 int main(void) {
scohennm 1:44dcf262c7dd 69 int i;
scohennm 0:04499bc54bee 70 char lcdData[LCDCHARLEN];
scohennm 1:44dcf262c7dd 71 float lastTouch = 0.0;
scohennm 1:44dcf262c7dd 72 TSISensor tsi;
scohennm 1:44dcf262c7dd 73 float tempTSI;
scohennm 0:04499bc54bee 74 PwmOut gled(LED_GREEN);
scohennm 0:04499bc54bee 75 PwmOut rled(LED_RED);
scohennm 1:44dcf262c7dd 76
scohennm 1:44dcf262c7dd 77 initialize_global_vars();
scohennm 0:04499bc54bee 78
scohennm 0:04499bc54bee 79 while (true) {
scohennm 1:44dcf262c7dd 80 if (ButtonTimer > BUTTONTIME){
scohennm 1:44dcf262c7dd 81 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1
scohennm 1:44dcf262c7dd 82 if(!buttons[i]) {
scohennm 1:44dcf262c7dd 83 displayState = i;
scohennm 1:44dcf262c7dd 84 // do something here.
scohennm 1:44dcf262c7dd 85
scohennm 1:44dcf262c7dd 86 } // if ! buttons
scohennm 1:44dcf262c7dd 87 }// for loop to look at buttons
scohennm 1:44dcf262c7dd 88 ButtonTimer.reset();
dorian505 2:64f0521a1213 89 lcdArgument = tsidata * 100;
scohennm 1:44dcf262c7dd 90 sprintf (lcdData,"%0.4f",tsidata);
scohennm 1:44dcf262c7dd 91 LCDMess(lcdData);
scohennm 0:04499bc54bee 92 rled = 0.0;
scohennm 1:44dcf262c7dd 93 gled = 1.0;
scohennm 1:44dcf262c7dd 94
scohennm 0:04499bc54bee 95 }
scohennm 1:44dcf262c7dd 96 if(dataTimer.read() > DATAINTERVAL){
scohennm 1:44dcf262c7dd 97 dataTimer.reset();
scohennm 1:44dcf262c7dd 98 tempTSI = tsi.readPercentage();
scohennm 1:44dcf262c7dd 99 if (tempTSI > TSILIMIT){
scohennm 1:44dcf262c7dd 100 tsidata = tempTSI;
scohennm 1:44dcf262c7dd 101 if (fabs(tsidata - lastTouch)> PRINTDELTA){
scohennm 1:44dcf262c7dd 102 pc.printf("Position %0.4f\n\r", tsidata);
scohennm 1:44dcf262c7dd 103 }
scohennm 1:44dcf262c7dd 104 }
scohennm 1:44dcf262c7dd 105 lastTouch=tsidata;
scohennm 1:44dcf262c7dd 106 }
scohennm 0:04499bc54bee 107 }
dorian505 2:64f0521a1213 108 }