lab3 solution

Fork of ADCandticker_sample by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
cvmanuel
Date:
Fri Feb 09 21:27:18 2018 +0000
Parent:
1:126dd2f5fc2d
Commit message:
lab3 solution

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 126dd2f5fc2d -r 4348f9a6c179 main.cpp
--- a/main.cpp	Wed Jan 24 21:55:43 2018 +0000
+++ b/main.cpp	Fri Feb 09 21:27:18 2018 +0000
@@ -7,11 +7,69 @@
 
 Ticker tick;                // Ticker for reading analog
 AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(LED_RED);   // Red LED
+DigitalOut led1(D8);   // Red LED
+DigitalOut led2(D9);   // Red LED
+DigitalOut led3(D10);   // Red LED
+DigitalOut led4(D11);   // Red LED
+DigitalOut led5(D12);   // Red LED
+
+DigitalIn b1(PTE1,PullUp);
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
 
-// Message type
+Thread pollT ; // thread to poll
+volatile int pressEvent = 0 ;  // Variabe set by the polling thread
+int maxvolt = 330;
+int threshold =55;
+
+
+enum buttonPos { up, down, bounce }; // Button positions
+void polling() {
+    buttonPos pos = up ;
+    int bcounter = 0 ;
+    while (true) {
+        switch (pos) {
+            case up :
+                if (!b1.read()) {    // now down 
+                    pressEvent = 1 ;  // transition occurred
+                    pos = down ;
+                }
+                break ;
+            case down : 
+                if (b1 == 1) { // no longer down
+                    bcounter = 3 ; // wait four cycles
+                    pos = bounce ;
+                }
+                break ;
+            case bounce :
+                if (b1 == 0) { // down again - button has bounced
+                    pos = down ;   // no event
+                } else if (bcounter == 0) {
+                    pos = up ;     // delay passed - reset to up
+                } else {
+                    bcounter-- ;   // continue waiting
+                }
+                break ;
+        }
+        Thread::wait(30);
+    }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// Message type 
 typedef struct {
   uint16_t analog; /* Analog input value */
 } message_t;
@@ -53,12 +111,21 @@
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
-    led1 = 1 ; // turn off 
+    led1 = 0 ; // turn off 
+    led2 = 0 ;
+    led3 = 0 ;
+    led4 = 0 ;
+    led5 = 0 ;
     int volts = 0 ;
-    const int threshold = 100 ;
+    //const int threshold = 55 ;
+    
+    pollT.start(callback(polling));
+    
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
 
+    threshold = maxvolt/6;
+
     tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
     while (true) {
         osEvent evt = mailbox.get(); // wait for mail 
@@ -66,7 +133,66 @@
             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(pressEvent)
+            {
+               pressEvent=0;
+               maxvolt = volts;
+               threshold= maxvolt / 6;
+               led1 = 1;
+                    led2 = 1;
+                    led3 = 1;
+                    led4 = 1;
+                    led5 = 1;
+            }             
+                
+               
+                if(volts >=threshold*5 && volts <= threshold*6)
+                {
+                    led1 = 1;
+                    led2 = 1;
+                    led3 = 1;
+                    led4 = 1;
+                    led5 = 1;
+                }else
+                 if(volts >=threshold*4 && volts <= threshold*5)
+                {
+                    led1 = 1;
+                    led2 = 1;
+                    led3 = 1;
+                    led4 = 1;
+                    led5 = 0;
+                }else if(volts>=threshold*3 && volts <= threshold*4)
+                {
+                    led1 = 1;
+                    led2 = 1;
+                    led3 = 1;
+                    led4 = 0;
+                    led5 = 0;
+                }else
+                if( volts>=threshold*2 && volts <= threshold*3)
+                {
+                    led1 = 1;
+                    led2 = 1;
+                    led3 = 0;
+                    led4 = 0;
+                    led5 = 0;
+                }else
+                 if( volts >threshold && volts <=threshold*2)
+                {
+                    led1 = 1;   
+                    led2 = 0;
+                    led3 = 0;
+                    led4 = 0;
+                    led5 = 0;
+                }
+                else if(volts < threshold){
+                    led1 = 0 ;
+                    led2 = 0 ;
+                    led3 = 0 ;
+                    led5 = 0 ;
+                    led5 = 0 ;
+                    }
+                    
             vToString(volts, vstring) ;
             counter++ ;
             if (counter == 10) {  // limit bandwidth of serial