Homework for week 5, #2. Sets up light sensor to control brightness of LEDs.

Dependencies:   SLCD mbed TSI

Revision:
0:a9d218e72251
Child:
1:050d003386cb
diff -r 000000000000 -r a9d218e72251 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 14 18:51:16 2016 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+#include "SLCD.h"
+
+
+#define PROGNAME "lightsense_kl46z_basic modified by CKM v1\n\r"
+#define DATATIME 400 // milliseconds
+#define LCDLEN 10
+#define LIGHTSENSORPORT PTE22
+
+
+
+SLCD slcd; //define LCD display globally define
+Serial pc(USBTX, USBRX);
+Timer LEDTimer;
+
+void LCDMess(char *lMess){
+        slcd.Home();
+        slcd.clear();
+        slcd.printf(lMess);
+} 
+
+
+int main() {
+    //set up LEDs
+    PwmOut gled(LED_GREEN);
+    PwmOut rled(LED_RED);
+    
+    AnalogIn LightSensor(LIGHTSENSORPORT);
+    float lightData; 
+    char lcdData[LCDLEN];
+   
+    int timeToChangeDF = DATATIME;
+    LEDTimer.start();
+    LEDTimer.reset();
+    pc.printf(PROGNAME);
+    
+    while(true) {    
+        if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
+            lightData = (1.0 - LightSensor.read()); // show as increasiing with increasing intensity  
+            sprintf(lcdData,"%4.3f",lightData);       
+            
+            //set LEDs to light data value for brightness
+            //they get brighter in dark rooms and dimmer in bright rooms
+            gled = lightData;
+            rled = lightData;
+            
+            LCDMess(lcdData);  
+            timeToChangeDF = DATATIME;
+            LEDTimer.reset();
+        }        
+    }// emd while
+}
\ No newline at end of file