Part 1

Dependencies:   TSI

Revision:
7:002686a28225
Parent:
6:71ef35e456ab
--- a/main.cpp	Thu Feb 20 11:28:36 2020 +0000
+++ b/main.cpp	Mon Mar 16 12:05:45 2020 +0000
@@ -1,63 +1,151 @@
 #include "mbed.h"
 #include "TSISensor.h"
 
-// Example program for lab 5
-// -------------------------
-//  A value is read from the touch sensor and use
-//    to control two LEDs
-//  The value is also output to the serial interface
+//Lab 5 part 1 
+//LED light up according to which button is pressed
 
 Serial pc(USBTX, USBRX); // tx, rx
-DigitalOut redLED(LED_RED);
-DigitalOut greenLED(LED_GREEN);
+DigitalOut led1(PTD3,0);
+DigitalOut led2(PTD5,0);
+DigitalOut led3(PTD0,0);
+DigitalOut led4(PTD2,0);
 TSISensor tsi;
 
-Thread redThread ; // thread for red LED
-Thread greenThread ; // thread for green LED
+Thread l_out ; 
+Thread l_in ; 
+Thread r_out ; 
+Thread r_in ; 
+
+enum pos{neutral, lo, li, ri, ro};
+enum ledonoff{on, off};
+
+# define lout 0x01   
+# define lin 0x02
+# define rin 0x04
+# define rout 0x08   
+
+EventFlags signals;  
 
-# define REDFLAG 0x01   
-# define GREENFLAG 0x02
-EventFlags signals;  // event flags for signalling; 2 used
- 
-void red_thread() {  // method to run in thread
+void leftout() {  
+    int l1=on;
     while (true) {
-        signals.wait_any(REDFLAG);
-        redLED = false ; // turn on
-        ThisThread::sleep_for(5000) ; // wait(5.0);
-        redLED = true ; // turn off 
-        signals.clear(REDFLAG) ;
-          // Signal are automatically cleared by wait_any but
-          // the signal might have been set again while LED on 
+        switch (l1){
+            case on:
+                signals.wait_any(lout);//Wait until flag
+                //pc.printf("lo  ");
+                led1=1;
+                l1=off;//Turn LED on or off
+                signals.clear(lout);
+            break;
+            case off:
+                signals.wait_any(lout);
+                //pc.printf("lo  ");
+                led1=0;
+                l1=on;
+                signals.clear(lout);
+            break;    
+        } 
     }
 }
-
-void green_thread() {  // method to run in thread
+void leftin() {  
+    int l2=on;
+    while (true) {
+        switch (l2){
+            case on:
+                signals.wait_any(lin);
+                //pc.printf("li  ");
+                led2=1;
+                l2=off;
+                signals.clear(lin);
+            break;
+            case off:
+                signals.wait_any(lin);
+                //pc.printf("li  ");
+                led2=0;
+                l2=on;
+                signals.clear(lin);
+            break;    
+        } 
+    }
+}
+void rightin() {  
+    int l3=on;
     while (true) {
-        signals.wait_any(GREENFLAG);
-        greenLED = false ; // turn on 
-        ThisThread::sleep_for(5000) ; // wait(5.0);
-        greenLED = true ; // turn off 
-        signals.clear(GREENFLAG) ;
-          // Signal are automatically cleared by wait_any but
-          // the signal might have been set again while LED on 
+        switch (l3){
+            case on:
+                signals.wait_any(rin);
+                //pc.printf("ri    ");
+                led3=1;
+                l3=off;
+                signals.clear(rin);
+            break;
+            case off:
+                signals.wait_any(rin);
+                //pc.printf("ri   ");
+                led3=0;
+                l3=on;
+                signals.clear(rin);
+            break;    
+        } 
+    }
+}
+void rightout() {  
+    int l4=on;
+    while (true) {
+        switch (l4){
+            case on:
+                signals.wait_any(rout);
+                //pc.printf("ro   ");
+                led4=1;
+                l4=off;
+                signals.clear(rout);
+            break;
+            case off:
+                signals.wait_any(rout);
+                //pc.printf("ro   ");
+                led4=0;
+                l4=on;
+                signals.clear(rout);
+            break;    
+        } 
     }
 }
 
 int main(void) {
-    redLED = true ; // turn off 
-    greenLED = true ; // turn off 
-    redThread.start(red_thread) ; // start the red thread
-    greenThread.start(green_thread) ; // start the green thread
+    int pos=neutral;
+    uint8_t d;
+    pc.printf("START");
+    l_out.start(leftout) ; 
+    l_in.start(leftin) ; 
+    r_in.start(rightin);
+    r_out.start(rightout);
     
     while (true) {
-        uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
+        d = tsi.readDistance() ;  // Distance is between 0 and 39
                                           // When no touch --> 0
                                           // Left --> low value  Right --> high value
+        switch (pos){
+            case neutral:
+                if (d>3 && d<9){pos=lo; signals.set(lout);}    //Set flags and change states
+                if (d>13 && d<19){pos=li; signals.set(lin);} 
+                if (d>23 && d<29){pos=ri; signals.set(rin);} 
+                if (d>33){pos=ro; signals.set(rout);} 
+            break;
+            case lo:
+                if (d<3 || d>9) pos=neutral;
+            break;
+            case li:
+                if (d<13 || d>19) pos=neutral;
+            break;
+            case ri:
+                if (d<23 || d>29) pos=neutral;
+            break;
+            case ro:
+                if (d<33) pos=neutral;
+            break;
+        }
         pc.printf("%d", d) ;  
         pc.putc(' ') ;
-        if (d == 10) signals.set(REDFLAG) ;
-        if (d == 20) signals.set(GREENFLAG) ;
-        ThisThread::sleep_for(500) ; // This polling rate is too slow - increase it
-                    // The slower rate maks it easier to output on the terminal
-    }
+        ThisThread::sleep_for(100) ; 
+        }
 }
\ No newline at end of file