HW5.2

Dependencies:   blink_kl46z_button_nowait mbed

Fork of blink_kl46z_button_nowait by Stanley Cohen

Revision:
6:cacef74ca13e
Parent:
5:dc506f422393
--- a/main.cpp	Thu Sep 24 15:08:25 2015 +0000
+++ b/main.cpp	Mon Sep 28 16:54:43 2015 +0000
@@ -7,28 +7,32 @@
 #define RBUT PTC3
 #define BLINKTIME 0.3 // in seconds
 #define BUTTONTIME 0.2
-#define LCDCHARLEN 10
-#define NUMMESS 2
 
-#define PROGNAME "blink_kl46z_buttton no wait\r\n"
+#define PROGNAME "DEstrada-HW-5.2-Interrupt and Timout\r\n"
 
 // Timer to elliminate wait() function
 Timer LEDTimer; // for blinking LEDs
 Timer ButtonTimer; // for reading button states
-
-bool ledState = LEDON;
+Timeout blinker;
 
-DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
-DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED};
+bool ledState = LEDON; // false
+
+DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};          // buttons[2] 
+DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED};    // LEDs[2]
 Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons
 
+//interrupts
+InterruptIn rtButton(RBUT);  
+InterruptIn lButton(LBUT); 
+
+//keept this
 void allLEDsOff(){
     int i;
     for (i=0; i<NUMBUTS; i++){ 
         LEDs[i] = LEDOFF;    
     }
 }
- 
+
 
 void initialize_global_vars(){
     pc.printf(PROGNAME);
@@ -37,32 +41,49 @@
     ButtonTimer.reset();
     LEDTimer.start();
     LEDTimer.reset();  
-    allLEDsOff();
+    allLEDsOff(); // makes sure LEDS are off
 } 
+
+//global variable
+int currentLED = 0;
+
+
+// interrupt 
+void rtButtonPressed(){
+    allLEDsOff(); 
+    currentLED = 0;
+}
+
+// interrupt 
+void lButtonPressed(){
+    allLEDsOff(); 
+    currentLED = 1;   
+}
+
+//Timeout call
+ void blink() {
+    ledState = !ledState; // Flip the general state
+    LEDs[currentLED] = ledState;
+}
+
+
+
 // --------------------------------
  int main() {
-    int i; 
-    int currentLED = 0;
-    
     initialize_global_vars(); //keep things organized
     LEDs[currentLED] = LEDON;
-// End of setup
+    // End of setup
+
+    //Interrupts
+    rtButton.fall(&rtButtonPressed);
+    lButton.fall(&lButtonPressed);
+     
 
     while(true) {
-        if (ButtonTimer > BUTTONTIME){
-            for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 
-                if(!buttons[i]) { 
-                    allLEDsOff();  
-                    currentLED = i;
-                } // if ! buttons
-            }// for loop to look at buttons
-            ButtonTimer.reset();
+        
+        if(LEDTimer.read() > BLINKTIME){
+            LEDTimer.reset();  
+            blinker.attach(&blink, 0.2);             
         }
-        if(LEDTimer.read() > BLINKTIME){
-            LEDTimer.reset();               
-            ledState = !ledState; // Flip the general state
-            LEDs[currentLED] = ledState;
-        }
-    // Do other things here between times of reading and flashing
     }
 }
\ No newline at end of file