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 "lc.h" 00003 #include "pt-sem.h" 00004 #include "pt.h" 00005 00006 #define numsamples 1 00007 int touchSense1(void); //function for the button 1 00008 00009 DigitalOut myled1(LED1); 00010 AnalogIn input1(p20); 00011 DigitalIn charger1(p19); 00012 DigitalOut ground1(p18); 00013 Serial pc(USBTX, USBRX); // tx, rx 00014 00015 int touchSense2(void); //function for the button 2 00016 00017 DigitalOut myled2(LED4); 00018 AnalogIn input2(p15); 00019 DigitalIn charger2(p16); 00020 DigitalOut ground2(p17); 00021 00022 char incode[10]; 00023 char strcode[10]; 00024 char matcode[10]; 00025 char temp; 00026 int codelen = 1; 00027 int matlen; 00028 int i=0; 00029 int j=0; 00030 int flag1 = 0; 00031 int flag2 = 0; 00032 int failflag = 0; 00033 00034 int protothread1_flag, protothread2_flag; 00035 static struct pt pt1, pt2; 00036 00037 00038 static int protothread1(struct pt *pt){ 00039 00040 PT_BEGIN(pt); 00041 00042 while(1) { 00043 00044 PT_WAIT_UNTIL(pt, protothread2_flag != 0); 00045 00046 if (pc.readable()){ 00047 protothread2_flag = 0; 00048 protothread1_flag = 1; 00049 } 00050 00051 if (touchSense1()) { 00052 wait (0.005); //wait 5ms for checking press 00053 if (touchSense1()){ 00054 myled1 = 1; 00055 if (flag1 == 0){ 00056 flag1 =1; //flag1 for symbol press 00057 pc.printf("1"); 00058 matcode[matlen] = '1'; //record for compare 00059 matlen ++; 00060 } 00061 } 00062 00063 } else { 00064 wait(0.005); //wait 5ms confirm release 00065 if (touchSense1()==0){ 00066 myled1 = 0; 00067 flag1 = 0; //clear flag 00068 } 00069 } 00070 00071 if (touchSense2()) { //samiliar as above 1 00072 wait (0.005); 00073 if (touchSense2()){ 00074 myled2 = 1; 00075 if (flag2 == 0){ 00076 flag2 =1; 00077 pc.printf("0"); 00078 matcode[matlen] = '0'; 00079 matlen ++; 00080 } 00081 } 00082 00083 } else { 00084 wait(0.005); 00085 if (touchSense2()==0){ 00086 myled2 = 0; 00087 flag2 = 0; 00088 } 00089 } 00090 00091 if (flag1 == 1 && flag2 == 1){ //detect press both button 00092 pc.printf("\nError, please don't touch both!!"); 00093 //break; 00094 } 00095 00096 if (matlen >= codelen){ //check input length match the set up 00097 for (i=0;i<codelen;i++){ 00098 if (matcode[i] != strcode[i]) 00099 failflag = 1; //flag for symbol not match 00100 } 00101 00102 if (failflag == 0){ 00103 pc.printf("\n Congratulations! Code is matched!!\n"); 00104 matlen = 0; 00105 00106 } 00107 00108 else{ 00109 pc.printf("\nSorry, the code is wrong, please input again!!\n"); 00110 failflag = 0; 00111 matlen = 0; 00112 } 00113 00114 } 00115 } 00116 00117 00118 PT_END(pt); 00119 } 00120 00121 00122 00123 static int protothread2(struct pt *pt) { 00124 00125 PT_BEGIN(pt); 00126 while(1) { 00127 00128 protothread2_flag = 1; 00129 PT_WAIT_UNTIL(pt, protothread1_flag != 0); 00130 00131 i=0; 00132 j=0; 00133 pc.printf("\nPlease inpunt the code:\n"); 00134 while ((temp = pc.getc())!= 'E') { //getchar 00135 pc.printf("%c",temp); 00136 incode[i] = temp; //put the original char in incode 00137 i++; 00138 } 00139 00140 incode[i] = 'E'; //put "E" last as the end symbol 00141 pc.printf("%c",temp); 00142 pc.printf("\nThanks for the string, your input is\n"); 00143 00144 for (i=0;i<10;i++){ //extract 0/1 from incode to strcode 00145 if (incode[i] == 'E') 00146 break; 00147 else{ 00148 if (incode[i] == '0' || incode[i] == '1'){ 00149 strcode[j] = incode[i]; 00150 j++; 00151 } 00152 } 00153 00154 } 00155 00156 codelen = j; 00157 00158 for (j=0;j<codelen;j++) //display the configuration 00159 pc.printf("%c",strcode[j]); 00160 pc.printf("\n"); 00161 00162 matlen = 0; 00163 00164 pc.printf("\nPlease input the code:"); 00165 00166 protothread1_flag = 0; 00167 00168 } 00169 PT_END(pt); 00170 } 00171 00172 00173 int main() 00174 { 00175 for (i=0;i<10;i++){ //initial the char array 00176 incode[i] = 'a'; 00177 strcode[i] = 'a'; 00178 } 00179 00180 PT_INIT(&pt1); 00181 PT_INIT(&pt2); 00182 00183 while(1) { 00184 protothread1(&pt1); 00185 protothread2(&pt2); 00186 } 00187 } 00188 00189 int touchSense1(void) //function for touch sensor 00190 { 00191 float sample; 00192 ground1 = 0; 00193 charger1.mode(PullUp); 00194 charger1.mode(PullNone); 00195 sample=input1.read(); 00196 if (sample < 0.3) { 00197 return 1; 00198 } else { 00199 return 0; 00200 } 00201 } 00202 int touchSense2(void) 00203 { 00204 float sample; 00205 ground2 = 0; 00206 charger2.mode(PullUp); 00207 charger2.mode(PullNone); 00208 sample=input2.read(); 00209 if (sample < 0.3) { 00210 return 1; 00211 } else { 00212 return 0; 00213 } 00214 }
Generated on Sun Jul 31 2022 04:41:30 by
1.7.2