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 blueLED(D5); // first LED 00012 DigitalOut greenLED(D6); // second LED 00013 DigitalOut yellowLED(D7); // third led 00014 DigitalOut redLED(D8); // fourth led 00015 TSISensor tsi; 00016 00017 Thread redThread ; // thread for red LED 00018 Thread greenThread ; // thread for green LED 00019 Thread blueThread ; // thread for blue LED 00020 Thread yellowThread ; // thread for yellow LED 00021 00022 # define BLUEFLAG 0x01 00023 # define GREENFLAG 0x02 00024 # define YELLOWFLAG 0x04 00025 # define REDFLAG 0x08 00026 00027 EventFlags signals; // event flags for signalling; 2 used 00028 00029 void blue_thread() // method to run in thread 00030 { 00031 while (true) { 00032 signals.wait_any(BLUEFLAG); // wait for signal 00033 blueLED = !blueLED ; // toggle 00034 ThisThread::sleep_for(1000) ; // wait(1.0); 00035 signals.clear(BLUEFLAG) ; 00036 // Signal are automatically cleared by wait_any but 00037 // the signal might have been set again while LED on 00038 } 00039 } 00040 00041 void green_thread() // method to run in thread 00042 { 00043 while (true) { 00044 signals.wait_any(GREENFLAG); 00045 greenLED = !greenLED ; // toggle 00046 ThisThread::sleep_for(1000) ; // wait(1.0); 00047 signals.clear(GREENFLAG) ; 00048 // Signal are automatically cleared by wait_any but 00049 // the signal might have been set again while LED on 00050 } 00051 } 00052 void yellow_thread() // method to run in thread 00053 { 00054 while (true) { 00055 signals.wait_any(YELLOWFLAG); 00056 yellowLED = !yellowLED ; // toggle 00057 ThisThread::sleep_for(1000) ; // wait(1.0); 00058 signals.clear(YELLOWFLAG) ; 00059 // Signal are automatically cleared by wait_any but 00060 // the signal might have been set again while LED on 00061 } 00062 } 00063 void red_thread() // method to run in thread 00064 { 00065 while (true) { 00066 signals.wait_any(REDFLAG); 00067 redLED = !redLED ; // toggle 00068 ThisThread::sleep_for(1000) ; // wait(1.0); 00069 signals.clear(REDFLAG) ; 00070 // Signal are automatically cleared by wait_any but 00071 // the signal might have been set again while LED on 00072 } 00073 } 00074 00075 00076 int main(void) 00077 { 00078 redLED = 0 ; // turn off 00079 greenLED = 0 ; // turn off 00080 blueLED = 0 ; 00081 yellowLED = 0; 00082 redThread.start(red_thread) ; // start the red thread 00083 greenThread.start(green_thread) ; // start the green thread 00084 blueThread.start(blue_thread) ; // start the blue thread 00085 yellowThread.start(yellow_thread) ; // start the yellow thread 00086 00087 enum state {None, outerleft, innerleft, innerright, outerright}; 00088 int state = None; 00089 while (true) { 00090 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 00091 // When no touch --> 0 00092 // Left --> low value Right --> high value 00093 pc.printf("%d", d) ; 00094 pc.putc(' ') ; 00095 00096 00097 switch(state) { 00098 case None: 00099 if (d > 3 && d < 9) { 00100 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. 00101 state = outerleft; 00102 } 00103 if (d > 13 && d < 19) { 00104 signals.set(GREENFLAG); // if the tsi value is within 13 and 19, set the green flag, which turns on the green led. 00105 state = innerleft; 00106 } 00107 if (d > 23 && d < 29) { 00108 signals.set(YELLOWFLAG); // if the tsi value is within 23 and 29, set the yellow flag, which turns on the yellow led. 00109 state = innerright; 00110 } 00111 if (d > 33) { 00112 signals.set(REDFLAG); // if the tsi value is more then 33, set the red flag, which turns on the red led. 00113 state = outerright; 00114 } 00115 break; 00116 00117 case innerright: 00118 if(d < 3 || d > 9) state = None; // if the tsi value is outside of 3 and 9, go to state none. 00119 break; 00120 case innerleft: 00121 if(d < 13 || d > 19) state = None; // if the tsi value is outside of 13 and 19, go to state none. 00122 break; 00123 case outerleft: 00124 if(d < 23 || d > 29)state = None; // if the tsi value is outside of 23 and 29, go to state none. 00125 break; 00126 case outerright: 00127 if (d < 33)state = None; // if the tsi value is less then 33 go to state none. 00128 break ; 00129 00130 ThisThread::sleep_for(1000) ; // This polling rate is too slow - increase it 00131 // The slower rate maks it easier to output on the terminal 00132 } 00133 } 00134 }
Generated on Thu Aug 25 2022 20:42:18 by
1.7.2