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 //Lab 5 part 1 00005 //LED light up according to which button is pressed 00006 00007 Serial pc(USBTX, USBRX); // tx, rx 00008 DigitalOut led1(PTD3,0); 00009 DigitalOut led2(PTD5,0); 00010 DigitalOut led3(PTD0,0); 00011 DigitalOut led4(PTD2,0); 00012 TSISensor tsi; 00013 00014 Thread l_out ; 00015 Thread l_in ; 00016 Thread r_out ; 00017 Thread r_in ; 00018 00019 enum pos{neutral, lo, li, ri, ro}; 00020 enum ledonoff{on, off}; 00021 00022 # define lout 0x01 00023 # define lin 0x02 00024 # define rin 0x04 00025 # define rout 0x08 00026 00027 EventFlags signals; 00028 00029 void leftout() { 00030 int l1=on; 00031 while (true) { 00032 switch (l1){ 00033 case on: 00034 signals.wait_any(lout);//Wait until flag 00035 //pc.printf("lo "); 00036 led1=1; 00037 l1=off;//Turn LED on or off 00038 signals.clear(lout); 00039 break; 00040 case off: 00041 signals.wait_any(lout); 00042 //pc.printf("lo "); 00043 led1=0; 00044 l1=on; 00045 signals.clear(lout); 00046 break; 00047 } 00048 } 00049 } 00050 void leftin() { 00051 int l2=on; 00052 while (true) { 00053 switch (l2){ 00054 case on: 00055 signals.wait_any(lin); 00056 //pc.printf("li "); 00057 led2=1; 00058 l2=off; 00059 signals.clear(lin); 00060 break; 00061 case off: 00062 signals.wait_any(lin); 00063 //pc.printf("li "); 00064 led2=0; 00065 l2=on; 00066 signals.clear(lin); 00067 break; 00068 } 00069 } 00070 } 00071 void rightin() { 00072 int l3=on; 00073 while (true) { 00074 switch (l3){ 00075 case on: 00076 signals.wait_any(rin); 00077 //pc.printf("ri "); 00078 led3=1; 00079 l3=off; 00080 signals.clear(rin); 00081 break; 00082 case off: 00083 signals.wait_any(rin); 00084 //pc.printf("ri "); 00085 led3=0; 00086 l3=on; 00087 signals.clear(rin); 00088 break; 00089 } 00090 } 00091 } 00092 void rightout() { 00093 int l4=on; 00094 while (true) { 00095 switch (l4){ 00096 case on: 00097 signals.wait_any(rout); 00098 //pc.printf("ro "); 00099 led4=1; 00100 l4=off; 00101 signals.clear(rout); 00102 break; 00103 case off: 00104 signals.wait_any(rout); 00105 //pc.printf("ro "); 00106 led4=0; 00107 l4=on; 00108 signals.clear(rout); 00109 break; 00110 } 00111 } 00112 } 00113 00114 int main(void) { 00115 int pos=neutral; 00116 uint8_t d; 00117 pc.printf("START"); 00118 l_out.start(leftout) ; 00119 l_in.start(leftin) ; 00120 r_in.start(rightin); 00121 r_out.start(rightout); 00122 00123 while (true) { 00124 d = tsi.readDistance() ; // Distance is between 0 and 39 00125 // When no touch --> 0 00126 // Left --> low value Right --> high value 00127 switch (pos){ 00128 case neutral: 00129 if (d>3 && d<9){pos=lo; signals.set(lout);} //Set flags and change states 00130 if (d>13 && d<19){pos=li; signals.set(lin);} 00131 if (d>23 && d<29){pos=ri; signals.set(rin);} 00132 if (d>33){pos=ro; signals.set(rout);} 00133 break; 00134 case lo: 00135 if (d<3 || d>9) pos=neutral; 00136 break; 00137 case li: 00138 if (d<13 || d>19) pos=neutral; 00139 break; 00140 case ri: 00141 if (d<23 || d>29) pos=neutral; 00142 break; 00143 case ro: 00144 if (d<33) pos=neutral; 00145 break; 00146 } 00147 pc.printf("%d", d) ; 00148 pc.putc(' ') ; 00149 ThisThread::sleep_for(100) ; 00150 } 00151 }
Generated on Thu Jul 14 2022 05:31:52 by
1.7.2