Alif Ahmed / Mbed OS ADCandticker_with_interrupt_part1
Revision:
7:af6188d78afe
Parent:
6:2b04d2caab1f
Child:
8:f6a614ff2041
--- a/main.cpp	Thu Feb 20 10:04:01 2020 +0000
+++ b/main.cpp	Tue Feb 25 02:14:54 2020 +0000
@@ -7,9 +7,19 @@
 
 
 AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(LED_RED);   // Red LED
+DigitalOut led1(D2);   // Red LED
+DigitalOut led2(D3);   // Red LED
+DigitalOut led3(D4);   // Green LED
+DigitalOut led4(D5);   // Red LED
+DigitalOut led5(D6);   // Green LED
 EventQueue queue;  // creates an event queue, to call read ADC
 
+volatile int pressEvent = 0 ;
+
+DigitalIn b1(PTD0, PullUp);
+enum buttonPos { up, down, bounce }; // Button positions
+
+
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
 
 // This thread runs the event queue
@@ -30,6 +40,7 @@
 volatile uint16_t smoothed = 0 ; 
 
 void readA0() {
+    //pollT.start(callback(polling));
     smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
     samples++ ;
     if (samples == 10) {
@@ -41,6 +52,7 @@
         }
         samples = 0;
     }
+    
 }
 
 // Write voltage digits
@@ -53,19 +65,23 @@
     s[0] = '0' + (v % 10) ;
 }
 
+
 // Main program
 //   Initialise variables
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
     led1 = 1 ; // turn off 
+   
     int volts = 0 ;
-    const int threshold = 100 ;
+    const int threshold = 55 ;
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
     
+    
+    
     // Start the event queue
-    eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
+    eventThread.start(callback( &queue, &EventQueue::dispatch_forever));
 
     // call the readA0 function every 10ms 
     queue.call_every(10, readA0) ; 
@@ -76,10 +92,35 @@
             message_t* mess = (message_t*)evt.value.p ;
             volts = (mess->analog * 330) / 0xffff ;
             mailbox.free(mess) ;  // free the message space
-            if (volts > threshold) led1 = 0 ; else led1 = 1 ;
+            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) ;
             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 ;
             }