ADCandticker_lab-3

Fork of ADCandticker_lab-3_edwin by Edwin Kadavy

Revision:
3:ac78d4a2dd3f
Parent:
2:fe6ada70739c
diff -r fe6ada70739c -r ac78d4a2dd3f main.cpp
--- a/main.cpp	Thu Feb 01 18:05:47 2018 +0000
+++ b/main.cpp	Sun Feb 11 21:00:45 2018 +0000
@@ -4,14 +4,14 @@
 
 #include "mbed.h"
 
-
 Ticker tick;                // Ticker for reading analog
 AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(PTA13);   // Red LED
-DigitalOut led2(PTD3);   // Red LED
-DigitalOut led3(PTD2);   // Red LED
-DigitalOut led4(PTD0);   // Red LED
-DigitalOut led5(PTD5);   // Red LED
+DigitalIn b1(PTC9, PullUp);
+DigitalOut led1(PTA13);  // Red LED
+DigitalOut led2(PTD5);   // Yellow LED
+DigitalOut led3(PTD0);   // Green LED
+DigitalOut led4(PTD2);   // Green LED
+DigitalOut led5(PTD3);   // Green LED
 
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
@@ -24,6 +24,7 @@
 // Mail box
 Mail<message_t, 2> mailbox;
 
+
 // Function called every 10ms to read ADC
 // Low pass filter
 // Every 10th value is sent to mailbox
@@ -54,6 +55,40 @@
     v = v / 10 ;
     s[0] = '0' + (v % 10) ;
 }
+Thread pollV;            // thread to set new max voltage
+volatile int pressEvent = 0 ;  // Variabe set by the polling thread
+enum buttonPos { up, down, bounce }; // Button positions
+
+void switchPoll(){
+        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);
+    }        
+    }
 
 // Main program
 //   Initialise variables
@@ -61,8 +96,10 @@
 //   Procss messages from mailbox
 int main()
 {
-    led1 = 1 ; // turn off
+    pollV.start(callback(switchPoll));
+    //led1 = 1 ; // turn off
     int volts = 0 ;
+    int newVolts=330;
     const int threshold = 100 ;
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
@@ -73,32 +110,36 @@
         if (evt.status == osEventMail) {
             message_t* mess = (message_t*)evt.value.p ;
             volts = (mess->analog * 330) / 0xffff ;
+            if (pressEvent) {
+                pressEvent = 0 ; // clear the event variable
+                newVolts=volts;
+            }
             mailbox.free(mess) ;  // free the message space
-            if (volts > 55 && volts < 109) {
+            if (volts >= ((newVolts/6)*1) && volts < ((newVolts/6)*2)) {
                 led1 = 1 ;
                 led2 = 0 ;
                 led3 = 0 ;
                 led4 = 0 ;
                 led5 = 0 ;
-            } else if (volts > 110 && volts < 164) {
+            } else if (volts >= ((newVolts/6)*2) && volts < ((newVolts/6)*3)) {
                 led1 = 1 ;
                 led2 = 1 ;
                 led3 = 0 ;
                 led4 = 0 ;
                 led5 = 0 ;
-            } else if (volts > 165 && volts < 219) {
+            } else if (volts >= ((newVolts/6)*3) && volts < ((newVolts/6)*4)) {
                 led1 = 1 ;
                 led2 = 1 ;
                 led3 = 1 ;
                 led4 = 0 ;
                 led5 = 0 ;
-            } else if (volts > 220 && volts < 274) {
+            } else if (volts >= ((newVolts/6)*4) && volts < ((newVolts/6)*5)) {
                 led1 = 1 ;
                 led2 = 1 ;
                 led3 = 1 ;
                 led4 = 1 ;
                 led5 = 0 ;
-            } else if (volts > 275) {
+            } else if (volts >= ((newVolts/6)*5)) {
                 led1 = 1 ;
                 led2 = 1 ;
                 led3 = 1 ;