led light on and off using light sensor
Fork of lightsense_kl46z_basic by
main.cpp@5:817aa144563d, 2014-09-12 (annotated)
- Committer:
- scohennm
- Date:
- Fri Sep 12 19:46:15 2014 +0000
- Revision:
- 5:817aa144563d
- Parent:
- 4:bd42ab18979b
- Child:
- 6:710e18c552f5
Cleaned up timer redundancy
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 | 1:51f8c2b04ce2 | 5 | // An example of C++ abuse 140904 sc |
scohennm | 1:51f8c2b04ce2 | 6 | //#define PRINTDEBUG |
scohennm | 1:51f8c2b04ce2 | 7 | #define PROGNAME "blink_kl46z_states v1\n\r" |
scohennm | 0:e23fffd4b9a7 | 8 | #define LEDON false |
scohennm | 0:e23fffd4b9a7 | 9 | #define LEDOFF true |
scohennm | 5:817aa144563d | 10 | #define LEDTMESS "TRUE" |
scohennm | 5:817aa144563d | 11 | #define LEDFMESS "FALS" |
scohennm | 5:817aa144563d | 12 | #define BLINKDWELL 400 // milliseconds |
scohennm | 1:51f8c2b04ce2 | 13 | #define DFDELTA 0.01 |
scohennm | 1:51f8c2b04ce2 | 14 | #define PWMTIME 1 // ms (kHz |
scohennm | 1:51f8c2b04ce2 | 15 | #define LCDLEN 10 |
scohennm | 1:51f8c2b04ce2 | 16 | #define NUMSTATES 2 |
scohennm | 5:817aa144563d | 17 | #define NEWBLINK 1 |
scohennm | 3:64e28ee5719b | 18 | #define IDLESTATE 0 |
scohennm | 0:e23fffd4b9a7 | 19 | |
scohennm | 5:817aa144563d | 20 | char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS}; |
scohennm | 4:bd42ab18979b | 21 | SLCD slcd; //define LCD display globally define |
scohennm | 1:51f8c2b04ce2 | 22 | Serial pc(USBTX, USBRX); |
scohennm | 1:51f8c2b04ce2 | 23 | |
scohennm | 1:51f8c2b04ce2 | 24 | void LCDMess(char *lMess){ |
scohennm | 1:51f8c2b04ce2 | 25 | slcd.Home(); |
scohennm | 1:51f8c2b04ce2 | 26 | slcd.clear(); |
scohennm | 1:51f8c2b04ce2 | 27 | slcd.printf(lMess); |
scohennm | 1:51f8c2b04ce2 | 28 | } |
scohennm | 0:e23fffd4b9a7 | 29 | |
scohennm | 0:e23fffd4b9a7 | 30 | int main() { |
scohennm | 5:817aa144563d | 31 | DigitalOut greenColor(LED_GREEN); |
scohennm | 5:817aa144563d | 32 | DigitalOut redColor(LED_RED); |
scohennm | 1:51f8c2b04ce2 | 33 | char lcdData[LCDLEN]; |
scohennm | 4:bd42ab18979b | 34 | Timer LEDTimer; // time till next PWM values is to change. |
scohennm | 5:817aa144563d | 35 | int PGMState = IDLESTATE; // work till timer transitions |
scohennm | 5:817aa144563d | 36 | int ledState = LEDON; |
scohennm | 5:817aa144563d | 37 | |
scohennm | 4:bd42ab18979b | 38 | |
scohennm | 5:817aa144563d | 39 | int timeToChangeDF = BLINKDWELL; |
scohennm | 3:64e28ee5719b | 40 | // set up timer for next step of Duty Factor timing |
scohennm | 3:64e28ee5719b | 41 | LEDTimer.start(); |
scohennm | 3:64e28ee5719b | 42 | LEDTimer.reset(); |
scohennm | 1:51f8c2b04ce2 | 43 | pc.printf(PROGNAME); |
scohennm | 1:51f8c2b04ce2 | 44 | |
scohennm | 3:64e28ee5719b | 45 | |
scohennm | 4:bd42ab18979b | 46 | |
scohennm | 0:e23fffd4b9a7 | 47 | while(true) { |
scohennm | 5:817aa144563d | 48 | switch (PGMState){ |
scohennm | 4:bd42ab18979b | 49 | case IDLESTATE: { |
scohennm | 4:bd42ab18979b | 50 | if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion |
scohennm | 5:817aa144563d | 51 | PGMState = NEWBLINK; |
scohennm | 4:bd42ab18979b | 52 | } |
scohennm | 4:bd42ab18979b | 53 | break; |
scohennm | 4:bd42ab18979b | 54 | } |
scohennm | 5:817aa144563d | 55 | case NEWBLINK: { |
scohennm | 5:817aa144563d | 56 | ledState = !ledState; |
scohennm | 5:817aa144563d | 57 | redColor = ledState; |
scohennm | 5:817aa144563d | 58 | greenColor = !ledState; |
scohennm | 5:817aa144563d | 59 | sprintf(lcdData,logicalString[ledState]); |
scohennm | 5:817aa144563d | 60 | LCDMess(lcdData); |
scohennm | 4:bd42ab18979b | 61 | LEDTimer.reset(); // reset the timer |
scohennm | 5:817aa144563d | 62 | PGMState = IDLESTATE; // go idle state |
scohennm | 4:bd42ab18979b | 63 | break; |
scohennm | 4:bd42ab18979b | 64 | } |
scohennm | 4:bd42ab18979b | 65 | } // end state machine |
scohennm | 4:bd42ab18979b | 66 | |
scohennm | 1:51f8c2b04ce2 | 67 | #ifdef PRINTDEBUG |
scohennm | 1:51f8c2b04ce2 | 68 | pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta); |
scohennm | 1:51f8c2b04ce2 | 69 | #endif |
scohennm | 4:bd42ab18979b | 70 | }// emd while |
scohennm | 4:bd42ab18979b | 71 | } |