NMHU example for light sensor SSD 341

Dependencies:   SLCD mbed

Fork of lightsense_kl46z_states by Stanley Cohen

Revision:
3:64e28ee5719b
Parent:
2:c016448d89b2
Child:
4:bd42ab18979b
--- a/main.cpp	Fri Sep 05 23:37:33 2014 +0000
+++ b/main.cpp	Tue Sep 09 21:49:20 2014 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "SLCD.h"
 
+
 // An example of C++ abuse 140904 sc
 //#define PRINTDEBUG
 #define PROGNAME "blink_kl46z_states v1\n\r"
@@ -13,6 +14,8 @@
 #define RMPUP true
 #define RMPDWN false
 #define NUMSTATES 2
+#define NEWDUTYFACTOR 1
+#define IDLESTATE 0
 
 float dutyFactor = 0.0;
 PwmOut greenColor(LED_GREEN);
@@ -20,6 +23,7 @@
 SLCD slcd; //define LCD display
 float rampDirection[NUMSTATES] = {-DFDELTA, DFDELTA};
 
+
 Serial pc(USBTX, USBRX);
 
 void LCDMess(char *lMess){
@@ -33,29 +37,36 @@
     int rampstate = RMPUP;
     int numSteps;
     float workingDelta;
+    Timer LEDTimer;
+    int timeToChangeDF = PWMDWELL;
     int i=0;
-    
+    // set up timer for next step of Duty Factor timing
+    LEDTimer.start();
+    LEDTimer.reset();
     pc.printf(PROGNAME);
     
     greenColor.period_ms(PWMTIME); // set the frequency of the pulse train
-    redColor.period_ms(PWMTIME);
+    redColor.period_ms(PWMTIME);   // so there is no flickering
+    
     workingDelta = rampDirection[rampstate];
     numSteps = (int)(1.0/workingDelta);
     while(true) {
-        i++;      
-        redColor.write(dutyFactor);
-        greenColor.write(1.0 - dutyFactor);
-        dutyFactor += workingDelta;
-        sprintf (lcdData,"%4.3f",dutyFactor);  
-        LCDMess(lcdData);
+        if (LEDTimer.read_ms() > timeToChangeDF) { 
+            i++;      
+            redColor.write(dutyFactor);
+            greenColor.write(1.0 - dutyFactor);
+            dutyFactor += workingDelta;
+            sprintf (lcdData,"%4.3f",dutyFactor);  
+            LCDMess(lcdData);
 #ifdef PRINTDEBUG
         pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r",  i, dutyFactor, workingDelta); 
 #endif
-        if (!(i % numSteps)){
-            i=0;
-            rampstate = !rampstate;
-            workingDelta = rampDirection[rampstate];
+            if (!(i % numSteps)){
+                i=0;
+                LEDTimer.reset();
+                rampstate = !rampstate;
+                workingDelta = rampDirection[rampstate];
+            }
         }
-        wait_ms(PWMDWELL); 
     }
 }
\ No newline at end of file