
Test Demo for Ambient Light Sensor
main.cpp@0:75c2c5adf52a, 2016-01-27 (annotated)
- Committer:
- ceojoe1
- Date:
- Wed Jan 27 02:15:00 2016 +0000
- Revision:
- 0:75c2c5adf52a
Ambient Light Sensor Demo;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ceojoe1 | 0:75c2c5adf52a | 1 | #include "mbed.h" |
ceojoe1 | 0:75c2c5adf52a | 2 | #include "SLCD.h" |
ceojoe1 | 0:75c2c5adf52a | 3 | |
ceojoe1 | 0:75c2c5adf52a | 4 | #define PROGNAME "LightSense_K146z_PWM v1\n\r" |
ceojoe1 | 0:75c2c5adf52a | 5 | #define DATATIME 400 //milliseconds |
ceojoe1 | 0:75c2c5adf52a | 6 | #define LCDLEN 10 |
ceojoe1 | 0:75c2c5adf52a | 7 | #define LIGHTSENSORPORT PTE22 |
ceojoe1 | 0:75c2c5adf52a | 8 | |
ceojoe1 | 0:75c2c5adf52a | 9 | SLCD slcd; //define LCD display globally define |
ceojoe1 | 0:75c2c5adf52a | 10 | Serial pc(USBTX, USBRX); |
ceojoe1 | 0:75c2c5adf52a | 11 | Timer LEDTimer; |
ceojoe1 | 0:75c2c5adf52a | 12 | |
ceojoe1 | 0:75c2c5adf52a | 13 | void LCDMess(char *lMess) |
ceojoe1 | 0:75c2c5adf52a | 14 | { |
ceojoe1 | 0:75c2c5adf52a | 15 | slcd.Home(); |
ceojoe1 | 0:75c2c5adf52a | 16 | slcd.clear(); |
ceojoe1 | 0:75c2c5adf52a | 17 | slcd.printf(lMess); |
ceojoe1 | 0:75c2c5adf52a | 18 | } |
ceojoe1 | 0:75c2c5adf52a | 19 | |
ceojoe1 | 0:75c2c5adf52a | 20 | int main() |
ceojoe1 | 0:75c2c5adf52a | 21 | { |
ceojoe1 | 0:75c2c5adf52a | 22 | PwmOut greenColor(LED_GREEN); |
ceojoe1 | 0:75c2c5adf52a | 23 | PwmOut redColor(LED_RED); |
ceojoe1 | 0:75c2c5adf52a | 24 | AnalogIn LightSensor(LIGHTSENSORPORT); |
ceojoe1 | 0:75c2c5adf52a | 25 | float lightData; |
ceojoe1 | 0:75c2c5adf52a | 26 | char lcdData[LCDLEN]; |
ceojoe1 | 0:75c2c5adf52a | 27 | float max; |
ceojoe1 | 0:75c2c5adf52a | 28 | float min = 1.0; |
ceojoe1 | 0:75c2c5adf52a | 29 | int timeToChangeDF = DATATIME; |
ceojoe1 | 0:75c2c5adf52a | 30 | // set up tier for next step of Duty Factor timing |
ceojoe1 | 0:75c2c5adf52a | 31 | LEDTimer.start(); |
ceojoe1 | 0:75c2c5adf52a | 32 | LEDTimer.reset(); |
ceojoe1 | 0:75c2c5adf52a | 33 | pc.printf(PROGNAME); |
ceojoe1 | 0:75c2c5adf52a | 34 | |
ceojoe1 | 0:75c2c5adf52a | 35 | while (true) { |
ceojoe1 | 0:75c2c5adf52a | 36 | if(LEDTimer.read_ms() > timeToChangeDF) { //check for timer |
ceojoe1 | 0:75c2c5adf52a | 37 | lightData = (1.0 - LightSensor.read()); |
ceojoe1 | 0:75c2c5adf52a | 38 | /* |
ceojoe1 | 0:75c2c5adf52a | 39 | * IF LIGHT DATA > MAX, |
ceojoe1 | 0:75c2c5adf52a | 40 | MAX VALUE BECOMES THE CURRENT LIGHT DATA |
ceojoe1 | 0:75c2c5adf52a | 41 | */ |
ceojoe1 | 0:75c2c5adf52a | 42 | if (lightData > max) |
ceojoe1 | 0:75c2c5adf52a | 43 | { |
ceojoe1 | 0:75c2c5adf52a | 44 | max = lightData; |
ceojoe1 | 0:75c2c5adf52a | 45 | } |
ceojoe1 | 0:75c2c5adf52a | 46 | if(lightData < min) |
ceojoe1 | 0:75c2c5adf52a | 47 | { |
ceojoe1 | 0:75c2c5adf52a | 48 | min = lightData; |
ceojoe1 | 0:75c2c5adf52a | 49 | } |
ceojoe1 | 0:75c2c5adf52a | 50 | pc.printf("MAX: %4.3f\r\n", max); |
ceojoe1 | 0:75c2c5adf52a | 51 | pc.printf("MIN: %4.3f\r\n", min); |
ceojoe1 | 0:75c2c5adf52a | 52 | greenColor.write(1.0 - lightData); |
ceojoe1 | 0:75c2c5adf52a | 53 | redColor.write(1.0 - lightData); |
ceojoe1 | 0:75c2c5adf52a | 54 | sprintf(lcdData, "%4.3f", lightData); |
ceojoe1 | 0:75c2c5adf52a | 55 | LCDMess(lcdData); |
ceojoe1 | 0:75c2c5adf52a | 56 | pc.printf("%4.3f\r\n", lightData); |
ceojoe1 | 0:75c2c5adf52a | 57 | timeToChangeDF = DATATIME; |
ceojoe1 | 0:75c2c5adf52a | 58 | LEDTimer.reset(); |
ceojoe1 | 0:75c2c5adf52a | 59 | } |
ceojoe1 | 0:75c2c5adf52a | 60 | } |
ceojoe1 | 0:75c2c5adf52a | 61 | } |