The code for Lab 5

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
Tobden
Date:
Fri Mar 02 23:39:52 2018 +0000
Parent:
4:d54e74fbf82c
Child:
6:968bd77025bb
Commit message:
TSI program Lab 5

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
--- a/main.cpp	Thu Feb 22 16:59:24 2018 +0000
+++ b/main.cpp	Fri Mar 02 23:39:52 2018 +0000
@@ -10,50 +10,160 @@
 Serial pc(USBTX, USBRX); // tx, rx
 DigitalOut redLED(LED_RED);
 DigitalOut greenLED(LED_GREEN);
+DigitalOut blueLED(LED_BLUE);
+DigitalOut led1(D8);
+enum positions{LeftOut, RightOut, LeftIn, RightIn, None};
 TSISensor tsi;
 
-Thread redThread ; // thread for red LED
-Thread greenThread ; // thread for green LED
- 
+Thread redThread;//(osPriorityNormal, 1000); // thread for red LED
+Thread greenThread;//(osPriorityNormal, 1000); // thread for green LED
+Thread blueThread;//(osPriorityNormal, 1000);
+Thread whiteThread;//(osPriorityNormal, 1000);
+volatile int numb = 1;
+
 void red_thread() {  // method to run in thread
-    while (true) {
+        while (true) {
         Thread::signal_wait(0x1);
-        redLED = false ; // turn on 
-        Thread::wait(5000);
-        redLED = true ; // turn off 
-        redThread.signal_clr(0x1) ;
+        redLED = !redLED ; // turn on
+        //blueLED = true;
+        //greenLED = true;
+       // Thread::wait(5000);
+       // redLED = true ; // turn off
+       redThread.signal_clr(0x1) ;
           // Signal are automatically cleared by wait_signal but
-          // the signal might have been set again while LED on 
+          // the signal might have been set again while LED on
     }
 }
 
 void green_thread() {  // method to run in thread
-    while (true) {
+        while (true) {
         Thread::signal_wait(0x1);
-        greenLED = false ; // turn on 
-        Thread::wait(5000);
-        greenLED = true ; // turn off 
+        greenLED = !greenLED ; // turn on
+       // blueLED = true;
+        //redLED = true;
+       // Thread::wait(5000);
+       // greenLED = true ; // turn off
         greenThread.signal_clr(0x1) ;
           // Signal are automatically cleared by wait_signal but
-          // the signal might have been set again while LED on 
+          // the signal might have been set again while LED on
+    }
+}
+
+void blue_thread() {  // method to run in thread
+        while (true) {
+        Thread::signal_wait(0x1);
+        blueLED = !blueLED ; // turn on
+        //redLED = true;
+        //greenLED = true;
+        //Thread::wait(5000);
+        //blueLED = true ; // turn off
+        blueThread.signal_clr(0x1) ;
+          // Signal are automatically cleared by wait_signal but
+          // the signal might have been set again while LED on
+    }
+}
+
+
+void white_thread() {  // method to run in thread
+        while (true) {
+        Thread::signal_wait(0x1);
+        led1 = !led1;
+      //  Thread::wait(5000);
+        //redLED = true ; // turn off
+        //blueLED = true ; // turn off
+        //greenLED = true ; // turn off
+        whiteThread.signal_clr(0x1) ;
+          // Signal are automatically cleared by wait_signal but
+          // the signal might have been set again while LED on
     }
 }
 
+
 int main(void) {
-    redLED = true ; // turn off 
-    greenLED = true ; // turn off 
+    redLED = true ; // turn off
+    greenLED = true ; // turn off
+    blueLED = true;
     redThread.start(&red_thread) ; // start the red thread
     greenThread.start(&green_thread) ; // start the green thread
-    
+    blueThread.start(&blue_thread) ; // start the green thread
+    whiteThread.start(&white_thread) ; // start the green thread
+    positions area = None;
+
     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.printf("%d", d) ;
         pc.putc(' ') ;
-        if (d == 10) redThread.signal_set(0x1) ;
-        if (d == 20) greenThread.signal_set(0x1) ;
+        switch (area)
+        {
+case LeftOut:
+
+         if (numb == 0)
+
+                 redLED =  redLED;
+            if (d<3 || d>9)
+                    {numb = 1;
+                     area = None;
+                      break;
+              }
+case LeftIn:
+          if (numb == 0)
+                greenLED = greenLED;
+                     if (d<13 || d > 19){
+                            numb = 1;
+                            area = None;
+                             break;
+        }
+ case RightIn:
+        if (numb == 0)
+                blueLED = blueLED;
+                    if (d< 23 || d > 29){
+                          numb = 1;
+                            area = None;
+                            break;
+        }
+case RightOut:
+        if (numb == 0)
+            led1 = led1;
+                    if (d<33)
+                         {numb = 1;
+                          area = None;
+                             break;
+                        }
+case None:
+        if (d>3 && d < 9){
+                if (numb == 1){
+                 redThread.signal_set(0x1);
+                numb = 0;
+                area = LeftOut ;
+                break;}
+       }
+                else if (d > 13 && d < 19)
+                        {if (numb == 1){
+                        greenThread.signal_set(0x1);
+                        numb = 0;
+                        area = LeftIn;
+                         break;}
+       }
+                else if (d > 23 && d < 29)
+                        {if (numb == 1){
+                        blueThread.signal_set(0x1);
+                        numb = 0;
+                        area = RightIn;
+                        break;}
+       }
+
+             else if (d > 33)
+                    {if (numb == 1){
+                    whiteThread.signal_set(0x1);
+                    numb = 0;
+                     area = RightOut;
+                    break;}
+       }
+       }
         Thread::wait(200);  // 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
+}
--- a/mbed_app.json	Thu Feb 22 16:59:24 2018 +0000
+++ b/mbed_app.json	Fri Mar 02 23:39:52 2018 +0000
@@ -1,7 +1,7 @@
 {
     "config": {
-        "main-stack-size": {
-            "value": 2000
+        "thread-stack-size": {
+            "value": 1000
         }
     }
 }
\ No newline at end of file