KBrat-SSD541-Midterm-Q2a

Dependencies:   SLCD TSI mbed

Fork of kl46z_slider_mid_v1 by Stanley Cohen

Revision:
1:44dcf262c7dd
Parent:
0:04499bc54bee
Child:
2:8b60d4eb7388
--- a/main.cpp	Fri Sep 09 17:51:13 2016 +0000
+++ b/main.cpp	Sat Oct 01 22:10:10 2016 +0000
@@ -1,15 +1,39 @@
 #include "mbed.h"
+#include <math.h>  
 #include "TSISensor.h"
 #include "SLCD.h"
-#define TSILIMIT 0.99
+
+#define LEDON false
+#define LEDOFF true
+#define NUMBUTS 2
+#define LBUT PTC12  // port addresses for buttons
+#define RBUT PTC3
+#define ARGUMENTSTATE 0
+#define ANSWERSTATE 1
+#define TSILIMIT 0.01
+#define PRINTDELTA 0.01
 #define LCDCHARLEN 10
 #define DATAINTERVAL 0.1
-#define PROGNAME "kl46z_slider_test_v1\n\r"
+#define BUTTONTIME 0.1
+#define PROGNAME "kl46z_slider_mid_v1\n\r"
 
 SLCD slcd; //define LCD display
 Serial pc(USBTX, USBRX);
 
+Timer dataTimer;
+Timer ButtonTimer; // for reading button states
+DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
 float tsidata;
+int displayState;
+
+void initialize_global_vars(){
+    pc.printf(PROGNAME);
+    // set up DAQ timers
+    ButtonTimer.start();
+    ButtonTimer.reset();
+    dataTimer.start();
+    dataTimer.reset(); 
+} 
 
 void LCDMess(char *lMess){
         slcd.Home();
@@ -18,24 +42,42 @@
 }
 
 int main(void) {
+    int i;
     char lcdData[LCDCHARLEN];
+    float lastTouch = 0.0;
+    TSISensor tsi;
+    float tempTSI;
     PwmOut gled(LED_GREEN);
     PwmOut rled(LED_RED);
-    pc.printf(PROGNAME);
-    TSISensor tsi;
+    
+    initialize_global_vars();
 
      while (true) {
-        tsidata = tsi.readPercentage();
-        if (tsidata > TSILIMIT){
-            gled = 0.0;
+        if (ButtonTimer > BUTTONTIME){
+            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
+                if(!buttons[i]) { 
+                    displayState = i;
+                    // do something here.
+                    
+                } // if ! buttons
+            }// for loop to look at buttons
+            ButtonTimer.reset();
+            sprintf (lcdData,"%0.4f",tsidata);  
+            LCDMess(lcdData); 
             rled = 0.0;
-        }else {
-            pc.printf("\n Position %f\n\r", tsidata);
-            sprintf (lcdData,"%0.4f",tsidata);  
-            LCDMess(lcdData);  
-            gled = tsidata;
-            rled = 1.0 - tsidata;
+            gled = 1.0;
+            
         }
-            wait(DATAINTERVAL);
+        if(dataTimer.read() > DATAINTERVAL){
+            dataTimer.reset();                               
+            tempTSI = tsi.readPercentage();        
+            if (tempTSI > TSILIMIT){
+                tsidata = tempTSI;
+                if (fabs(tsidata - lastTouch)> PRINTDELTA){
+                    pc.printf("Position %0.4f\n\r", tsidata);
+                }           
+            }
+            lastTouch=tsidata;
+        }
     }
 }
\ No newline at end of file