NMHU SSD 341 sieve with light sensor if's for timing
Fork of lightsense_kl46z_states by
Diff: main.cpp
- Revision:
- 6:710e18c552f5
- Parent:
- 5:817aa144563d
- Child:
- 7:3c7768e90016
diff -r 817aa144563d -r 710e18c552f5 main.cpp --- a/main.cpp Fri Sep 12 19:46:15 2014 +0000 +++ b/main.cpp Sun Sep 14 23:45:39 2014 +0000 @@ -2,9 +2,10 @@ #include "SLCD.h" -// An example of C++ abuse 140904 sc +// An State Machine solution to HW 4.1 Q3 +// using wait() for high and low limits. //#define PRINTDEBUG -#define PROGNAME "blink_kl46z_states v1\n\r" +#define PROGNAME "blink_kl46z_states v2\n\r" #define LEDON false #define LEDOFF true #define LEDTMESS "TRUE" @@ -14,8 +15,12 @@ #define PWMTIME 1 // ms (kHz #define LCDLEN 10 #define NUMSTATES 2 -#define NEWBLINK 1 -#define IDLESTATE 0 +#define LOWLIMIT 0.1 +#define HILIMIT 0.85 +#define ONTIME 1.0 + + +enum blinkStates {IDLESTATE, NEWBLINK, LOWLITE, HILITE}; // define the states char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS}; SLCD slcd; //define LCD display globally define @@ -30,9 +35,11 @@ int main() { DigitalOut greenColor(LED_GREEN); DigitalOut redColor(LED_RED); + AnalogIn LightSensor(PTE22); + float lightData; char lcdData[LCDLEN]; Timer LEDTimer; // time till next PWM values is to change. - int PGMState = IDLESTATE; // work till timer transitions + blinkStates PGMState = IDLESTATE; // work till timer transitions int ledState = LEDON; @@ -48,16 +55,44 @@ switch (PGMState){ case IDLESTATE: { if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion - PGMState = NEWBLINK; - } + 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; + } + + } break; } case NEWBLINK: { ledState = !ledState; - redColor = ledState; - greenColor = !ledState; - sprintf(lcdData,logicalString[ledState]); - LCDMess(lcdData); + redColor.write(ledState); + greenColor.write(!ledState); + // 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;