Lab 3 the ADC part with the leds

Files at this revision

API Documentation at this revision

Comitter:
anair12345
Date:
Thu Feb 07 13:43:32 2019 +0000
Parent:
4:ebd00f94455a
Commit message:
lab 3 part 1 full

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jan 31 13:53:15 2019 +0000
+++ b/main.cpp	Thu Feb 07 13:43:32 2019 +0000
@@ -1,13 +1,20 @@
-
 // LAB 3 SAMPLE PROGRAM 1
 //   Revised for mbed 5 
 //   Revised to replace Ticker with event queue and thread
 
 #include "mbed.h"
 
+AnalogIn ain(A0) ;          // Analog input
 
-AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(LED_RED);   // Red LED
+DigitalOut led0(PTA13);
+DigitalOut led1(PTD5);
+DigitalOut led2(PTD0);
+DigitalOut led3(PTD2);
+DigitalOut led4(PTD3);
+
+DigitalIn b1(PTA1, PullUp);
+
+//DigitalOut led1(REDLED);   // Red LED
 EventQueue queue;  // creates an event queue, to call read ADC
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
@@ -18,6 +25,7 @@
 // Message type
 typedef struct {
   uint16_t analog; /* Analog input value */
+  int button;
 } message_t;
 
 // Mail box
@@ -29,17 +37,54 @@
 volatile int samples = 0 ;
 volatile uint16_t smoothed = 0 ; 
 
-void readA0() {
+//Thread pollT ; 
+volatile int pressEvent = 0 ;
+
+enum buttonPos { up, down, bounce }; // Button positions
+
+buttonPos pos = up ;
+int bcounter = 0 ;
+
+void readA0() {   
     smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
     samples++ ;
+    
+    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 ;
+        }
+        wait(0.03);
+    
     if (samples == 10) {
         // send to thread
         message_t *mess = mailbox.alloc() ; // may fail but does not block
         if (mess) {
             mess->analog = smoothed ;
+            mess->button = pressEvent ;
             mailbox.put(mess); // fails but does not block if full
+            pressEvent = 0;// clear press
         }
         samples = 0;
+        
     }
 }
 
@@ -58,12 +103,19 @@
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
-    led1 = 1 ; // turn off 
+    led0 = 0 ; // turn off 
+    led1 = 0 ;
+    led2 = 0 ;
+    led3 = 0 ;
+    led4 = 0 ;
+    
     int volts = 0 ;
-    const int threshold = 100 ;
+    int threshold[] = {55,110,165,220,275,330} ;
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
     
+    //pollT.start(callback(polling));
+    
     // Start the event queue
     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
 
@@ -75,8 +127,18 @@
         if (evt.status == osEventMail) {
             message_t* mess = (message_t*)evt.value.p ;
             volts = (mess->analog * 330) / 0xffff ;
+            int mypressEvent = mess->button;
             mailbox.free(mess) ;  // free the message space
-            if (volts > threshold) led1 = 0 ; else led1 = 1 ;
+            if(mypressEvent) {
+                for(int i = 0; i<5;i++){
+                    threshold[i] = (i*volts)/6;
+                }// use volts to reset thresholds
+            } 
+            if (volts > threshold[0] ) led0 = 1 ; else led0 = 0 ;
+            if (volts > threshold[1]) led1 = 1 ; else led1 = 0 ;
+            if (volts > threshold[2]) led2 = 1 ; else led2 = 0 ;
+            if (volts > threshold[3]) led3 = 1 ; else led3 = 0 ;
+            if (volts > threshold[4]) led4 = 1 ; else led4 = 0 ;
             vToString(volts, vstring) ;
             counter++ ;
             if (counter == 10) {  // limit bandwidth of serial