EMench_SSD645_HW3.2. Took out the decimal point.

Dependencies:   SLCD mbed

Fork of lightsense_kl46z_PWM_simple by Stanley Cohen

Revision:
4:bd42ab18979b
Parent:
3:64e28ee5719b
Child:
5:817aa144563d
--- a/main.cpp	Tue Sep 09 21:49:20 2014 +0000
+++ b/main.cpp	Thu Sep 11 22:32:51 2014 +0000
@@ -17,13 +17,8 @@
 #define NEWDUTYFACTOR 1
 #define IDLESTATE 0
 
-float dutyFactor = 0.0;
-PwmOut greenColor(LED_GREEN);
-PwmOut redColor(LED_RED);
-SLCD slcd; //define LCD display
-float rampDirection[NUMSTATES] = {-DFDELTA, DFDELTA};
 
-
+SLCD slcd; //define LCD display globally define
 Serial pc(USBTX, USBRX);
 
 void LCDMess(char *lMess){
@@ -33,13 +28,15 @@
 } 
 
 int main() {
+    PwmOut greenColor(LED_GREEN);
+    PwmOut redColor(LED_RED);
     char lcdData[LCDLEN];
-    int rampstate = RMPUP;
-    int numSteps;
-    float workingDelta;
-    Timer LEDTimer;
+    Timer LEDTimer;  // time till next PWM values is to change.
+    int dfState = IDLESTATE; // work till timer transitions
+    float dutyFactor = 0.0;  
+    float workingDelta; // hold value of direction of duty factor
+   
     int timeToChangeDF = PWMDWELL;
-    int i=0;
     // set up timer for next step of Duty Factor timing
     LEDTimer.start();
     LEDTimer.reset();
@@ -48,25 +45,35 @@
     greenColor.period_ms(PWMTIME); // set the frequency of the pulse train
     redColor.period_ms(PWMTIME);   // so there is no flickering
     
-    workingDelta = rampDirection[rampstate];
-    numSteps = (int)(1.0/workingDelta);
+    workingDelta = DFDELTA;
+
     while(true) {
-        if (LEDTimer.read_ms() > timeToChangeDF) { 
-            i++;      
-            redColor.write(dutyFactor);
-            greenColor.write(1.0 - dutyFactor);
-            dutyFactor += workingDelta;
-            sprintf (lcdData,"%4.3f",dutyFactor);  
-            LCDMess(lcdData);
+        switch (dfState){ 
+            case IDLESTATE: {
+                if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
+                    dfState = NEWDUTYFACTOR;
+                    LEDTimer.reset();
+                    }
+                break;
+              }
+             case  NEWDUTYFACTOR: {              
+                dutyFactor += workingDelta;           
+                if(dutyFactor >= 1.0) workingDelta = -workingDelta; // change direction if needed
+                if(dutyFactor < DFDELTA) workingDelta = DFDELTA; 
+                redColor.write(dutyFactor);
+                greenColor.write(1.0 - dutyFactor);
+                // print to LCD screen
+                sprintf (lcdData,"%4.3f",dutyFactor);  
+                LCDMess(lcdData);
+                
+                LEDTimer.reset();  // reset the timer
+                dfState = 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
-            if (!(i % numSteps)){
-                i=0;
-                LEDTimer.reset();
-                rampstate = !rampstate;
-                workingDelta = rampDirection[rampstate];
-            }
-        }
-    }
-}
\ No newline at end of file
+    }// emd while
+}