NMHU SSD 341 sieve with light sensor if's for timing

Dependencies:   SLCD mbed

Fork of lightsense_kl46z_states by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
scohennm
Date:
Tue Oct 14 17:01:43 2014 +0000
Parent:
6:710e18c552f5
Commit message:
NMHU SSD 341 sieve with light sensor if's for timing

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Sep 14 23:45:39 2014 +0000
+++ b/main.cpp	Tue Oct 14 17:01:43 2014 +0000
@@ -4,23 +4,33 @@
 
 // An State Machine solution to HW 4.1 Q3
 // using wait() for high and low limits.
+// Use the idea of a sieve modoel instead of a series of if..then..elseif..
 //#define PRINTDEBUG
-#define PROGNAME "blink_kl46z_states v2\n\r"
+#define PROGNAME "blink_kl46z_states seive v2\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 LCDLEN 10
-#define NUMSTATES 2
+#define NUMLIMITS 3
+#define BOTTOMLIMIT 0.0
 #define LOWLIMIT 0.1
 #define HILIMIT 0.85
+#define TOPLIMIT 1.1
 #define ONTIME 1.0
 
 
-enum blinkStates {IDLESTATE, NEWBLINK, LOWLITE, HILITE}; // define the states
+#define NUMSTATES 4
+#define IDLESTATE 0
+#define NEWBLINK 1
+#define LOWLITE 2
+#define HILITE 3
+int stateArray[NUMSTATES] = {HILITE, NEWBLINK, LOWLITE,IDLESTATE};
+float lightLimits[NUMLIMITS] = {HILIMIT, LOWLIMIT, BOTTOMLIMIT};
 
 char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS};
 SLCD slcd; //define LCD display globally define
@@ -39,9 +49,9 @@
     float lightData; 
     char lcdData[LCDLEN];
     Timer LEDTimer;  // time till next PWM values is to change.
-    blinkStates PGMState = IDLESTATE; // work till timer transitions
+    int PGMState = IDLESTATE; // work till timer transitions
     int ledState = LEDON;  
-    
+    int i;
    
     int timeToChangeDF = BLINKDWELL;
     // set up timer for next step of Duty Factor timing
@@ -49,6 +59,8 @@
     LEDTimer.reset();
     pc.printf(PROGNAME);
     
+    greenColor.write(LEDON);
+    redColor.write(LEDON);
     
 
     while(true) {
@@ -58,23 +70,23 @@
                     lightData = (1.0 - LightSensor.read());
                     sprintf(lcdData,"%4.3f",lightData);
                     LCDMess(lcdData);  
-                    if (lightData < LOWLIMIT){ // find appropriate state
-                        PGMState = LOWLITE;
-                    } else if (lightData > HILIMIT){
-                        PGMState = HILITE;
-                    } else {
-                        PGMState = NEWBLINK;
-                    }                      
-                    
+                    timeToChangeDF = BLINKDWELL;
+                    LEDTimer.reset();  // reset the timer
+                    // put data throuth the sieve.
+                    i=0;//start sieve
+                    while((lightData < lightLimits[i])&& (i < NUMLIMITS)){
+                        i++;
+                    }
+                    PGMState = stateArray[i];
                 }
                 break;
               }
              case  NEWBLINK: {              
-                ledState = !ledState;
                 redColor.write(ledState);
                 greenColor.write(!ledState);
+                ledState = !ledState;
                 // create string for display on LCD
-                             
+                timeToChangeDF = BLINKDWELL;            
                 LEDTimer.reset();  // reset the timer
                 PGMState = IDLESTATE;  // go idle state
                 break;
@@ -82,25 +94,27 @@
              case HILITE: {              
                 redColor.write(LEDON);
                 greenColor.write(LEDOFF);
+                timeToChangeDF = LIMITSTAYON;
                 // create string for display on LCD
-                wait(ONTIME);       
-                LEDTimer.reset();  // reset the timer
-                PGMState = IDLESTATE;  // go idle state
+                if (LEDTimer.read_ms() > timeToChangeDF){     
+                    LEDTimer.reset();  // reset the timer
+                    PGMState = IDLESTATE;  // go idle state
+                }
                 break;
               }
              case LOWLITE: {              
                 redColor.write(LEDOFF);
                 greenColor.write(LEDON);
+                timeToChangeDF = LIMITSTAYON;
                 // create string for display on LCD
-                wait(ONTIME);       
-                LEDTimer.reset();  // reset the timer
-                PGMState = IDLESTATE;  // go idle state
+                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
-    }// emd while
+
+    }// end while
 }