Keisha Brathwaite
/
KBrat-SSD541-HW-5_2
KBrat-SSD541-HW-5_2
Fork of lightsense_kl46z_basic by
Diff: main.cpp
- Revision:
- 5:817aa144563d
- Parent:
- 4:bd42ab18979b
- Child:
- 6:710e18c552f5
diff -r bd42ab18979b -r 817aa144563d main.cpp --- a/main.cpp Thu Sep 11 22:32:51 2014 +0000 +++ b/main.cpp Fri Sep 12 19:46:15 2014 +0000 @@ -7,17 +7,17 @@ #define PROGNAME "blink_kl46z_states v1\n\r" #define LEDON false #define LEDOFF true -#define PWMDWELL 50 // milliseconds +#define LEDTMESS "TRUE" +#define LEDFMESS "FALS" +#define BLINKDWELL 400 // milliseconds #define DFDELTA 0.01 #define PWMTIME 1 // ms (kHz #define LCDLEN 10 -#define RMPUP true -#define RMPDWN false #define NUMSTATES 2 -#define NEWDUTYFACTOR 1 +#define NEWBLINK 1 #define IDLESTATE 0 - +char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS}; SLCD slcd; //define LCD display globally define Serial pc(USBTX, USBRX); @@ -28,46 +28,38 @@ } int main() { - PwmOut greenColor(LED_GREEN); - PwmOut redColor(LED_RED); + DigitalOut greenColor(LED_GREEN); + DigitalOut redColor(LED_RED); char lcdData[LCDLEN]; Timer LEDTimer; // time till next PWM values is to change. - int dfState = IDLESTATE; // work till timer transitions - float dutyFactor = 0.0; - float workingDelta; // hold value of direction of duty factor + int PGMState = IDLESTATE; // work till timer transitions + int ledState = LEDON; + - int timeToChangeDF = PWMDWELL; + int timeToChangeDF = BLINKDWELL; // set up timer for next step of Duty Factor timing LEDTimer.start(); LEDTimer.reset(); pc.printf(PROGNAME); - greenColor.period_ms(PWMTIME); // set the frequency of the pulse train - redColor.period_ms(PWMTIME); // so there is no flickering - workingDelta = DFDELTA; while(true) { - switch (dfState){ + switch (PGMState){ case IDLESTATE: { if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion - dfState = NEWDUTYFACTOR; - LEDTimer.reset(); + PGMState = NEWBLINK; } break; } - case NEWDUTYFACTOR: { - dutyFactor += workingDelta; - if(dutyFactor >= 1.0) workingDelta = -workingDelta; // change direction if needed - if(dutyFactor < DFDELTA) workingDelta = DFDELTA; - redColor.write(dutyFactor); - greenColor.write(1.0 - dutyFactor); - // print to LCD screen - sprintf (lcdData,"%4.3f",dutyFactor); - LCDMess(lcdData); - + case NEWBLINK: { + ledState = !ledState; + redColor = ledState; + greenColor = !ledState; + sprintf(lcdData,logicalString[ledState]); + LCDMess(lcdData); LEDTimer.reset(); // reset the timer - dfState = IDLESTATE; // go idle state + PGMState = IDLESTATE; // go idle state break; } } // end state machine