umar saeed / Mbed OS TSI_Complete

Dependencies:   TSI

Files at this revision

API Documentation at this revision

Comitter:
ec19664
Date:
Thu Mar 19 17:35:19 2020 +0000
Parent:
6:71ef35e456ab
Commit message:
TSI Complete

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r 71ef35e456ab -r b62f800f9b6c main.cpp
--- a/main.cpp	Thu Feb 20 11:28:36 2020 +0000
+++ b/main.cpp	Thu Mar 19 17:35:19 2020 +0000
@@ -8,56 +8,127 @@
 //  The value is also output to the serial interface
 
 Serial pc(USBTX, USBRX); // tx, rx
-DigitalOut redLED(LED_RED);
-DigitalOut greenLED(LED_GREEN);
+DigitalOut blueLED(D5);   // first LED
+DigitalOut greenLED(D6);   // second LED
+DigitalOut yellowLED(D7); // third led
+DigitalOut redLED(D8);    // fourth led
 TSISensor tsi;
 
 Thread redThread ; // thread for red LED
 Thread greenThread ; // thread for green LED
+Thread blueThread ; // thread for blue LED
+Thread yellowThread ; // thread for yellow LED
 
-# define REDFLAG 0x01   
+# define BLUEFLAG 0x01
 # define GREENFLAG 0x02
+# define YELLOWFLAG 0x04
+# define REDFLAG 0x08
+
 EventFlags signals;  // event flags for signalling; 2 used
- 
-void red_thread() {  // method to run in thread
+
+void blue_thread()    // method to run in thread
+{
+    while (true) {
+        signals.wait_any(BLUEFLAG); // wait for signal
+        blueLED = !blueLED ;  // toggle
+        ThisThread::sleep_for(1000) ; // wait(1.0);
+        signals.clear(BLUEFLAG) ;
+        // Signal are automatically cleared by wait_any but
+        // the signal might have been set again while LED on
+    }
+}
+
+void green_thread()    // method to run in thread
+{
+    while (true) {
+        signals.wait_any(GREENFLAG);
+        greenLED = !greenLED ; // toggle
+        ThisThread::sleep_for(1000) ; // wait(1.0);
+        signals.clear(GREENFLAG) ;
+        // Signal are automatically cleared by wait_any but
+        // the signal might have been set again while LED on
+    }
+}
+void yellow_thread()    // method to run in thread
+{
+    while (true) {
+        signals.wait_any(YELLOWFLAG);
+        yellowLED = !yellowLED ; // toggle
+        ThisThread::sleep_for(1000) ; // wait(1.0);
+        signals.clear(YELLOWFLAG) ;
+        // Signal are automatically cleared by wait_any but
+        // the signal might have been set again while LED on
+    }
+}
+void red_thread()    // method to run in thread
+{
     while (true) {
         signals.wait_any(REDFLAG);
-        redLED = false ; // turn on
-        ThisThread::sleep_for(5000) ; // wait(5.0);
-        redLED = true ; // turn off 
+        redLED = !redLED ; // toggle
+        ThisThread::sleep_for(1000) ; // wait(1.0);
         signals.clear(REDFLAG) ;
-          // Signal are automatically cleared by wait_any but
-          // the signal might have been set again while LED on 
+        // Signal are automatically cleared by wait_any but
+        // the signal might have been set again while LED on
     }
 }
 
-void green_thread() {  // method to run in thread
+
+int main(void)
+{
+    redLED = 0 ; // turn off
+    greenLED = 0 ; // turn off
+    blueLED = 0 ;
+    yellowLED = 0;
+    redThread.start(red_thread) ; // start the red thread
+    greenThread.start(green_thread) ; // start the green thread
+    blueThread.start(blue_thread) ;  // start the blue thread
+    yellowThread.start(yellow_thread) ;  // start the yellow thread
+
+    enum state {None, outerleft, innerleft, innerright, outerright};
+    int state = None;
     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 
+        uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
+        // When no touch --> 0
+        // Left --> low value  Right --> high value
+        pc.printf("%d", d) ;
+        pc.putc(' ') ;
+
+
+        switch(state) {
+            case None:
+                if (d > 3 && d < 9) {
+                    signals.set(BLUEFLAG);  // if the tsi value is within 3 and 9, set the blue flag, which turns on the blue led, the flag is mentioned within the led thread, that controls the on and off procedure.
+                    state = outerleft;
+                }
+                if (d > 13 && d < 19) {
+                    signals.set(GREENFLAG);  // if the tsi value is within 13 and 19, set the green flag, which turns on the green led.
+                    state = innerleft;
+                }
+                if (d > 23 && d < 29) {
+                    signals.set(YELLOWFLAG);  // if the tsi value is within 23 and 29, set the yellow flag, which turns on the yellow led.
+                    state = innerright;
+                }
+                if (d > 33) {
+                    signals.set(REDFLAG);  // if the tsi value is more then 33, set the red flag, which turns on the red led.
+                    state = outerright;
+                }
+                break;
+
+            case innerright:
+                if(d < 3 || d > 9) state = None; // if the tsi value is outside of 3 and 9, go to state none.
+                break;
+            case innerleft:
+                if(d < 13 || d > 19) state = None;  // if the tsi value is outside of 13 and 19, go to state none.
+                break;
+            case outerleft:
+                if(d < 23 || d > 29)state = None;  // if the tsi value is outside of 23 and 29, go to state none.
+                break;
+            case outerright:
+                if (d < 33)state = None;  // if the tsi value is less then 33 go to state none.
+                break ;
+
+                ThisThread::sleep_for(1000) ; // This polling rate is too slow - increase it
+                // The slower rate maks it easier to output on the terminal
+        }
     }
 }
-
-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
-    
-    while (true) {
-        uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
-                                          // When no touch --> 0
-                                          // Left --> low value  Right --> high value
-        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
-    }
-}
\ No newline at end of file
diff -r 71ef35e456ab -r b62f800f9b6c mbed_app.json
--- a/mbed_app.json	Thu Feb 20 11:28:36 2020 +0000
+++ b/mbed_app.json	Thu Mar 19 17:35:19 2020 +0000
@@ -1,7 +1,19 @@
 {
+
     "config": {
+
         "main-stack-size": {
+
             "value": 2000
-        }
+
+        },
+
+        "thread_stack-size": {
+
+            "value": 2000
+
+            }
+
     }
+
 }
\ No newline at end of file