HW_5.2

Dependencies:   SLCD mbed

Fork of lightsense_kl46z_basic by Stanley Cohen

Revision:
8:3b19ecdc4261
Parent:
7:8d7089b514ae
Child:
9:f619cdaa7a65
diff -r 8d7089b514ae -r 3b19ecdc4261 main.cpp
--- a/main.cpp	Wed Sep 17 18:20:10 2014 +0000
+++ b/main.cpp	Fri Sep 09 19:31:08 2016 +0000
@@ -2,30 +2,16 @@
 #include "SLCD.h"
 
 
-// An State Machine solution to HW 4.1 Q3
-// using wait() for high and low limits.
-//#define PRINTDEBUG
-#define PROGNAME "blink_kl46z_states nowait v1\n\r"
-#define LEDON false
-#define LEDOFF true
-#define LEDTMESS "TRUE"
-#define LEDFMESS "FALS"
-#define BLINKDWELL 400 // milliseconds
-#define LIMITSTAYON 1000
-#define DFDELTA 0.01
-#define PWMTIME 1 // ms (kHz
+#define PROGNAME "lightsense_kl46z_minmax v1\n\r"
+#define DATATIME 400 // milliseconds
 #define LCDLEN 10
-#define NUMSTATES 2
-#define LOWLIMIT 0.1
-#define HILIMIT 0.85
-#define ONTIME 1.0
+#define LIGHTSENSORPORT PTE22
 
 
-enum blinkStates {IDLESTATE, NEWBLINK, LOWLITE, HILITE}; // define the states
 
-char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS};
 SLCD slcd; //define LCD display globally define
 Serial pc(USBTX, USBRX);
+Timer LEDTimer;
 
 void LCDMess(char *lMess){
         slcd.Home();
@@ -34,80 +20,33 @@
 } 
 
 int main() {
-    DigitalOut greenColor(LED_GREEN);
-    DigitalOut redColor(LED_RED);
-    AnalogIn LightSensor(PTE22);
+    float maxLight = 0;
+    float minLight = 1.0;
+    PwmOut greenColor(LED_GREEN);
+    PwmOut redColor(LED_RED);
+    AnalogIn LightSensor(LIGHTSENSORPORT);
     float lightData; 
     char lcdData[LCDLEN];
-    Timer LEDTimer;  // time till next PWM values is to change.
-    blinkStates PGMState = IDLESTATE; // work till timer transitions
-    int ledState = LEDON;  
-    
    
-    int timeToChangeDF = BLINKDWELL;
+    int timeToChangeDF = DATATIME;
     // set up timer for next step of Duty Factor timing
     LEDTimer.start();
     LEDTimer.reset();
     pc.printf(PROGNAME);
     
     
-
-    while(true) {
-        switch (PGMState){ 
-            case IDLESTATE: {
-                if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
-                    lightData = (1.0 - LightSensor.read());
-                    sprintf(lcdData,"%4.3f",lightData);
-                    LCDMess(lcdData);  
-                    timeToChangeDF = BLINKDWELL;
-                    if (lightData < LOWLIMIT){ // find appropriate state
-                        PGMState = LOWLITE;
-                        timeToChangeDF = LIMITSTAYON;
-                    } else if (lightData > HILIMIT){
-                        PGMState = HILITE;
-                        timeToChangeDF = LIMITSTAYON;
-                    } else {
-                        PGMState = NEWBLINK;
-                        timeToChangeDF = BLINKDWELL;
-                    }                      
-                    
-                }
-                break;
-              }
-             case  NEWBLINK: {              
-                ledState = !ledState;
-                redColor.write(ledState);
-                greenColor.write(!ledState);
-                // create string for display on LCD
-                             
-                LEDTimer.reset();  // reset the timer
-                PGMState = IDLESTATE;  // go idle state
-                break;
-              }
-             case HILITE: {              
-                redColor.write(LEDON);
-                greenColor.write(LEDOFF);
-                // create string for display on LCD
-                if (LEDTimer.read_ms() > timeToChangeDF){     
-                    LEDTimer.reset();  // reset the timer
-                    PGMState = IDLESTATE;  // go idle state
-                }
-                break;
-              }
-             case LOWLITE: {              
-                redColor.write(LEDOFF);
-                greenColor.write(LEDON);
-                // create string for display on LCD
-                if (LEDTimer.read_ms() > timeToChangeDF){     
-                    LEDTimer.reset();  // reset the timer
-                    PGMState = IDLESTATE;  // go idle state
-                }
-                break;
-              }
-        } // end state machine
-        
-#ifdef PRINTDEBUG
-        pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r",  i, dutyFactor, workingDelta); 
-#endif
+    while(true) {    
+        if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
+            lightData = (1.0 - LightSensor.read()); // show as increasiing with increasing intensity
+            greenColor.write(1.0- lightData);
+            redColor.write(1.0-lightData);
+            sprintf(lcdData,"%4.3f",lightData);
+            LCDMess(lcdData);  
+            if(lightData > maxLight) maxLight = lightData;
+            if(lightData < minLight) minLight = lightData;
+            pc.printf("%4.3f min = %4.3f max = %4.3f\r\n",lightData, minLight, maxLight);
+            timeToChangeDF = DATATIME;
+            LEDTimer.reset();
+        }        
     }// emd while
 }