Alif Ahmed / Mbed OS ADCandticker_with_interrupt_part1
Revision:
8:f6a614ff2041
Parent:
7:af6188d78afe
Child:
9:208f11c83786
--- a/main.cpp	Tue Feb 25 02:14:54 2020 +0000
+++ b/main.cpp	Thu Feb 27 14:08:44 2020 +0000
@@ -16,8 +16,18 @@
 
 volatile int pressEvent = 0 ;
 
-DigitalIn b1(PTD0, PullUp);
-enum buttonPos { up, down, bounce }; // Button positions
+//DigitalIn b1(PTD0, PullUp);
+InterruptIn button(PTD0);  // Pin must be on ports A or D
+
+
+// This function is invoked when then interrupt occurs
+//   Signal that the button has been pressed
+//   Note: bounce may occur 
+void buttonCallback(){
+    pressEvent = 1 ;  
+}
+
+// enum buttonPos { up, down, bounce }; // Button positions
 
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
@@ -71,14 +81,18 @@
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
+    int extra = 50;
     led1 = 1 ; // turn off 
    
     int volts = 0 ;
-    const int threshold = 55 ;
+     int threshold = 55 ;  // 55 * 6 = 330
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
     
-    
+    // write a comment here
+    button.mode(PullUp);             // Ensure button i/p has pull up
+    button.fall(&buttonCallback) ;   // Attach function to falling edge
+
     
     // Start the event queue
     eventThread.start(callback( &queue, &EventQueue::dispatch_forever));
@@ -91,6 +105,10 @@
         if (evt.status == osEventMail) {
             message_t* mess = (message_t*)evt.value.p ;
             volts = (mess->analog * 330) / 0xffff ;
+            if (pressEvent) {
+                pressEvent = 0 ;
+                threshold = volts / 6 ;
+            }
             mailbox.free(mess) ;  // free the message space
             if (volts < threshold) led1 = 0 ; else led1 = 1 ;
             if (volts < threshold * 2) led2 = 0 ; else led2 = 1 ;
@@ -100,30 +118,32 @@
             vToString(volts, vstring) ;
             counter++ ;
             if (counter == 10) {  // limit bandwidth of serial
-               
-               
-                        
-                                if (!b1.read()) {    // now down 
-                                    while(true){
-                                    pc.printf("Button is pressed") ;
-                                    volts = 340;
-                                    if (volts < threshold) led1 = 0 ; else led1 = 1 ;
-                                    if (volts < threshold * 2) led2 = 0 ; else led2 = 1 ;
-                                    if (volts < threshold * 3) led3 = 0 ; else led3 = 1 ;
-                                    if (volts < threshold * 4) led4 = 0 ; else led4 = 1 ;
-                                    if (volts < threshold * 5) led5 = 0 ; else led5 = 1 ;
-                                    
-                                    wait(5);
-                                    
-                                    }
-                                }
-                               
-    
-               
-                    
                 pc.printf(vstring) ;
                 counter = 0 ;
             }
+                
+  //             if (!b1.read()) {    // now down
+  //                  while(true) {
+  //                      pc.printf("Button is pressed") ;
+  //                      osEvent evt = mailbox.get(); // wait for mail
+  //                      if (evt.status == osEventMail) {
+  //                          message_t* mess = (message_t*)evt.value.p ;
+  //                          volts = ((mess->analog * 330) / 0xffff)+extra ;
+  //                          mailbox.free(mess) ;  // free the message space
+  //                          if (volts < threshold) led1 = 0 ;
+  //                          else led1 = 1 ;
+  //                          if (volts < threshold * 2) led2 = 0 ;
+  //                          else led2 = 1 ;
+  //                          if (volts < threshold * 3) led3 = 0 ;
+  //                          else led3 = 1 ;
+  //                          if (volts < threshold * 4) led4 = 0 ;
+  //                          else led4 = 1 ;
+  //                          if (volts < threshold * 5) led5 = 0 ;
+  //                          else led5 = 1 ;
+  //                          vToString(volts, vstring) ;
+//                                                                                                 }                        
+//                                                                        }
+//                                                                 }
         }
     }
 }