final

Files at this revision

API Documentation at this revision

Comitter:
ex19397
Date:
Fri Feb 28 09:32:06 2020 +0000
Parent:
5:1f36332ed463
Commit message:
Final

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 06 08:11:53 2020 +0000
+++ b/main.cpp	Fri Feb 28 09:32:06 2020 +0000
@@ -1,17 +1,28 @@
 
 // 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
-DigitalOut led1(LED_RED);   // Red LED
+DigitalOut led1(D3);   // Red LED
+DigitalOut led2(D4);
+DigitalOut led3(D5);
+DigitalOut led4(D6);
+DigitalOut led5(D7);
+InterruptIn b1(PTD0);
+
+
 EventQueue queue;  // creates an event queue, to call read ADC
 
 Serial pc(USBTX, USBRX); // tx, rx, for debugging
 
+volatile int pressEvent = 1 ;
+void buttonCallback(){
+    pressEvent = 1 ;  
+}
+
 // This thread runs the event queue
 Thread eventThread ;
 
@@ -58,11 +69,18 @@
 //   Attach ISR for ticker
 //   Procss messages from mailbox    
 int main() {
-    led1 = 1 ; // turn off 
+    led1 = 0; // turn off 
+    led2 = 0;// turn off 
+    led3 = 0;// turn off 
+    led4 = 0;// turn off 
+    led5 = 0;  // turn off 
     int volts = 0 ;
+    int max = 0 ;
     const int threshold = 100 ;
     int counter = 0 ;
     char vstring[] = "X.XX\r\n" ;
+    b1.mode(PullUp);
+    b1.fall(&buttonCallback);
     
     // Start the event queue
     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
@@ -74,9 +92,57 @@
         osEvent evt = mailbox.get(); // wait for mail 
         if (evt.status == osEventMail) {
             message_t* mess = (message_t*)evt.value.p ;
+             if (pressEvent) {
+                 pressEvent = 0;
+            max = (mess->analog * 330) / 0xffff ;
+        }
             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 > 0 && volts < max/6) {
+                led1 = 0;
+                led2 = 0;
+                led3 = 0;
+                led4 = 0;
+                led5 = 0; 
+                }
+            if (volts > max/6 && volts < 2*max/6) {
+                led1 = 1;
+                led2 = 0;
+                led3 = 0;
+                led4 = 0;
+                led5 = 0; 
+                }
+             if (volts > 2*max/6 && volts < 3*max/6) {
+                led1 = 1;
+                led2 = 1;
+                led3 = 0;
+                led4 = 0;
+                led5 = 0; 
+                }
+             if (volts > 3*max/6 && volts < 4*max/6) {
+                led1 = 1;
+                led2 = 1;
+                led3 = 1;
+                led4 = 0;
+                led5 = 0; 
+                }
+            if (volts > 4*max/6 && volts < 5*max/6) {
+                led1 = 1;
+                led2 = 1;
+                led3 = 1;
+                led4 = 1;
+                led5 = 0; 
+                }
+            if (volts > 5*max/6 && volts < 6*max/6) {
+                led1 = 1;
+                led2 = 1;
+                led3 = 1;
+                led4 = 1;
+                led5 = 1; 
+                }
+                
+                
             vToString(volts, vstring) ;
             counter++ ;
             if (counter == 10) {  // limit bandwidth of serial