Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

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