lab 5 part 2

Dependencies:   TSI

Files at this revision

API Documentation at this revision

Comitter:
sebbarpar
Date:
Mon Mar 16 12:01:39 2020 +0000
Parent:
8:ca714e821245
Commit message:
Finished lab 5 part 2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 16 11:37:43 2020 +0000
+++ b/main.cpp	Mon Mar 16 12:01:39 2020 +0000
@@ -1,11 +1,7 @@
 #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 2
 
 Serial pc(USBTX, USBRX); // tx, rx
 PwmOut redLED(LED_RED);
@@ -15,8 +11,8 @@
 
 enum pos{neutral, lo, li, ri, ro};
 
-Thread green ; // thread for red LED
-Thread red ; // thread for green LED
+Thread green ; // thread for green LED
+Thread red ; // thread for red LED
 
 # define lout 0x01   
 # define lin 0x02
@@ -24,44 +20,41 @@
 # define rout 0x08  
 EventFlags signals;  
 volatile uint32_t flags_red;
-
 volatile uint32_t flags_green;
 
 void changebrightnessred() {  // method to run in thread
     float i=0;
     while (true) {
-        signals.wait_any(lin,osWaitForever,false);
-        signals.clear(rin);
+        signals.wait_any(lin,osWaitForever,false);//Run thread indefinitely after left is is pressed
         flags_red=signals.wait_any(lout|rout,osWaitForever,true);
-        if (flags_red==lout){
+        if (flags_red==3){//If left out is pressed, less brightness
             if (i!=0) i--;
         }
-        if (flags_red==rout){
+        if (flags_red==10){//If right out is pressed, more brightness
             if (i!=9) i++;
         }
         signals.clear(lout); 
         signals.clear(rout);
-        pc.printf("red: %f   \n ", i);
-        redLED= i/10;
+        //pc.printf("flags_red: %d, red: %f   \n ",flags_red, i);
+        redLED= 1-i/10;
     }
 }
 
 void changebrightnessgreen() {  // method to run in thread
     float i=0;
     while (true) {
-        signals.wait_any(rin,osWaitForever,false);
-        signals.clear(lin);
+        signals.wait_any(rin,osWaitForever,false);//Run indefinitely after right in pressed
         flags_green=signals.wait_any(lout|rout,osWaitForever,true);
-        if (flags_green==lout){
+        if (flags_green==5){//Left out less brightness
             if (i!=0) i--;
         }
-        if (flags_green==rout){
+        if (flags_green==12){//Right out more brightness
             if (i!=9) i++;
         }
         signals.clear(lout); 
         signals.clear(rout);
-        pc.printf("green: %f \n", i);
-        greenLED= i/10;
+        //pc.printf("flags_green: %d,  green: %f \n",flags_green,  i);
+        greenLED= 1-i/10;
     }
 }
 
@@ -78,12 +71,12 @@
         switch (pos){
             case neutral:
                 if (d>3 && d<9){pos=lo; signals.set(lout); t.start();}    
-                if (d>13 && d<19){pos=li; signals.set(lin);} 
-                if (d>23 && d<29){pos=ri; signals.set(rin);} 
+                if (d>13 && d<19){pos=li; signals.set(lin); signals.clear(rin);} //Select LED
+                if (d>23 && d<29){pos=ri; signals.set(rin);signals.clear(lin);} //Select LED
                 if (d>33){pos=ro; signals.set(rout); t.start();} 
             break;
             case lo:
-                if (t.read()>1) {signals.set(lout); t.reset();}
+                if (t.read()>1) {signals.set(lout); t.reset();}//Repeat event after 1 second
                 if (d<3 || d>9) {pos=neutral; t.reset(); t.stop();}
             break;
             case li:
@@ -93,13 +86,12 @@
                 if (d<23 || d>29) {pos=neutral; }
             break;
             case ro:
-                if (t.read()>1) {signals.set(rout);t.reset();}
+                if (t.read()>1) {signals.set(rout);t.reset();}//repeat event after 1 second
                 if (d<33) {pos=neutral; t.reset(); t.stop();}
             break;
         }
-        pc.printf("%d", d) ;  
-        pc.putc(' ') ;
-        ThisThread::sleep_for(100) ; // This polling rate is too slow - increase it
-                    // The slower rate maks it easier to output on the terminal
-    }
+        //pc.printf("%d, position: %d", d, pos) ;  
+        //pc.putc(' ') ;
+        ThisThread::sleep_for(50) ; 
+        }
 }
\ No newline at end of file