KBrat-SSD541-Midterm-Q2b

Dependencies:   SLCD TSI mbed

Fork of kl46z_slider_mid_v1 by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
tisbrat
Date:
Mon Oct 10 05:57:45 2016 +0000
Parent:
1:44dcf262c7dd
Commit message:
KBrat-SSD541-Midterm-Q2b

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 44dcf262c7dd -r 6d82d759361f main.cpp
--- a/main.cpp	Sat Oct 01 22:10:10 2016 +0000
+++ b/main.cpp	Mon Oct 10 05:57:45 2016 +0000
@@ -1,30 +1,33 @@
 #include "mbed.h"
-#include <math.h>  
+#include <math.h>
+#include <cmath>
 #include "TSISensor.h"
 #include "SLCD.h"
-
 #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 NUMBUTS 2   //two buttons
+#define LBUT PTC12  //left button // port addresses for buttons
+#define RBUT PTC3 //right button
+#define ARGUMENTSTATE 0 //Switch case 0
+#define ANSWERSTATE 1 //Switch case 1
+#define LCDTIME 1.0 //LCD Timer 1 sec
 #define TSILIMIT 0.01
 #define PRINTDELTA 0.01
 #define LCDCHARLEN 10
 #define DATAINTERVAL 0.1
 #define BUTTONTIME 0.1
-#define PROGNAME "kl46z_slider_mid_v1\n\r"
+#define PROGNAME "KBrat-SSD541-Midterm-Q2b \nCubic Root\n\r"
 
-SLCD slcd; //define LCD display
+SLCD slcd; //define LCD display globally define
 Serial pc(USBTX, USBRX);
 
-Timer dataTimer;
-Timer ButtonTimer; // for reading button states
+Timer LCDTimer; //for reading lcd input state for display
+Timer dataTimer; //for reading data input states(from the slider 1-100)
+Timer ButtonTimer; //for reading button states
 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
 float tsidata;
-int displayState;
+//int displayState;
+int displayState = ARGUMENTSTATE;//Make initial state ARGUMENTSTATE
 
 void initialize_global_vars(){
     pc.printf(PROGNAME);
@@ -32,7 +35,9 @@
     ButtonTimer.start();
     ButtonTimer.reset();
     dataTimer.start();
-    dataTimer.reset(); 
+    dataTimer.reset();
+    LCDTimer.start();
+    LCDTimer.reset();  
 } 
 
 void LCDMess(char *lMess){
@@ -44,7 +49,7 @@
 int main(void) {
     int i;
     char lcdData[LCDCHARLEN];
-    float lastTouch = 0.0;
+    float lastTouch = 0.0; //intial lastTouch starts at 0.0
     TSISensor tsi;
     float tempTSI;
     PwmOut gled(LED_GREEN);
@@ -54,30 +59,69 @@
 
      while (true) {
         if (ButtonTimer > BUTTONTIME){
-            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
+            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 //find buttons
                 if(!buttons[i]) { 
                     displayState = i;
-                    // do something here.
-                    
                 } // if ! buttons
             }// for loop to look at buttons
             ButtonTimer.reset();
-            sprintf (lcdData,"%0.4f",tsidata);  
+        }
+        
+        
+        if(LCDTimer.read() > LCDTIME){
+            LCDTimer.reset();                               
+            switch (displayState){ //start switch case for displayState 
+                
+                case ARGUMENTSTATE: { // case #0
+                    rled = 0.0;//red light on
+                    gled = 1.0;//green light off
+
+                    if(dataTimer.read() > DATAINTERVAL){
+                        dataTimer.reset();                               
+                        tempTSI = tsi.readPercentage();        
+                        if (tempTSI > TSILIMIT){
+                            tsidata = tempTSI;
+                            if (fabs(tsidata - lastTouch)> PRINTDELTA){
+                                //int randomNum = rand() % 101 + (-50);
+                                //tsidata = tsidata*randomNum;
+                                //if(tsidata < 0.0)
+                                //pc.printf("Position z%2.1f", fabs(tsidata));
+                                pc.printf("Position %2.0f\n\r", tsidata*50); //print to computer tsidata*50 to get a range from 1-50
+                                
+                            }           
+                        }
+                        lastTouch=tsidata;
+                    }
+        
+                    sprintf (lcdData,"%2.1f",tsidata*50); //print to lcd screen tsidata*50 to get a range from 1-50
+                    LCDMess(lcdData); 
+                    
+                    /*sprintf (lcdData,"%2.1f",tsidata*50)&
+                    sprintf (lcdData,"%2.1f",tsidata*(0-50));
+                    if(tsidata < 0.0) sprint (lcdData,"z%2.1f", fabs(tsidata*(0-50));
+                    LCDMess(lcdData);*/
+
+                break;
+                }
+                
+                case ANSWERSTATE: {
+                    
+                    rled = 1.0;//red light off
+                    gled = 0.0;//green light on
+                    
+                    double slid_input, cub_root;
+                    slid_input = tsidata*50;
+                    if (slid_input > 0){
+                    cub_root = cbrt(slid_input);//built-in cubic root function
+                    pc.printf ("Cubic root(%2.2f) = %2.2f\n", slid_input, cub_root);
+                    sprintf (lcdData,"%2.2f", cub_root);
+                    }
+                    break;
+                }// end switch displaystate
+            }                 
             LCDMess(lcdData); 
-            rled = 0.0;
-            gled = 1.0;
+        } // end LCD timer.read
             
-        }
-        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;
-        }
-    }
+    }// end while(true)
+        
 }
\ No newline at end of file