HW_5.2_pt2

Dependencies:   SLCD TSI mbed

Fork of lightsense_kl46z_basic by Stanley Cohen

Committer:
scohennm
Date:
Thu Sep 11 22:32:51 2014 +0000
Revision:
4:bd42ab18979b
Parent:
3:64e28ee5719b
Child:
5:817aa144563d
Created a true state machine for NMHU class program HW sample

Who changed what in which revision?

UserRevisionLine numberNew 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
scohennm 4:bd42ab18979b 21 SLCD slcd; //define LCD display globally define
scohennm 1:51f8c2b04ce2 22 Serial pc(USBTX, USBRX);
scohennm 1:51f8c2b04ce2 23
scohennm 1:51f8c2b04ce2 24 void LCDMess(char *lMess){
scohennm 1:51f8c2b04ce2 25 slcd.Home();
scohennm 1:51f8c2b04ce2 26 slcd.clear();
scohennm 1:51f8c2b04ce2 27 slcd.printf(lMess);
scohennm 1:51f8c2b04ce2 28 }
scohennm 0:e23fffd4b9a7 29
scohennm 0:e23fffd4b9a7 30 int main() {
scohennm 4:bd42ab18979b 31 PwmOut greenColor(LED_GREEN);
scohennm 4:bd42ab18979b 32 PwmOut redColor(LED_RED);
scohennm 1:51f8c2b04ce2 33 char lcdData[LCDLEN];
scohennm 4:bd42ab18979b 34 Timer LEDTimer; // time till next PWM values is to change.
scohennm 4:bd42ab18979b 35 int dfState = IDLESTATE; // work till timer transitions
scohennm 4:bd42ab18979b 36 float dutyFactor = 0.0;
scohennm 4:bd42ab18979b 37 float workingDelta; // hold value of direction of duty factor
scohennm 4:bd42ab18979b 38
scohennm 3:64e28ee5719b 39 int timeToChangeDF = PWMDWELL;
scohennm 3:64e28ee5719b 40 // set up timer for next step of Duty Factor timing
scohennm 3:64e28ee5719b 41 LEDTimer.start();
scohennm 3:64e28ee5719b 42 LEDTimer.reset();
scohennm 1:51f8c2b04ce2 43 pc.printf(PROGNAME);
scohennm 1:51f8c2b04ce2 44
scohennm 2:c016448d89b2 45 greenColor.period_ms(PWMTIME); // set the frequency of the pulse train
scohennm 3:64e28ee5719b 46 redColor.period_ms(PWMTIME); // so there is no flickering
scohennm 3:64e28ee5719b 47
scohennm 4:bd42ab18979b 48 workingDelta = DFDELTA;
scohennm 4:bd42ab18979b 49
scohennm 0:e23fffd4b9a7 50 while(true) {
scohennm 4:bd42ab18979b 51 switch (dfState){
scohennm 4:bd42ab18979b 52 case IDLESTATE: {
scohennm 4:bd42ab18979b 53 if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
scohennm 4:bd42ab18979b 54 dfState = NEWDUTYFACTOR;
scohennm 4:bd42ab18979b 55 LEDTimer.reset();
scohennm 4:bd42ab18979b 56 }
scohennm 4:bd42ab18979b 57 break;
scohennm 4:bd42ab18979b 58 }
scohennm 4:bd42ab18979b 59 case NEWDUTYFACTOR: {
scohennm 4:bd42ab18979b 60 dutyFactor += workingDelta;
scohennm 4:bd42ab18979b 61 if(dutyFactor >= 1.0) workingDelta = -workingDelta; // change direction if needed
scohennm 4:bd42ab18979b 62 if(dutyFactor < DFDELTA) workingDelta = DFDELTA;
scohennm 4:bd42ab18979b 63 redColor.write(dutyFactor);
scohennm 4:bd42ab18979b 64 greenColor.write(1.0 - dutyFactor);
scohennm 4:bd42ab18979b 65 // print to LCD screen
scohennm 4:bd42ab18979b 66 sprintf (lcdData,"%4.3f",dutyFactor);
scohennm 4:bd42ab18979b 67 LCDMess(lcdData);
scohennm 4:bd42ab18979b 68
scohennm 4:bd42ab18979b 69 LEDTimer.reset(); // reset the timer
scohennm 4:bd42ab18979b 70 dfState = IDLESTATE; // go idle state
scohennm 4:bd42ab18979b 71 break;
scohennm 4:bd42ab18979b 72 }
scohennm 4:bd42ab18979b 73 } // end state machine
scohennm 4:bd42ab18979b 74
scohennm 1:51f8c2b04ce2 75 #ifdef PRINTDEBUG
scohennm 1:51f8c2b04ce2 76 pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta);
scohennm 1:51f8c2b04ce2 77 #endif
scohennm 4:bd42ab18979b 78 }// emd while
scohennm 4:bd42ab18979b 79 }