Alif Ahmed / Mbed OS digitalInPolling_sample_program
Revision:
4:82f7ccb294d3
Parent:
3:8d87cbabe37e
Child:
5:f95f17441dab
--- a/main.cpp	Tue Jan 15 09:47:06 2019 +0000
+++ b/main.cpp	Thu Jan 30 08:04:56 2020 +0000
@@ -13,12 +13,15 @@
 
 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
 
 enum buttonPos { up, down, bounce }; // Button positions
 void polling() {
     buttonPos pos = up ;
     int bcounter = 0 ;
     while (true) {
+        ThisThread::sleep_for(30) ; // poll every 30ms
         switch (pos) {
             case up :
                 if (!b1.read()) {    // now down 
@@ -42,7 +45,6 @@
                 }
                 break ;
         }
-        wait(0.03);
     }
 }
 
@@ -58,6 +60,6 @@
             pressEvent = 0 ; // clear the event variable
             led = !led ;
         }
-        Thread::wait(0.1) ;
+        ThisThread::sleep_for(100) ; // delay for 100ms 
     }
 }
\ No newline at end of file