polling

Files at this revision

API Documentation at this revision

Comitter:
hzsun
Date:
Wed Feb 12 19:42:16 2020 +0000
Parent:
4:82f7ccb294d3
Commit message:
polling

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jan 30 08:04:56 2020 +0000
+++ b/main.cpp	Wed Feb 12 19:42:16 2020 +0000
@@ -1,65 +1,63 @@
 #include "mbed.h"
 
-// Labs 2: Example program for polling an input
+// Labs 2:  polling 
 // --------------------------------------------
-// The program uses a thread to poll a digital input
-//   - The thread monitors the position of the button
-//   - When the button transitions up and down, a press event is signaled 
-//   - Button bounce is guarded against
-// A second thread (the default one) checks for the press event and toggles the LED
 
-DigitalIn b1(PTD0, PullUp);
-DigitalOut led(LED1);
+DigitalIn b1(PTD0, PullUp);             // initialize the button  
+DigitalOut led(LED1);                   // initialize the led
 
-Thread pollT ; // thread to poll
-volatile int pressEvent = 0 ;  // Variabe set by the polling thread
-  // Sharing a global variable between two threads is not safe in general
-  //  but it can be ok if a) update is atomic and b) only one thread writes
+Thread pollT ;                            // thread to poll
+volatile int pressEvent = 0 ;             // Variabe set by the polling thread
+
 
-enum buttonPos { up, down, bounce }; // Button positions
-void polling() {
-    buttonPos pos = up ;
-    int bcounter = 0 ;
+enum buttonPos { up, down, bounce };     // Button positions
+void polling() {                       
+    buttonPos pos = up ;                  //initialize the button  position 
+    int bcounter = 0 ;                    //initialize the counter
     while (true) {
-        ThisThread::sleep_for(30) ; // poll every 30ms
+        ThisThread::sleep_for(30) ;       // poll every 30ms
         switch (pos) {
             case up :
-                if (!b1.read()) {    // now down 
-                    pressEvent = 1 ;  // transition occurred
-                    pos = down ;
+                if (!b1.read()) {            // now down 
+                    pressEvent = 1 ;        // transition occurred
+                    pos = down ;            // change to down position 
                 }
                 break ;
             case down : 
-                if (b1 == 1) { // no longer down
-                    bcounter = 3 ; // wait four cycles
-                    pos = bounce ;
+                if (b1 == 1) {                  // no longer down
+                    bcounter = 3 ;              // wait four cycles
+                    pos = bounce ;              //change to bounce position
                 }
                 break ;
             case bounce :
-                if (b1 == 0) { // down again - button has bounced
-                    pos = down ;   // no event
+                if (b1 == 0) {                // down again - button has bounced
+                    pos = down ;                 // no event
                 } else if (bcounter == 0) {
-                    pos = up ;     // delay passed - reset to up
+                    pos = up ;                  // delay passed - reset to up
                 } else {
-                    bcounter-- ;   // continue waiting
+                    bcounter-- ;               // continue waiting
                 }
                 break ;
         }
     }
 }
 
-/*  ---- Main function (default thread) ----
-    Note that if this thread completes, nothing else works
+/*  ---- Main function (----
  */
 int main() {
-    led = 1 ;  // Initially off
+    led = 0 ;                                       // Initially off
     pollT.start(callback(polling));
-
+    int i=0;                                       //create a counter for controling the speed of LED
+    int flashspeed[]={200, 400, 600, 800, 1000};    //input the value for chaging the speed of LED
     while(true) {
         if (pressEvent) {
-            pressEvent = 0 ; // clear the event variable
-            led = !led ;
+            pressEvent = 0 ;                            // clear the event variable
+            i++ ;                                       // counter add one 
+            if (i>4){                                   // when counter is bigger than four change back to zero
+            i=0; 
+            }
         }
-        ThisThread::sleep_for(100) ; // delay for 100ms 
+        led =!led;                                    //  led blinks 
+        ThisThread::sleep_for(flashspeed[i]) ;        //   delay for 200ms 400ms 600ms 800ms 1s then come back to 200ms
     }
 }
\ No newline at end of file