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 // program for lab 5 00004 // ------------------------- 00005 // Implement the functions of four virtual buttons 00006 00007 Serial pc(USBTX, USBRX); // tx, rx 00008 DigitalOut yellowLED(D6); // yellow led port 00009 DigitalOut greenLED(D7); // green led port 00010 DigitalOut redLED(D8); // red led port 00011 DigitalOut blueLED(D9); //blue led port 00012 00013 00014 TSISensor tsi; 00015 00016 Thread redThread ; // thread for red LED 00017 Thread greenThread ; // thread for green LED 00018 Thread blueThread; // thread for blue LED 00019 Thread yellowThread; //thread for yellow LED 00020 # define REDFLAG 0x01 00021 # define GREENFLAG 0x02 00022 # define BLUEFLAG 0x04 00023 # define YELLOWFLAG 0x08 00024 00025 EventFlags signals; // eventflag signal, uesd for comunicate 00026 enum pos {none, leftIn, leftOut, rightIn, rightOut}; // 5 states for the buttons 00027 00028 void red_thread() { // thread for red LED 00029 while (true) { 00030 signals.wait_any(REDFLAG); 00031 redLED = !redLED ; // on&off 00032 ThisThread::sleep_for(1000) ; // delay 00033 signals.clear(REDFLAG) ; // clear the signal flag for futher use 00034 } 00035 } 00036 00037 void green_thread() { // thread for green LED 00038 while (true) { 00039 signals.wait_any(GREENFLAG); 00040 greenLED = !greenLED ; // on&off 00041 ThisThread::sleep_for(1000) ; // delay 00042 signals.clear(GREENFLAG) ; // clear the signal flag for futher use 00043 } 00044 } 00045 00046 void blue_thread() { // thread for blue LED 00047 while (true) { 00048 signals.wait_any(BLUEFLAG); 00049 blueLED = !blueLED ; // on&off 00050 ThisThread::sleep_for(1000) ; // delay 00051 signals.clear(BLUEFLAG) ; // clear the signal flag for futher use 00052 } 00053 } 00054 00055 void yellow_thread() // thread for yellow LED 00056 { 00057 while (true) { 00058 signals.wait_any(YELLOWFLAG); 00059 yellowLED = !yellowLED ; // on&off 00060 ThisThread::sleep_for(1000) ; // delay 00061 signals.clear(YELLOWFLAG) ; // clear the signal flag for futher use 00062 } 00063 } 00064 00065 int main(void) { 00066 redLED = 0 ; 00067 greenLED = 0 ; 00068 blueLED = 0; 00069 yellowLED = 0; // beginning state for the four LEDs 00070 int pos = none; // initialise state 00071 redThread.start(red_thread) ; // red LED thread 00072 greenThread.start(green_thread) ; // green LED thread 00073 blueThread.start(blue_thread);// blue LED thread 00074 yellowThread.start(yellow_thread);// yellow LED thread 00075 00076 while (true) { 00077 uint8_t d = tsi.readDistance() ; // read the variables form the touchpad 00078 switch (pos){ 00079 00080 case none: // none state 00081 if (d > 3 && d < 9) 00082 {signals.set(YELLOWFLAG);pos = leftOut;} 00083 // if in range inside 3 - 9, left out button 00084 00085 if (d > 13 && d < 19) 00086 {signals.set(BLUEFLAG);pos = leftIn;} 00087 // if in range inside 13 - 19, leftin button 00088 00089 if (d > 23 && d < 29) 00090 {signals.set(GREENFLAG);pos = rightIn;} 00091 // if in range inside 23 - 29, right in button 00092 00093 if (d > 33) 00094 {signals.set(REDFLAG);pos = rightOut;} 00095 // if in range d bigger 33 ,right out button 00096 break; 00097 00098 case leftIn: 00099 if(d < 13 || d > 19) 00100 pos = none; // if out of the range, state goes back to none 00101 break; 00102 00103 case leftOut: 00104 if(d < 3 || d > 9) 00105 pos = none;// if out of the range, state goes back to none 00106 break; 00107 case rightIn: 00108 // case right in 00109 if(d < 23 || d > 29) 00110 pos = none;// if out of the range, state goes back to none 00111 break; 00112 case rightOut:// case right out 00113 if(d < 33) 00114 pos = none;// if out of the range, state goes back to none 00115 break; 00116 } 00117 00118 pc.printf("%d", d) ; 00119 pc.printf("%d", pos) ; 00120 pc.putc(' ') ; 00121 ThisThread::sleep_for(1000) ; 00122 00123 } 00124 }
Generated on Thu Jul 21 2022 11:03:23 by
1.7.2