Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of lightsense_kl46z_states by
Revision 7:5d370a924822, committed 2014-10-14
- Comitter:
- scohennm
- Date:
- Tue Oct 14 16:59:19 2014 +0000
- Parent:
- 6:710e18c552f5
- Commit message:
- NMHU SSD 341 sieve demo with light sensor while loop for timer
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 710e18c552f5 -r 5d370a924822 main.cpp --- a/main.cpp Sun Sep 14 23:45:39 2014 +0000 +++ b/main.cpp Tue Oct 14 16:59:19 2014 +0000 @@ -4,23 +4,33 @@ // An State Machine solution to HW 4.1 Q3 // using wait() for high and low limits. +// Use the idea of a sieve modoel instead of a series of if..then..elseif.. //#define PRINTDEBUG -#define PROGNAME "blink_kl46z_states v2\n\r" +#define PROGNAME "blink_kl46z_states seive v3\n\r" #define LEDON false #define LEDOFF true #define LEDTMESS "TRUE" #define LEDFMESS "FALS" -#define BLINKDWELL 400 // milliseconds +#define BLINKDWELL 400// milliseconds +#define LIMITSTAYON 1000 #define DFDELTA 0.01 #define PWMTIME 1 // ms (kHz #define LCDLEN 10 -#define NUMSTATES 2 +#define NUMLIMITS 3 +#define BOTTOMLIMIT 0.0 #define LOWLIMIT 0.1 #define HILIMIT 0.85 +#define TOPLIMIT 1.1 #define ONTIME 1.0 -enum blinkStates {IDLESTATE, NEWBLINK, LOWLITE, HILITE}; // define the states +#define NUMSTATES 4 +#define IDLESTATE 0 +#define NEWBLINK 1 +#define LOWLITE 2 +#define HILITE 3 +int stateArray[NUMSTATES] = {HILITE, NEWBLINK, LOWLITE,IDLESTATE}; +float lightLimits[NUMLIMITS] = {HILIMIT, LOWLIMIT, BOTTOMLIMIT}; char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS}; SLCD slcd; //define LCD display globally define @@ -39,9 +49,9 @@ float lightData; char lcdData[LCDLEN]; Timer LEDTimer; // time till next PWM values is to change. - blinkStates PGMState = IDLESTATE; // work till timer transitions + int PGMState = IDLESTATE; // work till timer transitions int ledState = LEDON; - + int i; int timeToChangeDF = BLINKDWELL; // set up timer for next step of Duty Factor timing @@ -49,58 +59,55 @@ LEDTimer.reset(); pc.printf(PROGNAME); + greenColor.write(LEDON); + redColor.write(LEDON); - while(true) { - switch (PGMState){ - case IDLESTATE: { - if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion + while(LEDTimer.read_ms() > timeToChangeDF){// check for timer time out transtion + switch (PGMState){ + case IDLESTATE: { lightData = (1.0 - LightSensor.read()); sprintf(lcdData,"%4.3f",lightData); LCDMess(lcdData); - if (lightData < LOWLIMIT){ // find appropriate state - PGMState = LOWLITE; - } else if (lightData > HILIMIT){ - PGMState = HILITE; - } else { - PGMState = NEWBLINK; - } - + timeToChangeDF = BLINKDWELL; + LEDTimer.reset(); // reset the timer + // put data throuth the sieve. + i=0;//start sieve + while((lightData < lightLimits[i])&& (i < NUMLIMITS)){ + i++; + } + PGMState = stateArray[i]; + break; } - break; - } - case NEWBLINK: { - ledState = !ledState; - redColor.write(ledState); - greenColor.write(!ledState); - // create string for display on LCD - - LEDTimer.reset(); // reset the timer - PGMState = IDLESTATE; // go idle state - break; + case NEWBLINK: { + redColor.write(ledState); + greenColor.write(!ledState); + ledState = !ledState; + // create string for display on LCD + timeToChangeDF = BLINKDWELL; + LEDTimer.reset(); // reset the timer + PGMState = IDLESTATE; // go idle state + break; + } + case HILITE: { + redColor.write(LEDON); + greenColor.write(LEDOFF); + timeToChangeDF = LIMITSTAYON; + + LEDTimer.reset(); // reset the timer + PGMState = IDLESTATE; // go idle state + break; + } + case LOWLITE: { + redColor.write(LEDOFF); + greenColor.write(LEDON); + timeToChangeDF = LIMITSTAYON; + // create string for display on LCD + LEDTimer.reset(); // reset the timer + PGMState = IDLESTATE; // go idle state + break; } - case HILITE: { - redColor.write(LEDON); - greenColor.write(LEDOFF); - // create string for display on LCD - wait(ONTIME); - LEDTimer.reset(); // reset the timer - PGMState = IDLESTATE; // go idle state - break; - } - case LOWLITE: { - redColor.write(LEDOFF); - greenColor.write(LEDON); - // create string for display on LCD - wait(ONTIME); - LEDTimer.reset(); // reset the timer - PGMState = IDLESTATE; // go idle state - break; - } - } // end state machine - -#ifdef PRINTDEBUG - pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta); -#endif - }// emd while + } // end state machine + }// end timer while + }// end foreever while }