Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "TSISensor.h" 00003 00004 // Example program for lab 5 00005 // ------------------------- 00006 // A value is read from the touch sensor and use 00007 // to control two LEDs 00008 // The value is also output to the serial interface 00009 00010 Serial pc(USBTX, USBRX); // tx, rx 00011 DigitalOut redLED(LED_RED); 00012 DigitalOut greenLED(LED_GREEN); 00013 DigitalOut blueLED(LED_BLUE); 00014 TSISensor tsi; 00015 00016 Thread redThread ; // thread for red LED 00017 Thread greenThread ; // thread for green LED 00018 Thread blueThread; 00019 Thread whiteThread; 00020 00021 # define REDFLAG 0x01 00022 # define GREENFLAG 0x02 00023 # define BLUEFLAG 0x04 00024 # define WHITEFLAG 0x08 00025 EventFlags signals; // event flags for signalling; 2 used 00026 00027 enum redLEDState {REDOn,REDOff}; 00028 void red_thread() { // method to run in thread 00029 redLEDState redstate = REDOff; 00030 while (true) { 00031 switch(redstate){ 00032 case REDOff: 00033 signals.wait_any(REDFLAG); 00034 redLED = false; 00035 wait(0.5); 00036 redstate = REDOn; 00037 break; 00038 case REDOn: 00039 redLED = true ; // turn on 00040 signals.clear(REDFLAG); 00041 redstate = REDOff; 00042 } 00043 } 00044 } 00045 enum greenLEDState {GREENOn,GREENOff}; 00046 void green_thread() { // method to run in thread 00047 greenLEDState greenstate = GREENOff; 00048 while (true) { 00049 switch(greenstate){ 00050 case GREENOff: 00051 signals.wait_any(GREENFLAG); 00052 greenLED = false ; 00053 wait(0.5); 00054 greenstate = GREENOn; 00055 break; 00056 case GREENOn: 00057 greenLED = true ; // turn on 00058 signals.clear(GREENFLAG); 00059 greenstate = GREENOff; 00060 } 00061 } 00062 } 00063 enum blueLEDState {BLUEOn,BLUEOff}; 00064 void blue_thread() { // method to run in thread 00065 blueLEDState bluestate = BLUEOff; 00066 while (true) { 00067 switch(bluestate){ 00068 case BLUEOff: 00069 signals.wait_any(BLUEFLAG); 00070 blueLED = false ; 00071 wait(0.5); 00072 bluestate = BLUEOn; 00073 break; 00074 case BLUEOn: 00075 blueLED = true ; // turn on 00076 signals.clear(BLUEFLAG); 00077 bluestate = BLUEOff; 00078 } 00079 } 00080 } 00081 enum whiteLEDState {WHITEOn,WHITEOff}; 00082 void white_thread() { // method to run in thread 00083 whiteLEDState whitestate = WHITEOff; 00084 while (true) { 00085 switch(whitestate){ 00086 case WHITEOff: 00087 signals.wait_any(WHITEFLAG); 00088 blueLED = false ; // turn on 00089 redLED = false ; 00090 greenLED = false ; 00091 wait(0.5); 00092 whitestate = WHITEOn; 00093 break; 00094 case WHITEOn: 00095 blueLED = true ; // turn off 00096 redLED = true ; 00097 greenLED = true ; 00098 signals.clear(WHITEFLAG); 00099 whitestate = WHITEOff; 00100 } 00101 } 00102 } 00103 00104 enum states {LeftIn,LeftOut,RightIn,RightOut,None}; 00105 00106 int main(void) { 00107 redLED = true ; // turn off 00108 greenLED = true ; // turn off 00109 blueLED = true ; 00110 redThread.start(red_thread) ; // start the red thread 00111 greenThread.start(green_thread) ; // start the green thread 00112 blueThread.start(blue_thread); 00113 whiteThread.start(white_thread); 00114 00115 states State = None; 00116 00117 while (true) { 00118 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 00119 // When no touch --> 0 00120 // Left --> low value Right --> high value 00121 pc.printf("%d", d) ; 00122 pc.putc(' ') ; 00123 wait(0.1); 00124 switch(State){ 00125 case None: 00126 if (d > 3 && d < 9) {signals.set(REDFLAG);State = LeftOut;} 00127 else if (d > 13 && d < 19) {signals.set(GREENFLAG) ;State = LeftIn; } 00128 else if (d > 23 && d < 29) {signals.set(BLUEFLAG) ;State = RightIn;} 00129 else if (d > 33) {signals.set(WHITEFLAG) ;State = RightOut;} 00130 break; 00131 case LeftOut: 00132 if (d < 3 || d >9) State = None;break; 00133 case LeftIn: 00134 if (d < 13 || d >19) State = None;break; 00135 case RightIn: 00136 if (d < 23 || d >29) State = None;break; 00137 case RightOut: 00138 if (d < 33) State = None;break; 00139 } 00140 } 00141 }
Generated on Sun Jul 24 2022 16:52:15 by
1.7.2