EMench_SSD645_HW3.2. Took out the decimal point.
Fork of lightsense_kl46z_PWM_simple by
main.cpp@6:710e18c552f5, 2014-09-14 (annotated)
- Committer:
- scohennm
- Date:
- Sun Sep 14 23:45:39 2014 +0000
- Revision:
- 6:710e18c552f5
- Parent:
- 5:817aa144563d
- Child:
- 7:8d7089b514ae
Example of using visible light sensor and LCD display. LEDs showing high and low limits.
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 | 6:710e18c552f5 | 5 | // An State Machine solution to HW 4.1 Q3 |
scohennm | 6:710e18c552f5 | 6 | // using wait() for high and low limits. |
scohennm | 1:51f8c2b04ce2 | 7 | //#define PRINTDEBUG |
scohennm | 6:710e18c552f5 | 8 | #define PROGNAME "blink_kl46z_states v2\n\r" |
scohennm | 0:e23fffd4b9a7 | 9 | #define LEDON false |
scohennm | 0:e23fffd4b9a7 | 10 | #define LEDOFF true |
scohennm | 5:817aa144563d | 11 | #define LEDTMESS "TRUE" |
scohennm | 5:817aa144563d | 12 | #define LEDFMESS "FALS" |
scohennm | 5:817aa144563d | 13 | #define BLINKDWELL 400 // milliseconds |
scohennm | 1:51f8c2b04ce2 | 14 | #define DFDELTA 0.01 |
scohennm | 1:51f8c2b04ce2 | 15 | #define PWMTIME 1 // ms (kHz |
scohennm | 1:51f8c2b04ce2 | 16 | #define LCDLEN 10 |
scohennm | 1:51f8c2b04ce2 | 17 | #define NUMSTATES 2 |
scohennm | 6:710e18c552f5 | 18 | #define LOWLIMIT 0.1 |
scohennm | 6:710e18c552f5 | 19 | #define HILIMIT 0.85 |
scohennm | 6:710e18c552f5 | 20 | #define ONTIME 1.0 |
scohennm | 6:710e18c552f5 | 21 | |
scohennm | 6:710e18c552f5 | 22 | |
scohennm | 6:710e18c552f5 | 23 | enum blinkStates {IDLESTATE, NEWBLINK, LOWLITE, HILITE}; // define the states |
scohennm | 0:e23fffd4b9a7 | 24 | |
scohennm | 5:817aa144563d | 25 | char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS}; |
scohennm | 4:bd42ab18979b | 26 | SLCD slcd; //define LCD display globally define |
scohennm | 1:51f8c2b04ce2 | 27 | Serial pc(USBTX, USBRX); |
scohennm | 1:51f8c2b04ce2 | 28 | |
scohennm | 1:51f8c2b04ce2 | 29 | void LCDMess(char *lMess){ |
scohennm | 1:51f8c2b04ce2 | 30 | slcd.Home(); |
scohennm | 1:51f8c2b04ce2 | 31 | slcd.clear(); |
scohennm | 1:51f8c2b04ce2 | 32 | slcd.printf(lMess); |
scohennm | 1:51f8c2b04ce2 | 33 | } |
scohennm | 0:e23fffd4b9a7 | 34 | |
scohennm | 0:e23fffd4b9a7 | 35 | int main() { |
scohennm | 5:817aa144563d | 36 | DigitalOut greenColor(LED_GREEN); |
scohennm | 5:817aa144563d | 37 | DigitalOut redColor(LED_RED); |
scohennm | 6:710e18c552f5 | 38 | AnalogIn LightSensor(PTE22); |
scohennm | 6:710e18c552f5 | 39 | float lightData; |
scohennm | 1:51f8c2b04ce2 | 40 | char lcdData[LCDLEN]; |
scohennm | 4:bd42ab18979b | 41 | Timer LEDTimer; // time till next PWM values is to change. |
scohennm | 6:710e18c552f5 | 42 | blinkStates PGMState = IDLESTATE; // work till timer transitions |
scohennm | 5:817aa144563d | 43 | int ledState = LEDON; |
scohennm | 5:817aa144563d | 44 | |
scohennm | 4:bd42ab18979b | 45 | |
scohennm | 5:817aa144563d | 46 | int timeToChangeDF = BLINKDWELL; |
scohennm | 3:64e28ee5719b | 47 | // set up timer for next step of Duty Factor timing |
scohennm | 3:64e28ee5719b | 48 | LEDTimer.start(); |
scohennm | 3:64e28ee5719b | 49 | LEDTimer.reset(); |
scohennm | 1:51f8c2b04ce2 | 50 | pc.printf(PROGNAME); |
scohennm | 1:51f8c2b04ce2 | 51 | |
scohennm | 3:64e28ee5719b | 52 | |
scohennm | 4:bd42ab18979b | 53 | |
scohennm | 0:e23fffd4b9a7 | 54 | while(true) { |
scohennm | 5:817aa144563d | 55 | switch (PGMState){ |
scohennm | 4:bd42ab18979b | 56 | case IDLESTATE: { |
scohennm | 4:bd42ab18979b | 57 | if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion |
scohennm | 6:710e18c552f5 | 58 | lightData = (1.0 - LightSensor.read()); |
scohennm | 6:710e18c552f5 | 59 | sprintf(lcdData,"%4.3f",lightData); |
scohennm | 6:710e18c552f5 | 60 | LCDMess(lcdData); |
scohennm | 6:710e18c552f5 | 61 | if (lightData < LOWLIMIT){ // find appropriate state |
scohennm | 6:710e18c552f5 | 62 | PGMState = LOWLITE; |
scohennm | 6:710e18c552f5 | 63 | } else if (lightData > HILIMIT){ |
scohennm | 6:710e18c552f5 | 64 | PGMState = HILITE; |
scohennm | 6:710e18c552f5 | 65 | } else { |
scohennm | 6:710e18c552f5 | 66 | PGMState = NEWBLINK; |
scohennm | 6:710e18c552f5 | 67 | } |
scohennm | 6:710e18c552f5 | 68 | |
scohennm | 6:710e18c552f5 | 69 | } |
scohennm | 4:bd42ab18979b | 70 | break; |
scohennm | 4:bd42ab18979b | 71 | } |
scohennm | 5:817aa144563d | 72 | case NEWBLINK: { |
scohennm | 5:817aa144563d | 73 | ledState = !ledState; |
scohennm | 6:710e18c552f5 | 74 | redColor.write(ledState); |
scohennm | 6:710e18c552f5 | 75 | greenColor.write(!ledState); |
scohennm | 6:710e18c552f5 | 76 | // create string for display on LCD |
scohennm | 6:710e18c552f5 | 77 | |
scohennm | 6:710e18c552f5 | 78 | LEDTimer.reset(); // reset the timer |
scohennm | 6:710e18c552f5 | 79 | PGMState = IDLESTATE; // go idle state |
scohennm | 6:710e18c552f5 | 80 | break; |
scohennm | 6:710e18c552f5 | 81 | } |
scohennm | 6:710e18c552f5 | 82 | case HILITE: { |
scohennm | 6:710e18c552f5 | 83 | redColor.write(LEDON); |
scohennm | 6:710e18c552f5 | 84 | greenColor.write(LEDOFF); |
scohennm | 6:710e18c552f5 | 85 | // create string for display on LCD |
scohennm | 6:710e18c552f5 | 86 | wait(ONTIME); |
scohennm | 6:710e18c552f5 | 87 | LEDTimer.reset(); // reset the timer |
scohennm | 6:710e18c552f5 | 88 | PGMState = IDLESTATE; // go idle state |
scohennm | 6:710e18c552f5 | 89 | break; |
scohennm | 6:710e18c552f5 | 90 | } |
scohennm | 6:710e18c552f5 | 91 | case LOWLITE: { |
scohennm | 6:710e18c552f5 | 92 | redColor.write(LEDOFF); |
scohennm | 6:710e18c552f5 | 93 | greenColor.write(LEDON); |
scohennm | 6:710e18c552f5 | 94 | // create string for display on LCD |
scohennm | 6:710e18c552f5 | 95 | wait(ONTIME); |
scohennm | 4:bd42ab18979b | 96 | LEDTimer.reset(); // reset the timer |
scohennm | 5:817aa144563d | 97 | PGMState = IDLESTATE; // go idle state |
scohennm | 4:bd42ab18979b | 98 | break; |
scohennm | 4:bd42ab18979b | 99 | } |
scohennm | 4:bd42ab18979b | 100 | } // end state machine |
scohennm | 4:bd42ab18979b | 101 | |
scohennm | 1:51f8c2b04ce2 | 102 | #ifdef PRINTDEBUG |
scohennm | 1:51f8c2b04ce2 | 103 | pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta); |
scohennm | 1:51f8c2b04ce2 | 104 | #endif |
scohennm | 4:bd42ab18979b | 105 | }// emd while |
scohennm | 4:bd42ab18979b | 106 | } |