AMart_ssd447_hw3.2

Dependencies:   SLCD mbed

Fork of lightsense_kl46z_PWM_simple by Stanley Cohen

Committer:
scohennm
Date:
Wed Sep 17 18:20:10 2014 +0000
Revision:
7:8d7089b514ae
Parent:
6:710e18c552f5
Child:
8:ea818c9220fc
Demo with no wait() function for NMHU SSD 341

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 6:710e18c552f5 5 // An State Machine solution to HW 4.1 Q3
scohennm 6:710e18c552f5 6 // using wait() for high and low limits.
scohennm 1:51f8c2b04ce2 7 //#define PRINTDEBUG
scohennm 7:8d7089b514ae 8 #define PROGNAME "blink_kl46z_states nowait v1\n\r"
scohennm 0:e23fffd4b9a7 9 #define LEDON false
scohennm 0:e23fffd4b9a7 10 #define LEDOFF true
scohennm 5:817aa144563d 11 #define LEDTMESS "TRUE"
scohennm 5:817aa144563d 12 #define LEDFMESS "FALS"
scohennm 5:817aa144563d 13 #define BLINKDWELL 400 // milliseconds
scohennm 7:8d7089b514ae 14 #define LIMITSTAYON 1000
scohennm 1:51f8c2b04ce2 15 #define DFDELTA 0.01
scohennm 1:51f8c2b04ce2 16 #define PWMTIME 1 // ms (kHz
scohennm 1:51f8c2b04ce2 17 #define LCDLEN 10
scohennm 1:51f8c2b04ce2 18 #define NUMSTATES 2
scohennm 6:710e18c552f5 19 #define LOWLIMIT 0.1
scohennm 6:710e18c552f5 20 #define HILIMIT 0.85
scohennm 6:710e18c552f5 21 #define ONTIME 1.0
scohennm 6:710e18c552f5 22
scohennm 6:710e18c552f5 23
scohennm 6:710e18c552f5 24 enum blinkStates {IDLESTATE, NEWBLINK, LOWLITE, HILITE}; // define the states
scohennm 0:e23fffd4b9a7 25
scohennm 5:817aa144563d 26 char logicalString [NUMSTATES][LCDLEN] = {LEDFMESS, LEDTMESS};
scohennm 4:bd42ab18979b 27 SLCD slcd; //define LCD display globally define
scohennm 1:51f8c2b04ce2 28 Serial pc(USBTX, USBRX);
scohennm 1:51f8c2b04ce2 29
scohennm 1:51f8c2b04ce2 30 void LCDMess(char *lMess){
scohennm 1:51f8c2b04ce2 31 slcd.Home();
scohennm 1:51f8c2b04ce2 32 slcd.clear();
scohennm 1:51f8c2b04ce2 33 slcd.printf(lMess);
scohennm 1:51f8c2b04ce2 34 }
scohennm 0:e23fffd4b9a7 35
scohennm 0:e23fffd4b9a7 36 int main() {
scohennm 5:817aa144563d 37 DigitalOut greenColor(LED_GREEN);
scohennm 5:817aa144563d 38 DigitalOut redColor(LED_RED);
scohennm 6:710e18c552f5 39 AnalogIn LightSensor(PTE22);
scohennm 6:710e18c552f5 40 float lightData;
scohennm 1:51f8c2b04ce2 41 char lcdData[LCDLEN];
scohennm 4:bd42ab18979b 42 Timer LEDTimer; // time till next PWM values is to change.
scohennm 6:710e18c552f5 43 blinkStates PGMState = IDLESTATE; // work till timer transitions
scohennm 5:817aa144563d 44 int ledState = LEDON;
scohennm 5:817aa144563d 45
scohennm 4:bd42ab18979b 46
scohennm 5:817aa144563d 47 int timeToChangeDF = BLINKDWELL;
scohennm 3:64e28ee5719b 48 // set up timer for next step of Duty Factor timing
scohennm 3:64e28ee5719b 49 LEDTimer.start();
scohennm 3:64e28ee5719b 50 LEDTimer.reset();
scohennm 1:51f8c2b04ce2 51 pc.printf(PROGNAME);
scohennm 1:51f8c2b04ce2 52
scohennm 3:64e28ee5719b 53
scohennm 4:bd42ab18979b 54
scohennm 0:e23fffd4b9a7 55 while(true) {
scohennm 5:817aa144563d 56 switch (PGMState){
scohennm 4:bd42ab18979b 57 case IDLESTATE: {
scohennm 4:bd42ab18979b 58 if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
scohennm 6:710e18c552f5 59 lightData = (1.0 - LightSensor.read());
scohennm 6:710e18c552f5 60 sprintf(lcdData,"%4.3f",lightData);
scohennm 6:710e18c552f5 61 LCDMess(lcdData);
scohennm 7:8d7089b514ae 62 timeToChangeDF = BLINKDWELL;
scohennm 6:710e18c552f5 63 if (lightData < LOWLIMIT){ // find appropriate state
scohennm 6:710e18c552f5 64 PGMState = LOWLITE;
scohennm 7:8d7089b514ae 65 timeToChangeDF = LIMITSTAYON;
scohennm 6:710e18c552f5 66 } else if (lightData > HILIMIT){
scohennm 6:710e18c552f5 67 PGMState = HILITE;
scohennm 7:8d7089b514ae 68 timeToChangeDF = LIMITSTAYON;
scohennm 6:710e18c552f5 69 } else {
scohennm 6:710e18c552f5 70 PGMState = NEWBLINK;
scohennm 7:8d7089b514ae 71 timeToChangeDF = BLINKDWELL;
scohennm 6:710e18c552f5 72 }
scohennm 6:710e18c552f5 73
scohennm 6:710e18c552f5 74 }
scohennm 4:bd42ab18979b 75 break;
scohennm 4:bd42ab18979b 76 }
scohennm 5:817aa144563d 77 case NEWBLINK: {
scohennm 5:817aa144563d 78 ledState = !ledState;
scohennm 6:710e18c552f5 79 redColor.write(ledState);
scohennm 6:710e18c552f5 80 greenColor.write(!ledState);
scohennm 6:710e18c552f5 81 // create string for display on LCD
scohennm 6:710e18c552f5 82
scohennm 6:710e18c552f5 83 LEDTimer.reset(); // reset the timer
scohennm 6:710e18c552f5 84 PGMState = IDLESTATE; // go idle state
scohennm 6:710e18c552f5 85 break;
scohennm 6:710e18c552f5 86 }
scohennm 6:710e18c552f5 87 case HILITE: {
scohennm 6:710e18c552f5 88 redColor.write(LEDON);
scohennm 6:710e18c552f5 89 greenColor.write(LEDOFF);
scohennm 6:710e18c552f5 90 // create string for display on LCD
scohennm 7:8d7089b514ae 91 if (LEDTimer.read_ms() > timeToChangeDF){
scohennm 7:8d7089b514ae 92 LEDTimer.reset(); // reset the timer
scohennm 7:8d7089b514ae 93 PGMState = IDLESTATE; // go idle state
scohennm 7:8d7089b514ae 94 }
scohennm 6:710e18c552f5 95 break;
scohennm 6:710e18c552f5 96 }
scohennm 6:710e18c552f5 97 case LOWLITE: {
scohennm 6:710e18c552f5 98 redColor.write(LEDOFF);
scohennm 6:710e18c552f5 99 greenColor.write(LEDON);
scohennm 6:710e18c552f5 100 // create string for display on LCD
scohennm 7:8d7089b514ae 101 if (LEDTimer.read_ms() > timeToChangeDF){
scohennm 7:8d7089b514ae 102 LEDTimer.reset(); // reset the timer
scohennm 7:8d7089b514ae 103 PGMState = IDLESTATE; // go idle state
scohennm 7:8d7089b514ae 104 }
scohennm 4:bd42ab18979b 105 break;
scohennm 4:bd42ab18979b 106 }
scohennm 4:bd42ab18979b 107 } // end state machine
scohennm 4:bd42ab18979b 108
scohennm 1:51f8c2b04ce2 109 #ifdef PRINTDEBUG
scohennm 1:51f8c2b04ce2 110 pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta);
scohennm 1:51f8c2b04ce2 111 #endif
scohennm 4:bd42ab18979b 112 }// emd while
scohennm 4:bd42ab18979b 113 }