NMHU example for light sensor SSD 341
Fork of lightsense_kl46z_states by
main.cpp@2:c016448d89b2, 2014-09-05 (annotated)
- Committer:
- scohennm
- Date:
- Fri Sep 05 23:37:33 2014 +0000
- Revision:
- 2:c016448d89b2
- Parent:
- 1:51f8c2b04ce2
- Child:
- 3:64e28ee5719b
Moved location of PWM frequencies
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 | 1:51f8c2b04ce2 | 4 | // An example of C++ abuse 140904 sc |
scohennm | 1:51f8c2b04ce2 | 5 | //#define PRINTDEBUG |
scohennm | 1:51f8c2b04ce2 | 6 | #define PROGNAME "blink_kl46z_states v1\n\r" |
scohennm | 0:e23fffd4b9a7 | 7 | #define LEDON false |
scohennm | 0:e23fffd4b9a7 | 8 | #define LEDOFF true |
scohennm | 1:51f8c2b04ce2 | 9 | #define PWMDWELL 50 // milliseconds |
scohennm | 1:51f8c2b04ce2 | 10 | #define DFDELTA 0.01 |
scohennm | 1:51f8c2b04ce2 | 11 | #define PWMTIME 1 // ms (kHz |
scohennm | 1:51f8c2b04ce2 | 12 | #define LCDLEN 10 |
scohennm | 1:51f8c2b04ce2 | 13 | #define RMPUP true |
scohennm | 1:51f8c2b04ce2 | 14 | #define RMPDWN false |
scohennm | 1:51f8c2b04ce2 | 15 | #define NUMSTATES 2 |
scohennm | 0:e23fffd4b9a7 | 16 | |
scohennm | 1:51f8c2b04ce2 | 17 | float dutyFactor = 0.0; |
scohennm | 1:51f8c2b04ce2 | 18 | PwmOut greenColor(LED_GREEN); |
scohennm | 1:51f8c2b04ce2 | 19 | PwmOut redColor(LED_RED); |
scohennm | 1:51f8c2b04ce2 | 20 | SLCD slcd; //define LCD display |
scohennm | 1:51f8c2b04ce2 | 21 | float rampDirection[NUMSTATES] = {-DFDELTA, DFDELTA}; |
scohennm | 1:51f8c2b04ce2 | 22 | |
scohennm | 1:51f8c2b04ce2 | 23 | Serial pc(USBTX, USBRX); |
scohennm | 1:51f8c2b04ce2 | 24 | |
scohennm | 1:51f8c2b04ce2 | 25 | void LCDMess(char *lMess){ |
scohennm | 1:51f8c2b04ce2 | 26 | slcd.Home(); |
scohennm | 1:51f8c2b04ce2 | 27 | slcd.clear(); |
scohennm | 1:51f8c2b04ce2 | 28 | slcd.printf(lMess); |
scohennm | 1:51f8c2b04ce2 | 29 | } |
scohennm | 0:e23fffd4b9a7 | 30 | |
scohennm | 0:e23fffd4b9a7 | 31 | int main() { |
scohennm | 1:51f8c2b04ce2 | 32 | char lcdData[LCDLEN]; |
scohennm | 1:51f8c2b04ce2 | 33 | int rampstate = RMPUP; |
scohennm | 1:51f8c2b04ce2 | 34 | int numSteps; |
scohennm | 1:51f8c2b04ce2 | 35 | float workingDelta; |
scohennm | 1:51f8c2b04ce2 | 36 | int i=0; |
scohennm | 1:51f8c2b04ce2 | 37 | |
scohennm | 1:51f8c2b04ce2 | 38 | pc.printf(PROGNAME); |
scohennm | 1:51f8c2b04ce2 | 39 | |
scohennm | 2:c016448d89b2 | 40 | greenColor.period_ms(PWMTIME); // set the frequency of the pulse train |
scohennm | 2:c016448d89b2 | 41 | redColor.period_ms(PWMTIME); |
scohennm | 1:51f8c2b04ce2 | 42 | workingDelta = rampDirection[rampstate]; |
scohennm | 1:51f8c2b04ce2 | 43 | numSteps = (int)(1.0/workingDelta); |
scohennm | 0:e23fffd4b9a7 | 44 | while(true) { |
scohennm | 1:51f8c2b04ce2 | 45 | i++; |
scohennm | 1:51f8c2b04ce2 | 46 | redColor.write(dutyFactor); |
scohennm | 1:51f8c2b04ce2 | 47 | greenColor.write(1.0 - dutyFactor); |
scohennm | 1:51f8c2b04ce2 | 48 | dutyFactor += workingDelta; |
scohennm | 1:51f8c2b04ce2 | 49 | sprintf (lcdData,"%4.3f",dutyFactor); |
scohennm | 1:51f8c2b04ce2 | 50 | LCDMess(lcdData); |
scohennm | 1:51f8c2b04ce2 | 51 | #ifdef PRINTDEBUG |
scohennm | 1:51f8c2b04ce2 | 52 | pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta); |
scohennm | 1:51f8c2b04ce2 | 53 | #endif |
scohennm | 1:51f8c2b04ce2 | 54 | if (!(i % numSteps)){ |
scohennm | 1:51f8c2b04ce2 | 55 | i=0; |
scohennm | 1:51f8c2b04ce2 | 56 | rampstate = !rampstate; |
scohennm | 1:51f8c2b04ce2 | 57 | workingDelta = rampDirection[rampstate]; |
scohennm | 1:51f8c2b04ce2 | 58 | } |
scohennm | 1:51f8c2b04ce2 | 59 | wait_ms(PWMDWELL); |
scohennm | 0:e23fffd4b9a7 | 60 | } |
scohennm | 0:e23fffd4b9a7 | 61 | } |