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

Dependencies:   SLCD mbed TSI

Committer:
CKMonroe
Date:
Wed Sep 14 18:51:16 2016 +0000
Revision:
0:a9d218e72251
Child:
1:050d003386cb
homework 5.2.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CKMonroe 0:a9d218e72251 1 #include "mbed.h"
CKMonroe 0:a9d218e72251 2 #include "SLCD.h"
CKMonroe 0:a9d218e72251 3
CKMonroe 0:a9d218e72251 4
CKMonroe 0:a9d218e72251 5 #define PROGNAME "lightsense_kl46z_basic modified by CKM v1\n\r"
CKMonroe 0:a9d218e72251 6 #define DATATIME 400 // milliseconds
CKMonroe 0:a9d218e72251 7 #define LCDLEN 10
CKMonroe 0:a9d218e72251 8 #define LIGHTSENSORPORT PTE22
CKMonroe 0:a9d218e72251 9
CKMonroe 0:a9d218e72251 10
CKMonroe 0:a9d218e72251 11
CKMonroe 0:a9d218e72251 12 SLCD slcd; //define LCD display globally define
CKMonroe 0:a9d218e72251 13 Serial pc(USBTX, USBRX);
CKMonroe 0:a9d218e72251 14 Timer LEDTimer;
CKMonroe 0:a9d218e72251 15
CKMonroe 0:a9d218e72251 16 void LCDMess(char *lMess){
CKMonroe 0:a9d218e72251 17 slcd.Home();
CKMonroe 0:a9d218e72251 18 slcd.clear();
CKMonroe 0:a9d218e72251 19 slcd.printf(lMess);
CKMonroe 0:a9d218e72251 20 }
CKMonroe 0:a9d218e72251 21
CKMonroe 0:a9d218e72251 22
CKMonroe 0:a9d218e72251 23 int main() {
CKMonroe 0:a9d218e72251 24 //set up LEDs
CKMonroe 0:a9d218e72251 25 PwmOut gled(LED_GREEN);
CKMonroe 0:a9d218e72251 26 PwmOut rled(LED_RED);
CKMonroe 0:a9d218e72251 27
CKMonroe 0:a9d218e72251 28 AnalogIn LightSensor(LIGHTSENSORPORT);
CKMonroe 0:a9d218e72251 29 float lightData;
CKMonroe 0:a9d218e72251 30 char lcdData[LCDLEN];
CKMonroe 0:a9d218e72251 31
CKMonroe 0:a9d218e72251 32 int timeToChangeDF = DATATIME;
CKMonroe 0:a9d218e72251 33 LEDTimer.start();
CKMonroe 0:a9d218e72251 34 LEDTimer.reset();
CKMonroe 0:a9d218e72251 35 pc.printf(PROGNAME);
CKMonroe 0:a9d218e72251 36
CKMonroe 0:a9d218e72251 37 while(true) {
CKMonroe 0:a9d218e72251 38 if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
CKMonroe 0:a9d218e72251 39 lightData = (1.0 - LightSensor.read()); // show as increasiing with increasing intensity
CKMonroe 0:a9d218e72251 40 sprintf(lcdData,"%4.3f",lightData);
CKMonroe 0:a9d218e72251 41
CKMonroe 0:a9d218e72251 42 //set LEDs to light data value for brightness
CKMonroe 0:a9d218e72251 43 //they get brighter in dark rooms and dimmer in bright rooms
CKMonroe 0:a9d218e72251 44 gled = lightData;
CKMonroe 0:a9d218e72251 45 rled = lightData;
CKMonroe 0:a9d218e72251 46
CKMonroe 0:a9d218e72251 47 LCDMess(lcdData);
CKMonroe 0:a9d218e72251 48 timeToChangeDF = DATATIME;
CKMonroe 0:a9d218e72251 49 LEDTimer.reset();
CKMonroe 0:a9d218e72251 50 }
CKMonroe 0:a9d218e72251 51 }// emd while
CKMonroe 0:a9d218e72251 52 }