Keisha Brathwaite
/
KBrat-SSD541-HW-5_2
KBrat-SSD541-HW-5_2
Fork of lightsense_kl46z_basic by
main.cpp@3:64e28ee5719b, 2014-09-09 (annotated)
- Committer:
- scohennm
- Date:
- Tue Sep 09 21:49:20 2014 +0000
- Revision:
- 3:64e28ee5719b
- Parent:
- 2:c016448d89b2
- Child:
- 4:bd42ab18979b
Revised so that there is no wait function.
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 | 1:51f8c2b04ce2 | 10 | #define PWMDWELL 50 // milliseconds |
scohennm | 1:51f8c2b04ce2 | 11 | #define DFDELTA 0.01 |
scohennm | 1:51f8c2b04ce2 | 12 | #define PWMTIME 1 // ms (kHz |
scohennm | 1:51f8c2b04ce2 | 13 | #define LCDLEN 10 |
scohennm | 1:51f8c2b04ce2 | 14 | #define RMPUP true |
scohennm | 1:51f8c2b04ce2 | 15 | #define RMPDWN false |
scohennm | 1:51f8c2b04ce2 | 16 | #define NUMSTATES 2 |
scohennm | 3:64e28ee5719b | 17 | #define NEWDUTYFACTOR 1 |
scohennm | 3:64e28ee5719b | 18 | #define IDLESTATE 0 |
scohennm | 0:e23fffd4b9a7 | 19 | |
scohennm | 1:51f8c2b04ce2 | 20 | float dutyFactor = 0.0; |
scohennm | 1:51f8c2b04ce2 | 21 | PwmOut greenColor(LED_GREEN); |
scohennm | 1:51f8c2b04ce2 | 22 | PwmOut redColor(LED_RED); |
scohennm | 1:51f8c2b04ce2 | 23 | SLCD slcd; //define LCD display |
scohennm | 1:51f8c2b04ce2 | 24 | float rampDirection[NUMSTATES] = {-DFDELTA, DFDELTA}; |
scohennm | 1:51f8c2b04ce2 | 25 | |
scohennm | 3:64e28ee5719b | 26 | |
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 | 1:51f8c2b04ce2 | 36 | char lcdData[LCDLEN]; |
scohennm | 1:51f8c2b04ce2 | 37 | int rampstate = RMPUP; |
scohennm | 1:51f8c2b04ce2 | 38 | int numSteps; |
scohennm | 1:51f8c2b04ce2 | 39 | float workingDelta; |
scohennm | 3:64e28ee5719b | 40 | Timer LEDTimer; |
scohennm | 3:64e28ee5719b | 41 | int timeToChangeDF = PWMDWELL; |
scohennm | 1:51f8c2b04ce2 | 42 | int i=0; |
scohennm | 3:64e28ee5719b | 43 | // set up timer for next step of Duty Factor timing |
scohennm | 3:64e28ee5719b | 44 | LEDTimer.start(); |
scohennm | 3:64e28ee5719b | 45 | LEDTimer.reset(); |
scohennm | 1:51f8c2b04ce2 | 46 | pc.printf(PROGNAME); |
scohennm | 1:51f8c2b04ce2 | 47 | |
scohennm | 2:c016448d89b2 | 48 | greenColor.period_ms(PWMTIME); // set the frequency of the pulse train |
scohennm | 3:64e28ee5719b | 49 | redColor.period_ms(PWMTIME); // so there is no flickering |
scohennm | 3:64e28ee5719b | 50 | |
scohennm | 1:51f8c2b04ce2 | 51 | workingDelta = rampDirection[rampstate]; |
scohennm | 1:51f8c2b04ce2 | 52 | numSteps = (int)(1.0/workingDelta); |
scohennm | 0:e23fffd4b9a7 | 53 | while(true) { |
scohennm | 3:64e28ee5719b | 54 | if (LEDTimer.read_ms() > timeToChangeDF) { |
scohennm | 3:64e28ee5719b | 55 | i++; |
scohennm | 3:64e28ee5719b | 56 | redColor.write(dutyFactor); |
scohennm | 3:64e28ee5719b | 57 | greenColor.write(1.0 - dutyFactor); |
scohennm | 3:64e28ee5719b | 58 | dutyFactor += workingDelta; |
scohennm | 3:64e28ee5719b | 59 | sprintf (lcdData,"%4.3f",dutyFactor); |
scohennm | 3:64e28ee5719b | 60 | LCDMess(lcdData); |
scohennm | 1:51f8c2b04ce2 | 61 | #ifdef PRINTDEBUG |
scohennm | 1:51f8c2b04ce2 | 62 | pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta); |
scohennm | 1:51f8c2b04ce2 | 63 | #endif |
scohennm | 3:64e28ee5719b | 64 | if (!(i % numSteps)){ |
scohennm | 3:64e28ee5719b | 65 | i=0; |
scohennm | 3:64e28ee5719b | 66 | LEDTimer.reset(); |
scohennm | 3:64e28ee5719b | 67 | rampstate = !rampstate; |
scohennm | 3:64e28ee5719b | 68 | workingDelta = rampDirection[rampstate]; |
scohennm | 3:64e28ee5719b | 69 | } |
scohennm | 1:51f8c2b04ce2 | 70 | } |
scohennm | 0:e23fffd4b9a7 | 71 | } |
scohennm | 0:e23fffd4b9a7 | 72 | } |