Shane Barton
/
sbart_lightsense_kl46z_PWM_simple
SB_HW_3-2
Fork of lightsense_kl46z_PWM_simple by
main.cpp@9:cc1bae13d6be, 2017-02-05 (annotated)
- Committer:
- sbart
- Date:
- Sun Feb 05 06:53:04 2017 +0000
- Revision:
- 9:cc1bae13d6be
- Parent:
- 8:ea818c9220fc
SB_HW_3-2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:e23fffd4b9a7 | 1 | #include "mbed.h" |
scohennm | 1:51f8c2b04ce2 | 2 | #include "SLCD.h" |
scohennm | 1:51f8c2b04ce2 | 3 | |
scohennm | 3:64e28ee5719b | 4 | |
scohennm | 8:ea818c9220fc | 5 | #define PROGNAME "lightsense_kl46z_PWM v1\n\r" |
scohennm | 8:ea818c9220fc | 6 | #define DATATIME 400 // milliseconds |
scohennm | 1:51f8c2b04ce2 | 7 | #define LCDLEN 10 |
scohennm | 8:ea818c9220fc | 8 | #define LIGHTSENSORPORT PTE22 |
scohennm | 6:710e18c552f5 | 9 | |
scohennm | 4:bd42ab18979b | 10 | SLCD slcd; //define LCD display globally define |
scohennm | 1:51f8c2b04ce2 | 11 | Serial pc(USBTX, USBRX); |
scohennm | 8:ea818c9220fc | 12 | Timer LEDTimer; |
scohennm | 1:51f8c2b04ce2 | 13 | |
scohennm | 1:51f8c2b04ce2 | 14 | void LCDMess(char *lMess){ |
scohennm | 1:51f8c2b04ce2 | 15 | slcd.Home(); |
scohennm | 1:51f8c2b04ce2 | 16 | slcd.clear(); |
scohennm | 1:51f8c2b04ce2 | 17 | slcd.printf(lMess); |
sbart | 9:cc1bae13d6be | 18 | |
sbart | 9:cc1bae13d6be | 19 | slcd.DP(0, false); //Disables the decimal point in position 0 |
scohennm | 1:51f8c2b04ce2 | 20 | } |
scohennm | 0:e23fffd4b9a7 | 21 | |
scohennm | 0:e23fffd4b9a7 | 22 | int main() { |
scohennm | 8:ea818c9220fc | 23 | PwmOut greenColor(LED_GREEN); |
scohennm | 8:ea818c9220fc | 24 | PwmOut redColor(LED_RED); |
scohennm | 8:ea818c9220fc | 25 | AnalogIn LightSensor(LIGHTSENSORPORT); |
scohennm | 6:710e18c552f5 | 26 | float lightData; |
scohennm | 1:51f8c2b04ce2 | 27 | char lcdData[LCDLEN]; |
scohennm | 4:bd42ab18979b | 28 | |
scohennm | 8:ea818c9220fc | 29 | int timeToChangeDF = DATATIME; |
scohennm | 3:64e28ee5719b | 30 | // set up timer for next step of Duty Factor timing |
scohennm | 3:64e28ee5719b | 31 | LEDTimer.start(); |
scohennm | 3:64e28ee5719b | 32 | LEDTimer.reset(); |
scohennm | 1:51f8c2b04ce2 | 33 | pc.printf(PROGNAME); |
scohennm | 1:51f8c2b04ce2 | 34 | |
scohennm | 3:64e28ee5719b | 35 | |
scohennm | 8:ea818c9220fc | 36 | while(true) { |
scohennm | 8:ea818c9220fc | 37 | if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion |
sbart | 9:cc1bae13d6be | 38 | lightData = (1.0 - LightSensor.read()) * 100; // show as increasiing with increasing intensity |
sbart | 9:cc1bae13d6be | 39 | greenColor.write(1.0 - lightData); |
sbart | 9:cc1bae13d6be | 40 | redColor.write(1.0 - lightData); |
scohennm | 8:ea818c9220fc | 41 | sprintf(lcdData,"%4.3f",lightData); |
scohennm | 8:ea818c9220fc | 42 | LCDMess(lcdData); |
scohennm | 8:ea818c9220fc | 43 | pc.printf("%4.3f\r\n",lightData); |
scohennm | 8:ea818c9220fc | 44 | timeToChangeDF = DATATIME; |
scohennm | 8:ea818c9220fc | 45 | LEDTimer.reset(); |
scohennm | 8:ea818c9220fc | 46 | } |
sbart | 9:cc1bae13d6be | 47 | }// end while |
scohennm | 4:bd42ab18979b | 48 | } |