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