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); 00005 int touchSense2(void); 00006 void readSerial(); 00007 char touchDetect (); 00008 00009 DigitalOut myled1(LED1); 00010 DigitalOut myled2(LED2); 00011 DigitalOut myled3(LED3); 00012 AnalogIn input1(p20); 00013 DigitalIn charger1(p19); 00014 DigitalOut ground1(p18); 00015 AnalogIn input2(p17); 00016 DigitalIn charger2(p16); 00017 DigitalOut ground2(p15); 00018 00019 00020 Serial pc(USBTX, USBRX); // tx, rx 00021 00022 char *serial = new char [100]; 00023 00024 int main() { 00025 00026 pc.attach (&readSerial); //Foreground: read the serial 00027 00028 00029 while (1){ //Background: compare the serial value and user input. 00030 int i=1,j=0; 00031 while (serial[j]!='E') { //Print the host value; 00032 pc.printf ("%c", serial[j]); 00033 j++; 00034 } 00035 pc.printf ("E"); 00036 00037 while (1) { 00038 pc.printf (" The next input should be :"); //Hint 00039 pc.printf ("%c", serial[i]); 00040 if (serial[i]=='E'){ 00041 pc.printf (" MATCH "); 00042 pc.printf ("\r\n"); 00043 i=1; 00044 break; 00045 } 00046 else if (serial[i]==touchDetect()){ 00047 i++; 00048 wait (0.5); 00049 } 00050 else{ 00051 pc.printf (" TOUCH ERROR "); 00052 pc.printf ("\r\n"); //If the user touch both sensor, the retun value is 3, which will lead to touch error. 00053 break;} //Also, if the user mistouch the sensor, it's also a touch error. 00054 } 00055 } 00056 } 00057 00058 void readSerial() 00059 { 00060 char b; 00061 int flag=1,i=0; 00062 b=pc.getc(); 00063 pc.printf("%c",b); 00064 if (b=='S'){ 00065 serial[0]='S'; 00066 i++; 00067 while (flag){ 00068 serial[i]=pc.getc(); 00069 pc.printf ("%c",serial[i]); 00070 if (serial[i]=='E'){ 00071 flag=0;} 00072 else if (serial[i]=='S'){ 00073 pc.printf (" HOST ERROR \r\n "); //Host sent two S before E. Host error. 00074 break;} 00075 i++; 00076 } 00077 } 00078 } 00079 00080 char touchDetect () //Detect the user input 00081 { 00082 while (1) { 00083 if (touchSense1()){ 00084 pc.printf (" T1 "); //T1 means sensor1 is pressed 00085 if (touchSense2()){ 00086 return '3'; 00087 } 00088 while (touchSense1()){ 00089 wait(0.5);} 00090 return '1'; 00091 } 00092 else if (touchSense2()){ 00093 pc.printf (" T0 "); 00094 if (touchSense1()){ 00095 return '3'; 00096 } 00097 while (touchSense2()){ 00098 wait(0.5);} 00099 return '0'; 00100 00101 } 00102 wait (0.05); 00103 } 00104 } 00105 00106 int touchSense1(void) 00107 { 00108 float sample; 00109 ground1 = 0; 00110 charger1.mode(PullUp); 00111 charger1.mode(PullNone); 00112 sample=input1.read(); 00113 if (sample < 0.3) { 00114 return 1; 00115 } else { 00116 return 0; 00117 } 00118 } 00119 00120 int touchSense2(void) 00121 { 00122 float sample; 00123 ground2 = 0; 00124 charger2.mode(PullUp); 00125 charger2.mode(PullNone); 00126 sample=input2.read(); 00127 if (sample < 0.3) { 00128 return 1; 00129 } else { 00130 return 0; 00131 } 00132 }
Generated on Mon Aug 22 2022 13:40:23 by
1.7.2