Haozun Sun / Mbed OS digitalInInterrupt_hzsun
Revision:
6:f475e8557afb
Parent:
5:86742cfaf4e4
--- a/main.cpp	Thu Jan 30 08:16:04 2020 +0000
+++ b/main.cpp	Wed Feb 12 19:32:52 2020 +0000
@@ -1,36 +1,34 @@
 #include "mbed.h"
 
-// Labs 2: Example program for using an interrupt (or callback)
-// -----------------------------------------------------------
-// A callback function (corresponding to an ISR) is called when a button 
-//    is pressed
-// The callback uses a shared variable to signal another thread
+InterruptIn button(PTD0);                             // Pin must be on ports A or D
+InterruptIn button2(PTD2);                           //another button uses pin PTD2
+DigitalOut led(LED_BLUE);                          // choose blue LED
+DigitalOut led2(LED_RED);                         //choose another RED LED 
 
-InterruptIn button(PTD0);  // Pin must be on ports A or D
-DigitalOut led(LED_GREEN);
+volatile int pressEvent = 0 ;              
+volatile int pressEvent2 = 0 ;
 
-volatile int pressEvent = 0 ;
-
-// This function is invoked when then interrupt occurs
-//   Signal that the button has been pressed
-//   Note: bounce may occur 
-void buttonCallback(){
-    pressEvent = 1 ;  
+void buttonCallback(){                              //when the first button is pressed,change the signal to the opposite value 
+    pressEvent = !pressEvent;  
 }
-
-/*  ---- Main function (default thread) ----
-    Note that if this thread completes, nothing else works
+void buttonCallback2(){                            //when the second button is pressed,change the signal to the opposite value 
+    pressEvent2 = !pressEvent2 ;            
+}
+/*  ---- Main function ----
  */
 int main() {
-    button.mode(PullUp);             // Ensure button i/p has pull up
-    button.fall(&buttonCallback) ;   // Attach function to falling edge
-
+    button.mode(PullUp);                           // Ensure button i/p has pull up
+    button.fall(&buttonCallback) ;                 // Attach function to falling edge
+    button2.mode(PullUp);                          // Ensure the second button i/p has pull up
+    button2.fall(&buttonCallback2) ;               // Attach function to falling edge    
     while(true) {
-        // Toggle the LED every time the button is pressed
+                                                 // Toggle the LED every time the button is pressed 
         if (pressEvent) {
-            led = !led ;
-            pressEvent = 0 ; // Clear the event variable
+            led = !led ;                         // when the signal arrived, change led1
         }
-        ThisThread::sleep_for(100) ; // delay for 100ms 
+        if (pressEvent2) {
+            led2 = !led2 ;                       // when the signal arrived, change led2
+        }               
+        ThisThread::sleep_for(500) ;             // delay for 500ms 
     }
 }
\ No newline at end of file