midtermDorianFolie

Dependencies:   SLCD TSI mbed

Fork of kl46z_slider_mid_v1 by Stanley Cohen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <math.h>  
00003 #include "TSISensor.h"
00004 #include "SLCD.h"
00005 
00006 #define LEDON false
00007 #define LEDOFF true
00008 #define NUMBUTS 2
00009 #define LBUT PTC12  // port addresses for buttons
00010 #define RBUT PTC3
00011 #define ARGUMENTSTATE 0
00012 #define ANSWERSTATE 1
00013 #define TSILIMIT 0.01
00014 #define PRINTDELTA 0.01
00015 #define LCDCHARLEN 10
00016 #define DATAINTERVAL 0.1
00017 #define BUTTONTIME 0.1
00018 #define PROGNAME "kl46z_slider_mid_v1\n\r"
00019 
00020 SLCD slcd; //define LCD display
00021 Serial pc(USBTX, USBRX);
00022 
00023 Timer dataTimer;
00024 Timer ButtonTimer; // for reading button states
00025 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
00026 float tsidata;
00027 int displayState;
00028 
00029 float newtonSqrt(float argument){
00030 #define MAXITER 20
00031     int i = 0;
00032     float xnew = 0.0;
00033     int itermax = MAXITER;
00034     float epsilon = EPSILONC;
00035     float xold = argument/2.0;
00036     float delta = 1;
00037     intmax = 20
00038     epsilon = 1e-7
00039     #xold = float(a/2.5)# make a guess5
00040 
00041     for interations in range(0,intmax):
00042         xnew = 0.5*(xold + (a/xold)) # Calculation
00043         delta = abs(xnew-xold) # Compare old and new values
00044         
00045         if delta < epsilon: # Check for convergence
00046             break
00047         else:
00048             xold = xnew # replace new calculated value to redo the calculation
00049     
00050    return(xnew);
00051 }
00052 
00053 void initialize_global_vars(){
00054     pc.printf(PROGNAME);
00055     // set up DAQ timers
00056     ButtonTimer.start();
00057     ButtonTimer.reset();
00058     dataTimer.start();
00059     dataTimer.reset(); 
00060 } 
00061 
00062 void LCDMess(char *lMess){
00063         slcd.Home();
00064         slcd.clear();
00065         slcd.printf(lMess);
00066 }
00067 
00068 int main(void) {
00069     int i;
00070     char lcdData[LCDCHARLEN];
00071     float lastTouch = 0.0;
00072     TSISensor tsi;
00073     float tempTSI;
00074     PwmOut gled(LED_GREEN);
00075     PwmOut rled(LED_RED);
00076     
00077     initialize_global_vars();
00078 
00079      while (true) {
00080         if (ButtonTimer > BUTTONTIME){
00081             for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
00082                 if(!buttons[i]) { 
00083                     displayState = i;
00084                     // do something here.
00085                     
00086                 } // if ! buttons
00087             }// for loop to look at buttons
00088             ButtonTimer.reset();
00089             lcdArgument = tsidata * 100;
00090             sprintf (lcdData,"%0.4f",tsidata);  
00091             LCDMess(lcdData); 
00092             rled = 0.0;
00093             gled = 1.0;
00094             
00095         }
00096         if(dataTimer.read() > DATAINTERVAL){
00097             dataTimer.reset();                               
00098             tempTSI = tsi.readPercentage();        
00099             if (tempTSI > TSILIMIT){
00100                 tsidata = tempTSI;
00101                 if (fabs(tsidata - lastTouch)> PRINTDELTA){
00102                     pc.printf("Position %0.4f\n\r", tsidata);
00103                 }           
00104             }
00105             lastTouch=tsidata;
00106         }
00107     }
00108 }