Modified interesting blinky program for homework 4.1

Dependencies:   SLCD mbed

Fork of blink_kl46z_button_LCD by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
eseifert
Date:
Mon Sep 12 07:34:47 2016 +0000
Parent:
3:fc189dd7ac64
Commit message:
ESeif-SD541-HW4.1

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r fc189dd7ac64 -r 34ddcc34f42b main.cpp
--- a/main.cpp	Wed Aug 24 16:37:09 2016 +0000
+++ b/main.cpp	Mon Sep 12 07:34:47 2016 +0000
@@ -1,20 +1,22 @@
 #include "mbed.h"
 #include "SLCD.h"
 
-#define LEDON false
-#define LEDOFF true
-#define NUMBUTS 2
-#define LBUT PTC12  // port addresses for buttons
-#define RBUT PTC3
-#define BLINKTIME 0.3 // in seconds
-#define BUTTONTIME 0.2
-#define LCDCHARLEN 10
-#define NUMMESS 2
-#define LRED "RED"
-#define LGREEN "GREN"
-#define PRED "RED\r\n"
-#define PGREEN "GREEN\r\n"
-#define PROGNAME "blink_kl46z_buttton LCD v2\r\n"
+#define LEDON           false
+#define LEDOFF          true
+#define NUMBUTS         2
+#define LBUT            PTC12  // port addresses for buttons
+#define RBUT            PTC3
+#define LED_ON_TIME     0.2 // in seconds
+#define LED_OFF_TIME    0.5 // set delay between blinks
+#define BUTTONTIME      0.2
+#define LCD_CH_LEN      5
+#define PC_CHAR_LEN     25  // add new variable for length of pc serial message
+#define NUMMESS         2
+#define L_RED           "RED"
+#define L_GRN           "GRN"
+#define P_RED           "RED BUTTON PUSHED\r\n"       // change red pc serial message
+#define P_GRN           "GREEN BUTTON PUSHED\r\n"     // change green pc serial message
+#define PROGNAME        "blink_kl46z_button LCD v2\r\n"
 
 // slightly more interesting blinky 140814 sc
 SLCD slcd; //define LCD display
@@ -27,59 +29,77 @@
 
 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
 DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED};
-Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons
+Serial pc(USBTX, USBRX);    // set up USB as communication is
+// to Host PC via USB connectons
 
-void allLEDsOff(){
+void allLEDsOff()
+{
     int i;
-    for (i=0; i<NUMBUTS; i++){ 
-        LEDs[i] = LEDOFF;    
+    for (i=0; i<NUMBUTS; i++) {
+        LEDs[i] = LEDOFF;
     }
 }
- 
-void LCDMess(char *lMess){
-        slcd.Home();
-        slcd.clear();
-        slcd.printf(lMess);
+
+void LCDMess(char *lMess)
+{
+    slcd.Home();
+    slcd.clear();
+    slcd.printf(lMess);
 }
 
-void initialize_global_vars(){
+void initialize_global_vars()
+{
     pc.printf(PROGNAME);
     // set up DAQ timers
     ButtonTimer.start();
     ButtonTimer.reset();
     LEDTimer.start();
-    LEDTimer.reset();  
+    LEDTimer.reset();
     allLEDsOff();
-} 
+}
 // --------------------------------
- int main() {
-    int i; 
+int main()
+{
+    int i;
     int currentLED = 0;
-    char rMess[NUMMESS][LCDCHARLEN]={LGREEN, LRED}; // for LCD
-    char pMess[NUMMESS][LCDCHARLEN]={PRED, PGREEN}; // for pc serial port
-
+    char rMess[NUMMESS][LCD_CH_LEN]= {L_GRN, L_RED}; // for LCD
+    char pMess[NUMMESS][PC_CHAR_LEN]= {P_GRN, P_RED}; // for pc serial port
+    // reverse these message so that the
+    // correct one prints to serial port
     initialize_global_vars(); //keep things organized
     LEDs[currentLED] = LEDON;
     LCDMess(rMess[currentLED]);
     pc.printf(pMess[currentLED]);
-// End of setup
+    // End of setup
+
     while(true) {
-        if (ButtonTimer > BUTTONTIME){
-            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
-                if(!buttons[i]) { 
-                    allLEDsOff();  
+        if (ButtonTimer > BUTTONTIME) {
+            for (i = 0; i < NUMBUTS; i++) { // index will be 0 or 1
+                if(!buttons[i]) {
+                    allLEDsOff();
                     LCDMess(rMess[i]);
-                    pc.printf(pMess[i]); 
+                    pc.printf(pMess[i]);
                     currentLED = i;
                 } // if ! buttons
             }// for loop to look at buttons
             ButtonTimer.reset();
         }
-        if(LEDTimer.read() > BLINKTIME){
-            LEDTimer.reset();               
-            ledState = !ledState; // Flip the general state
-            LEDs[currentLED] = ledState;
+
+        if(ledState == 0){ // switch on LED state
+            if(LEDTimer.read() > LED_ON_TIME) {
+                // if LED state is low wait 500 ms before change
+                LEDTimer.reset();
+                ledState = !ledState; // Flip the general state
+                LEDs[currentLED] = ledState;
+            }
+        } else {
+            // if LED state is not low wait 200 ms before change
+            if(LEDTimer.read() > LED_OFF_TIME) {
+                LEDTimer.reset();
+                ledState = !ledState; // Flip the general state
+                LEDs[currentLED] = ledState;
+            }
         }
-    // Do other things here between times of reading and flashing
+        // Do other things here between times of reading and flashing
     }
 }
\ No newline at end of file